spring -boot定时任务基于 quartz 实现
任务类一
package com.quartz.springboot_quartz.job; import org.quartz.*; import java.text.SimpleDateFormat; import java.util.Date; public class ScheduledJob implements Job { private String name; public void setName(String name) { this.name = name; } @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); //获取任务名称 JobKey key = jobExecutionContext.getJobDetail().getKey(); //JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap(); //String jobSays = dataMap.getString("jobSays"); System.out.println("Instance " + key + " CRON ----> schedule job1 is running ... + " + name + " ----> " + dateFormat.format(new Date())); } }
任务类二
package com.quartz.springboot_quartz.job; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import java.text.SimpleDateFormat; import java.util.Date; public class ScheduledJob2 implements Job { private String name; public void setName(String name) { this.name = name; } @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); //获取任务名称 JobKey key = jobExecutionContext.getJobDetail().getKey(); //JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap(); System.out.println("Instance " + key + " CRON ----> schedule job1 is running ... + " + name + " ----> " + dateFormat.format(new Date())); } }
开始构建
接下来构建 JobDetail,并且构建时传⼊ name 属性的值,构建 JobTrigger 和 scheduleBuilder,最后使⽤
Scheduler 启动定时任务。
package com.quartz.springboot_quartz.scheduler; import com.quartz.springboot_quartz.job.ScheduledJob; import com.quartz.springboot_quartz.job.ScheduledJob2; import org.quartz.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.quartz.SchedulerFactoryBean; import org.springframework.stereotype.Component; @Component public class CronSchedulerJob { @Autowired private SchedulerFactoryBean schedulerFactoryBean; private void scheduleJob1(Scheduler scheduler) throws SchedulerException { JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class) .withIdentity("ScheduledJob", "group1").build(); // 6的倍数秒执行 也就是 6 12 18 24 30 36 42 .... CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/6 * * * * ?"); CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1") .usingJobData("name","王智1").withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail,cronTrigger); } private void scheduleJob2(Scheduler scheduler) throws SchedulerException{ JobDetail jobDetail = JobBuilder.newJob(ScheduledJob2.class) .withIdentity("job222222", "group2").build(); // 12秒的倍数执行 12 24 36 48 60 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/12 * * * * ?"); CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "group2") .usingJobData("name","王智2").withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail,cronTrigger); } /** * * @Description 同时启动两个定时任务 * **/ public void scheduleJobs() throws SchedulerException { Scheduler scheduler = schedulerFactoryBean.getScheduler(); scheduleJob1(scheduler); scheduleJob2(scheduler); } }
执行任务
package com.quartz.springboot_quartz; import com.quartz.springboot_quartz.scheduler.CronSchedulerJob; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; /** * * @Description 项目启动即执行 * */ @Component public class MyStartupRunner implements CommandLineRunner { @Autowired public CronSchedulerJob scheduleJobs; @Override public void run(String... args) throws Exception { scheduleJobs.scheduleJobs(); System.out.println(">>>>>>>>>>>>>>>定时任务开始执行<<<<<<<<<<<<<"); } }
启动类
package com.quartz.springboot_quartz; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class SpringbootQuartzApplication { public static void main(String[] args) { SpringApplication.run(SpringbootQuartzApplication.class, args); } }
输出结果:
Instance group1.ScheduledJob CRON ----> schedule job1 is running ... + 王智1 ----> 17:35:24 Instance group2.job222222 CRON ----> schedule job1 is running ... + 王智2 ----> 17:35:24 Instance group1.ScheduledJob CRON ----> schedule job1 is running ... + 王智1 ----> 17:35:30
作 者:一支会记忆的笔
---------------------
个性 签名:真正的学习不是记住知识,而是学会如何提出问题,研究问题,解决问题。
如果觉得这篇文章对你有小小的帮助的话,记得在下方“关注”哦,博主在此感谢!

浙公网安备 33010602011771号
返回顶部