spring quartz 定时器动态设置执行时间

1、定时器配置文件内容
	<bean id="testServiceImpl"
		class="com.biz.impl.TestServiceImpl"
		singleton="true">
		<property name="cacheService" ref="exchangeCacheService" />
	</bean>
<bean id="schedulerJobDetail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="testServiceImpl" />
		<property name="targetMethod" value="run" />
		<property name="concurrent" value="false" />
	</bean>
	<bean id="cronTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="schedulerJobDetail" />
		<property name="cronExpression">
			<value>0 0/1 * * * ?</value>
		</property>
	</bean>
	<bean id="schedulerFactory"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref local="cronTrigger" />
			</list>
		</property>
	</bean>



<bean id="testService"
		class="com.biz.impl.Test1ServiceImpl">
		<property name="scheduler" ref="schedulerFactory"/> 
	</bean>


2、修改定时器时间间隔类中的代码

public void updateNotificationInterval() throws SchedulerException,
			ParseException {

		// 运行时可通过动态注入的scheduler得到trigger
		CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger(
				"cronTrigger", Scheduler.DEFAULT_GROUP);

		String originConExpression = trigger.getCronExpression();
		// 把定时器的执行时间间隔改为每2分钟执行一次
		trigger.setCronExpression("0 0/2 * * * ?");
		scheduler.rescheduleJob("cronTrigger", Scheduler.DEFAULT_GROUP, trigger);

	}


你可能感兴趣的:(spring,quartz,定时器)