编程语言
首页 > 编程语言> > java – Spring Quartz重新安排或更新触发器?

java – Spring Quartz重新安排或更新触发器?

作者:互联网

我使用的是Spring 3.2和Quartz 2.2.

我的目标类和方法,

public class Producer {

    public void executeListener() {
        System.out.println(" Test Method . ");
    }
}

在我的spring applicationContext.xml中,

<bean id="producer" class="com.service.Producer" />

<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="producer" />
  <property name="targetMethod" value="executeListener" />
  <property name="concurrent" value="false" />
</bean>

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
    <property name="jobDetail" ref="jobDetail" />
    <!-- 10 seconds -->
    <property name="startDelay" value="10000" />
    <!-- repeat every 5 seconds -->
    <property name="repeatInterval" value="5000" />
</bean>


<bean id="mySheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
    <list>
        <!-- <ref bean="cronTrigger" /> -->
        <ref bean="simpleTrigger" />
    </list>
    </property>
</bean>

上面的代码正常工作,executeListener()nethod连续执行,间隔为5秒,启动延迟为10秒.

我试过在spring controller中更新触发器,

@Controller
public class SpringController {

    @Autowired
    org.springframework.scheduling.quartz.SchedulerFactoryBean myScheduler;

    @Autowired
    org.springframework.scheduling.quartz.SimpleTriggerFactoryBean oldTrigger;

    @RequestMapping(value="/reSheduleTrigger.html",method=RequestMethod.POST)
    public @ResponseBody String reSheduleTrigger(@RequestParam(value="triggerInSec") String triggerInSec ){

        System.out.println("----------------- Changing the repeat interval request -----------");

        // obtain a builder that would produce the trigger
        TriggerBuilder<SimpleTrigger> tb = oldTrigger.getObject().getTriggerBuilder();   

        Trigger newTrigger = tb.withSchedule(simpleSchedule().withIntervalInSeconds(triggerInSec).build());

        myScheduler.getObject().rescheduleJob(oldTrigger.getObject().getKey(),newTrigger  );

        return "success";
    }
}

但是我得到编译时错误Trigger newTrigger = tb.withSchedule(simpleSchedule().withIntervalInSeconds(triggerInSec).build()); .

我的需要:

我想重新编写作业,以动态的2秒间隔执行executeListener()方法.

谢谢.

解决方法:

请尝试以下方法之一:

Trigger newTrigger = tb.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2)).build();

要么

Trigger newTrigger = tb.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(2)).build();

两者在语法上都是有效的.看起来你的问题是Trigger newTrigger = tb.withSchedule(simpleSchedule().withIntervalInSeconds(triggerInSec).build());是由不正确的包围引起的.即build()之后不应该有一个额外的括号,因为它返回一个触发器.

标签:java,spring,spring-mvc,quartz-scheduler,job-scheduling
来源: https://codeday.me/bug/20191009/1877177.html