1,springboot自带的定时任务 默认是单线程
有这个依赖就可以
org.springframework.boot spring-boot-starter
在启动类加注解 @EnableScheduling
然后在方法上 @Scheduled(cron="*/5 * * * * ?") 或者 @Scheduled(fixedRate = 5000) 就可以了 很简单
2,把springboot自带配成多线程
加一个类
@Configuration @EnableScheduling //开启定时器 public class SchedulerConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { //多线程配置 scheduledTaskRegistrar.setScheduler(taskExecutor()); } @Bean public Executor taskExecutor() { return Executors.newScheduledThreadPool(100); } } 再用@Scheduled 起任务时 已经是多线程了 3,springboot集成quartz 加依赖
写一个任务 继承Job就可以 自己的业务写在这 org.quartz-scheduler quartz 2.2.1
public class QuartzJobDemo implements Job { private static final Logger logger = LoggerFactory.getLogger(QuartzJobDemo.class); @Override public void execute(JobExecutionContext jobExecutionContext){ Thread current = Thread.currentThread(); logger.info("QuartzJobDemo.execute 定时任务Quartz:"+current.getId()+ ",name:"+current.getName()); } } 搞一个Manager管理一下任务startJobDemo方法就是怎么用quartz执行任务
@Component public class QuartzManager { private static final Logger logger = LoggerFactory.getLogger(QuartzManager.class); @Autowired private Scheduler scheduler; public void startJob() throws SchedulerException{ startJobDemo(scheduler); } private void startJobDemo(Scheduler scheduler) throws SchedulerException { // 通过JobBuilder构建JobDetail实例,JobDetail规定只能是实现Job接口的实例 // JobDetail 是具体Job实例 JobDetail jobDetail = JobBuilder.newJob(QuartzJobDemo.class).withIdentity("job1", "group1").build(); // 基于表达式构建触发器 CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?"); // CronTrigger表达式触发器 继承于Trigger // TriggerBuilder 用于构建触发器实例 CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job1", "group1") .withSchedule(cronScheduleBuilder).build(); scheduler.scheduleJob(jobDetail, cronTrigger); scheduler.start(); } public void removeJob(String jobName, String groupName) { TriggerKey triggerKey = TriggerKey.triggerKey(jobName, groupName); try { if (checkExists(jobName, groupName)) { scheduler.pauseTrigger(triggerKey); scheduler.unscheduleJob(triggerKey); } } catch (Exception e) { throw new RuntimeException(e); } } public boolean checkExists(String jobName, String groupName) throws SchedulerException { TriggerKey triggerKey = TriggerKey.triggerKey(jobName, groupName); return scheduler.checkExists(triggerKey); } public void updateJob(String jobName, String groupName, String time){ TriggerKey triggerKey = TriggerKey.triggerKey(jobName, groupName); try{ int fixedRate = Integer.valueOf(time)*1000; SimpleTriggerImpl trigger = (SimpleTriggerImpl)scheduler.getTrigger(triggerKey); if(trigger.getRepeatInterval() != fixedRate){ SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.repeatSecondlyForever(fixedRate); Trigger newTrigger = TriggerBuilder.newTrigger().withIdentity(jobName, groupName) .withSchedule(scheduleBuilder).build(); removeJob(jobName, groupName); scheduler.rescheduleJob(triggerKey, newTrigger); } }catch (Exception e){ throw new RuntimeException(e); } } } 可以用spring调用任务启动 或者用其他方式启动
@Configuration @Component public class StartQuartzJobListener implements ApplicationListener{ private static final Logger logger = LoggerFactory.getLogger(StartQuartzJobListener.class); @Autowired private QuartzManager quartzManager; /** * 初始启动quartz */ @Override public void onApplicationEvent(ContextRefreshedEvent event) { try { quartzManager.startJob(); logger.info("定时任务已经启动..."); } catch (SchedulerException e) { e.printStackTrace(); } } /** * 初始注入scheduler * @return * @throws SchedulerException */ @Bean public Scheduler scheduler() throws SchedulerException{ SchedulerFactory schedulerFactoryBean = new StdSchedulerFactory(); return schedulerFactoryBean.getScheduler(); } }