我正在用这些方法启动/停止工作:
ClassPathXmlApplicationContext scheduleContext;
public void startJobs() {
scheduleContext = new ClassPathXmlApplicationContext("applicationContext.xml");
}
public void stopJobs() {
scheduleContext.close();
}
的applicationContext.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd">
<bean name="quartzJob"
class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="mypackage.TestSpringJob" />
<property name="durability" value="true" />
</bean>
<bean id="simpleTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="quartzJob" />
<property name="repeatInterval" value="2000" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="quartzJob" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>
</beans>
例如,是否可以动态更改作业的repeatInterval
属性或向调度程序添加新作业?
如果是这样,我怎么能实现这个目标?
答案 0 :(得分:1)
您可以动态定义作业和触发器,并动态安排作业。
// Create Job
JobDetailFactoryBean JobDetail = new JobDetailFactoryBean();
jobDetail.setJobClass(ScheduledJob.class);
jobDetail.setName("reportJob1");
jobDetail.setDurability(true);
jobDetail.setJobDataMap(new JobDataMap());
jobDetail.afterPropertiesSet();
// create CRON Trigger
CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
trigger.setBeanName("UniqueCronBeanName");
trigger.setGroup("uniqueCronGroup");
trigger.setJobDetail(jobDetail.getObject());
trigger.setCronExpression("0 0 12 * * ?");
trigger.afterPropertiesSet();
然后,您可以通过将其设置为已定义的调度程序工厂来安排作业。
schedulerFactoryBean.getScheduler().scheduleJob(jobDetail.getObject(), trigger.getObject());
要更新调度程序重复间隔或cron表达式,可以按如下方式实现。
List<? extends Trigger> triggerList =
schedulerFactoryBean.getScheduler().getTriggersOfJob(new JobKey(jobName, jobGroup));
// get the trigger from Builder
TriggerBuilder tb = triggerList.get(0).getTriggerBuilder();
Trigger newTrigger =
tb.withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(10)).build();
Date updatedTriggerDate =
schedulerFactoryBean.getScheduler().rescheduleJob(triggerList.get(0).getKey(), newTrigger);