注解使用
定时任务使用。
先在springboot入口类添加注释: EnableScheduling
@SpringBootApplication
@EnableScheduling
public class GirlApplication {
public static void main(String[] args) {
SpringApplication.run(GirlApplication.class, args);
}
}
再在定时类上添加注解,感谢网友提供的可视化Cron表达式生成器
@Component
public class printScheduler {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Scheduled(cron="0/5 * * * * ?")
public void scheduleCheck() {
logger.info("每5秒执行一次");
}
}
自定义使用
参考:https://www.jianshu.com/p/deede3837293
package com.jege.spring.boot.task;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import com.jege.spring.boot.data.jpa.entity.User;
import com.jege.spring.boot.data.jpa.repository.UserRepository;
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:动态修改定时任务cron参数
*/
@Component
public class DynamicScheduledTask implements SchedulingConfigurer {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
private static final String DEFAULT_CRON = "0/5 * * * * ?";
private String cron = DEFAULT_CRON;
@Autowired
private UserRepository userRepository;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
if (!cron.equals(DEFAULT_CRON)) {
User user = new User("je_ge", 20);
userRepository.save(user);
}
// 定时任务的业务逻辑
System.out.println("动态修改定时任务cron参数,当前时间:" + dateFormat.format(new Date()));
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
// 定时任务触发,可修改定时任务的执行周期
CronTrigger trigger = new CronTrigger(cron);
Date nextExecDate = trigger.nextExecutionTime(triggerContext);
return nextExecDate;
}
});
}
public void setCron(String cron) {
this.cron = cron;
}
}
启动类
@EnableScheduling
修改
@Autowired
DynamicScheduledTask dynamicScheduledTask;
// 更新动态任务时间
@RequestMapping("/updateDynamicScheduledTask")
@ResponseBody
public AjaxResult updateDynamicScheduledTask() {
dynamicScheduledTask.setCron("0/10 * * * * ?");
return new AjaxResult().success();
}