spring quartz动态修改执行时间
1.获取schedule
<bean name="startQuartz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="runJobTrigger" />
</list>
</property>
</bean>
@Resource(name="startQuartz") private StdScheduler scheduler;
2.修改trigger
<!--es数据备份定时任务--> <bean id="runJobTrigger11" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="name"> <value>name</value> </property> <property name="group"> <value>group</value> </property> <property name="jobDetail"> <ref bean="runJobDetail11" /> </property> <property name="cronExpression"> <value>0/2 * * ? * * *</value><!-- 每2秒一次 --> </property> </bean> <bean id="runJobDetail11" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!--这边自己设置>
</bean>
TriggerKey triggerKey = TriggerKey.triggerKey("name", "group"); TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger(); triggerBuilder.withIdentity("esBackupTrigger", "es"); triggerBuilder.startNow(); triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule("0/"+day+" * * ? * * *")); System.out.println("0/"+5+" * * ? * * *"); CronTrigger trigger = (CronTrigger) triggerBuilder.build(); scheduler.rescheduleJob(triggerKey, trigger);
然后就修改了执行的时间,改成了5秒一次。