定时器
概述
Beaver IoT 平台提供了定时任务功能,集成可以在Spring Bean中通过@IntegrationScheduled
注解来快速实现定时任务, 并且不用关心定时任务在集群中的调度问题。
在代码中配置
类似于Spring Boot的@Scheduled
注解, @IntegrationScheduled
注解允许开发者在需要定期执行的方法上直接配置定时任务规则。需要注意的是, name字段必须被设置, 且全局唯一。
信息
通过这种方式配置的定时任务, 没有租户上下文
固定频率执行
通过@IntegrationScheduled
注解的fixedRate
和timeUnit
字段来设置固定频率执行某个方法。
@Component
public class ScheduledTasks {
private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@IntegrationScheduled(
name = "scheduled_tasks.report_current_time",
enabled = true,
fixedRate = 5,
timeUnit = TimeUnit.SECONDS
)
public void reportCurrentTime() {
log.info("The time is now {}", dateFormat.format(new Date()));
}
}
Cron表达式
@IntegrationScheduled
注解的cron
字段支持设置Spring的Cron表达式格式。另外,可以通过timeZone字段设置Cron表达式的时区,时区必须是标准的TZID,例如:Asia/Shanghai
。
@Component
public class ScheduledTasks {
private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@IntegrationScheduled(
name = "scheduled_tasks.report_current_time",
enabled = true,
cron = "0 * * * * *",
timeZone = "UTC"
)
public void reportCurrentTime() {
log.info("The time is now {}", dateFormat.format(new Date()));
}
}