package com.scan.xxx.config.quartz; import lombok.extern.slf4j.Slf4j; import org.quartz.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.quartz.CronTriggerFactoryBean; import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean; import org.springframework.scheduling.quartz.SchedulerFactoryBean; @Slf4j @Configuration public class QuartzConfig { @Autowired private Scheduler scheduler; @Bean(name = "jobDetail") public MethodInvokingJobDetailFactoryBean detailFactoryBean(SchedulerTask task){ MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean(); /* * 是否并发执行 * 例如每5s执行一次任务,但是当前任务还没有执行完,就已经过了5s了, * 如果此处为true,则下一个任务会bing执行,如果此处为false,则下一个任务会等待上一个任务执行完后,再开始执行 */ jobDetail.setConcurrent(true);//设置并行 jobDetail.setName("scheduler");// 设置任务的名字 jobDetail.setGroup("scheduler_group");// 设置任务的分组,这些属性都可以存储在数据库中,在多任务的时候使用 /* * 这两行代码表示执行task对象中的scheduleTest方法。定时执行的逻辑都在scheduleTest。 */ jobDetail.setTargetObject(task); jobDetail.setTargetMethod("start"); return jobDetail; } @Bean(name = "jobTrigger") public CronTriggerFactoryBean cronJobTrigger(MethodInvokingJobDetailFactoryBean jobDetail) { CronTriggerFactoryBean tigger = new CronTriggerFactoryBean(); tigger.setJobDetail(jobDetail.getObject()); tigger.setCronExpression("0 0 12 * * ?");// 表示每天中午12点 //tigger.set tigger.setName("myTigger");// trigger的name return tigger; } @Bean(name = "scheduler") public SchedulerFactoryBean schedulerFactory(Trigger cronJobTrigger) { SchedulerFactoryBean schedulerFactoryBean1 = new SchedulerFactoryBean(); //设置是否任意一个已定义的Job会覆盖现在的Job。默认为false,即已定义的Job不会覆盖现有的Job。 schedulerFactoryBean1.setOverwriteExistingJobs(true); // 延时启动,应用启动5秒后 ,定时器才开始启动 schedulerFactoryBean1.setStartupDelay(5); // 注册定时触发器 schedulerFactoryBean1.setTriggers(cronJobTrigger); return schedulerFactoryBean1; } //多任务时的Scheduler,动态设置Trigger。一个SchedulerFactoryBean可能会有多个Trigger @Bean(name = "multitaskScheduler") public SchedulerFactoryBean schedulerFactoryBean(){ SchedulerFactoryBean bean = new SchedulerFactoryBean(); return bean; } /** * 根据任务名任务组删除 */ }
配置类
package com.scan.xxx.controller.quartz; import com.scan.xxx.config.quartz.QuartzConfig; import com.scan.xxx.config.quartz.Task1; import com.scan.xxx.config.quartz.Task2; import lombok.extern.slf4j.Slf4j; import org.quartz.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; @Slf4j @Controller public class QuartzController2 { @Resource(name = "multitaskScheduler") private Scheduler scheduler; @Autowired private QuartzConfig quartzConfig; JobDetail jobDetail; @ResponseBody @RequestMapping("/task1") public Object task1() throws SchedulerException { //配置定时任务对应的Job,这里执行的是ScheduledJob类中定时的方法 jobDetail = JobBuilder .newJob(Task1.class) .withIdentity("job1", "group1") .build(); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/11 * * * * ?"); // 每3s执行一次 CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail, cronTrigger); return "任务1"; } @ResponseBody @RequestMapping("/task2") public Object task2() throws SchedulerException { //配置定时任务对应的Job,这里执行的是ScheduledJob类中定时的方法 jobDetail = JobBuilder.newJob(Task2.class).withIdentity("job2", "group1").build(); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/6 * * * * ?"); // 每3s执行一次 CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "group1").withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail, cronTrigger); return "任务2"; } @ResponseBody @RequestMapping("/taskDelete") public void taskDelete(String taskCode, String taskGroup) throws SchedulerException { //配置定时任务对应的Job,这里执行的是ScheduledJob类中定时的方法 delete(taskCode, taskGroup); } public void delete(String taskCode, String taskGroup) throws SchedulerException { JobKey jobKey = new JobKey(taskCode, taskGroup); log.info(jobKey.getName()); JobDetail jobDetail = scheduler.getJobDetail(jobKey); if (jobDetail == null) { log.info("不存在job"); return; } log.info("删除" + jobKey.toString()); scheduler.deleteJob(jobKey); } }
实现类
标签:quartz,springframework,public,并发,import,org,多任务,jobDetail From: https://www.cnblogs.com/caoaman/p/17332986.html