1、引入包
<!--spring2.0集成quartz -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
2、
配置定时器
@Configuration
public class QuartzConfig {
/**
* 指定任务类
*
* @return 任务
*/
@Bean
public JobDetail tokenClearQuartzDetail() {
return JobBuilder
// 定期清理sys_token中的失效token
.newJob(MyTask.class).withIdentity("tokenClear")
.storeDurably().build();
}
/**
* 触发器
*
* @return Trigger实例
*/
@Bean
public Trigger tokenClearQuartzTrigger() {
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
// 设置时间周期单位分钟
.withIntervalInSeconds(10)
.repeatForever();
return TriggerBuilder.newTrigger().forJob(tokenClearQuartzDetail())
.withIdentity("tokenClear")
.withSchedule(scheduleBuilder)
.build();
}
}
3、添加任务
public class MyTask extends QuartzJobBean {
/**
* 日志工具
*/
private static final Logger logger = LoggerFactory.getLogger(MyTask.class);
/**
* 执行定时任务
*
* @param jobExecutionContext
*/
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) {
//处理任务
logger.debug("任务{}", 1);
}
}