使用spring自带的scheduling定时调度任务相当于轻量级的Quartz,但是不支持分布式,若要实现分布式定时任务就得使用Quartz了.
第一步,在入口类中声明定时任务
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling //声明定时任务 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class,args); } }
第二步,创建定时任务对象,需要实现SchedulingConfigurer接口,其中有两个入参doTask业务逻辑,getTrigger触发器
import com.zenithink.demo.controller.MyDynamicTask; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; 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 java.text.SimpleDateFormat; import java.util.Date; //@Component public class ScheduledUtil implements SchedulingConfigurer { private static Logger log = LoggerFactory.getLogger(MyDynamicTask.class); private String cron="0/10 * * * * ?"; private String name="测试"; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCron() { return cron; } public void setCron(String cron) { this.cron = cron; } @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { scheduledTaskRegistrar.addTriggerTask(doTask(), getTrigger()); } /** * 业务执行方法 * @return */ private Runnable doTask() { return new Runnable() { @Override public void run() { SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 业务逻辑 log.info(name+",时间为:" + simpleDateFormat.format(new Date())); } }; } /** * 业务触发器 * @return */ private Trigger getTrigger() { return new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) { // 触发器 CronTrigger trigger = new CronTrigger(cron); return trigger.nextExecutionTime(triggerContext); } }; } }
第三步,创建多个定时任务对象,并注入spring容器
import com.zenithink.demo.utils.ScheduledUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; 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 org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.naming.Name; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping("/test") public class MyDynamicTask { private static Logger log = LoggerFactory.getLogger(MyDynamicTask.class); @Autowired @Qualifier("test1") private ScheduledUtil scheduledUtil1; @Autowired @Qualifier("test2") private ScheduledUtil scheduledUtil2; @Autowired @Qualifier("test3") private ScheduledUtil scheduledUtil3; @GetMapping public void getCron(String time,String name,Integer task) { if(task==1){ scheduledUtil1.setCron(time); scheduledUtil1.setName(name); }else if(task==2){ scheduledUtil2.setCron(time); scheduledUtil2.setName(name); }else if (task==3){ scheduledUtil3.setCron(time); scheduledUtil3.setName(name); } } }
执行日志示例: