quartzスケジューリング構成
2905 ワード
<bean id="incrementalJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" p:targetObject-ref="incrementalService" p:targetMethod="job" p:concurrent="false" />
<bean id="checkImagesTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean" p:jobDetail-ref="incrementalJob" p:cronExpression="0 0 22 * * ?" />
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="checkImagesTrigger" />
</list>
</property>
<property name="schedulerContextAsMap">
<map>
<entry key="timeout" value="30" />
</map>
</property>
<property name="quartzProperties">
<props>
<prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
<prop key="org.quartz.threadPool.threadCount">10</prop>
</props>
</property>
</bean>
スケジュール時間の動的変更
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
/**
*
*/
CronTriggerBean ct = (CronTriggerBean) webApplicationContext.getBean("myTrigger");
CronTriggerBean ct2 = (CronTriggerBean) webApplicationContext.getBean("myStopTrigger");
Scheduler scheduler = (Scheduler) webApplicationContext.getBean("scheduler");
ct.setCronExpression("5 * * * * ?");
ct2.setCronExpression("35 * * * * ?");
scheduler.rescheduleJob(ct.getName(), ct.getGroup(), ct);
scheduler.rescheduleJob(ct2.getName(), ct2.getGroup(), ct2);
初期化時に他の場所のスケジュール時間を読み込む
public class MyCronTriggerBean extends CronTriggerBean {
public void initStartCronExpression() {
try {
setCronExpression("15 * * * * ?");
} catch (ParseException e) {
e.printStackTrace();
}
}
public void initStopCronExpression() {
try {
setCronExpression("15 * * * * ?");
} catch (ParseException e) {
e.printStackTrace();
}
}
}
元のXMLプロファイルを変更すればよい
<bean id="myTrigger" class="com.siyue.gxwj.timer.MyCronTriggerBean" p:jobDetail-ref="myJob" p:cronExpression="0 * * * * ?" init-method="initStartCronExpression" />