quartz
设置参数, 获取参数
在job中使用spring注入的service对象
循环获取所有的job
删除job
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
maven 中添加依赖
org.quartz-scheduler quartz 2.2.1 org.quartz-scheduler quartz-jobs 2.2.1
quartz 的 API 设计使用 domain-specific language (DSL), 写起来十分的流畅, 可读性高。 写之前先导入类方便IDE联想:
import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.impl.StdSchedulerFactory; import static org.quartz.JobBuilder.*; import static org.quartz.TriggerBuilder.*; import static org.quartz.SimpleScheduleBuilder.*;
使用:
@Test public void test4() throws SchedulerException { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); JobDetail job1 = newJob(JobTest1.class) // 第一个参数name: job名称,job的唯一标志如果一个job未执行完继续使用同名的jobName会报错, // 第二个参数group:分组名称,用于逻辑处理 .withIdentity("test1", "test1Group").build(); Trigger trigger1 = newTrigger() .withIdentity("test1", "test1Group") .usingJobData("name", "scott") //设置参数,在JobTest1类中取用 .usingJobData("gender","male") .startNow() // 立即执行 .withSchedule(simpleSchedule() //通过simpleSchedule定时, 有很多种类,比如cron等等 .withIntervalInSeconds(60) //每60秒执行一次, 单位可设置为分,时,秒,毫秒 .withRepeatCount(2)) // 执行次数设置为2次,应为立即执行了一次, 所以实际执行此时是withRepeatCount+1次 .build(); JobDataMap jobDataMap = new JobDataMap(); jobDataMap.put("name", "scott"); JobDetail job2 = newJob(JobTest2.class) .withIdentity("test2", "test2Group") .usingJobData(jobDataMap) .build(); Trigger trigger2 = newTrigger() .withIdentity("test2", "test2Group") .startNow() .withSchedule(simpleSchedule() .withIntervalInSeconds(20) .repeatForever()) // 奔流到海不复回 .build(); scheduler.scheduleJob(job1, trigger1); scheduler.scheduleJob(job2, trigger2); scheduler.start(); }
JobTest1代码
package com.quartz;import org.quartz.*;public class JobTest1 implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { JobDataMap jobDataMap = context.getMergedJobDataMap(); String name = jobDataMap.getString("name"); //获取参数 int gender = jobDataMap.getIntValue("gender"); System.out.println(name+gender); }}
JobTest2 的代码:
package com.quartz;import com.service.device.DeviceService;import org.quartz.*;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.context.support.SpringBeanAutowiringSupport;@PersistJobDataAfterExecution@DisallowConcurrentExecutionpublic class JobTest2 implements Job { @Autowired private DeviceService deviceService; @Override public void execute(JobExecutionContext context) { SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); //在非spring托管的类中使用spring的对象 Integer result = deviceService.selectIdBydeviceCode("154653468"); //调用service层方法向数据库中查询数据 System.out.println(result); JobDataMap jobDataMap = context.getMergedJobDataMap(); String name = jobDataMap.getString("name"); System.out.println(name); //获取设置的参数 SimpleTrigger trigger = (SimpleTrigger) context.getTrigger(); int times = trigger.getTimesTriggered(); //获取当前执行次数 System.out.println("我是job2第"+times+"次运行"); }}
循环所有的job, 删除指定job
@Test public void testQuartz1() { try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); for (String groupName : scheduler.getJobGroupNames()) { for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) { String jobName = jobKey.getName(); String jobGroup = jobKey.getGroup(); Listtriggers = (List ) scheduler.getTriggersOfJob(jobKey); Date nextFireTime = triggers.get(0).getNextFireTime(); System.out.println(jobName + "第"+times+"次运行, 存在" + jobGroup + "组,距离下次到期时间" + nextFireTime); if(jobName.equalsIgnoreCase("test2")) //删除job scheduler.deleteJob(jobKey); } } } catch (Exception e) { e.printStackTrace(); } }