码迷,mamicode.com
首页 > 编程语言 > 详细

spring quartz动态修改执行时间

时间:2019-03-03 16:15:50      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:pre   scheduler   sys   vat   res   org   数据备份   work   one   

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秒一次。

 

spring quartz动态修改执行时间

标签:pre   scheduler   sys   vat   res   org   数据备份   work   one   

原文地址:https://www.cnblogs.com/chenmz1995/p/10465908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!