此例子实现的功能为是由N个不同启动时间Trigger来触发同一个JobDetail
缺少的日期工具类没有贴出来,自己改一下就行,这个例子我测试过了 没有问题
测试方法
//注入 private SchedulerManager schedulerManager; //添加计划 schedulerManager.addScheduleJob(DateTool.parse(buScheduler.getStart_date(), "yyyy-MM-dd HH:mm:ss"),"235235235235");
quartz 的spring 配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="schedulerManager" class="com.success.modules.scheduler.quartz.SchedulerManager"> <property name="scheduler" ref="schedulerFactory"/> <property name="simpleTrigger" ref="simpleTrigger" /> </bean> <bean id="sendMessageJob" class="com.success.modules.scheduler.quartz.SendMessageJob"> </bean> <bean name="sendMessageJobDetailsBean" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.success.modules.scheduler.quartz.SendMessageJob" /> </bean> <!-- 不需要继承、实现任何类的JobDetail <bean id="schedulerJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="sendMessageJob" /> <property name="targetMethod" value="sendMes" /> <property name="concurrent" value="false" /> </bean> --> <bean id="simpleTrigger" class="com.success.modules.scheduler.quartz.InitializingSimpleTrigger"> <property name="jobDetail" ref="sendMessageJobDetailsBean" /><!-- <property name="cronExpression"> <value>0/10 * * * * ?</value> </property> --> </bean> <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="simpleTrigger" /> </list> </property> </bean> </beans>
package com.success.modules.scheduler.quartz; import java.io.Serializable; import java.util.Calendar; import org.springframework.scheduling.quartz.SimpleTriggerBean; @SuppressWarnings("serial") /** * 设置初始默认的触发器 * 为满足spring 初始时必须至少有一个Trigger固设一个一时半会执行不到的时间段 */ public class InitializingSimpleTrigger extends SimpleTriggerBean implements Serializable{ private InitializingSimpleTrigger() { setName("simpleTrigger"); setRepeatCount(0); setStartDelay(0); setRepeatInterval(0); Calendar calendar = Calendar.getInstance(); // 在2100年1月1日 0点0分0秒 执行 calendar.set(2100, 0, 1, 0, 0, 0); setStartTime(calendar.getTime()); } }
package com.success.modules.scheduler.quartz; import java.util.Date; import org.quartz.JobDataMap; import org.quartz.Scheduler; import org.quartz.SchedulerException; import com.success.platform.util.DateTool; public class SchedulerManager { private Scheduler scheduler; private InitializingSimpleTrigger simpleTrigger; /** * * @Description: 添加计划 * @param @param startTime 计划开始执行的时间 * @param @param triggerName 全局唯一的触发器名称 * @throws SchedulerException */ public void addScheduleJob(Date startTime,String triggerName) throws SchedulerException { if(DateTool.getNow().compareTo(startTime)==-1){ JobDataMap jdm=new JobDataMap(); jdm.put("date", startTime); simpleTrigger.setJobDataMap(jdm); simpleTrigger.setName("trigger_"+triggerName); simpleTrigger.setStartTime(startTime); scheduler.scheduleJob(simpleTrigger); } } public Scheduler getScheduler() { return scheduler; } public void setScheduler(Scheduler scheduler) { this.scheduler = scheduler; } public InitializingSimpleTrigger getSimpleTrigger() { return simpleTrigger; } public void setSimpleTrigger(InitializingSimpleTrigger simpleTrigger) { this.simpleTrigger = simpleTrigger; } }
package com.success.modules.scheduler.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import com.success.platform.util.DateTool; public class SendMessageJob implements Job { @Override public void execute(JobExecutionContext paramJobExecutionContext) throws JobExecutionException { String mes="计划在:"+paramJobExecutionContext.getMergedJobDataMap().get("date"); mes+="实际在:"+DateTool.getDateTime(); mes+="触发器名:"+paramJobExecutionContext.getTrigger().getName(); System.out.println(mes); } }