Spring + Quartz resheduling or updating the trigger?

我只是一个虾纸丫 提交于 2019-12-01 09:38:25

问题


I am using Spring 3.2 and Quartz 2.2.

My target Class and method,

public class Producer {

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

In my 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>

The above code is working and the executeListener() nethod is executing continously with the interval of 5 seconds and the starting delay will be 10 sec.

I have tried in spring controller to update the trigger as ,

@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";
    }
}

But I am getting the compile time error Trigger newTrigger = tb.withSchedule(simpleSchedule().withIntervalInSeconds(triggerInSec).build()); .

My need :

I want to reshedule the job to execute the executeListener() method with the 2 sec interval dynamically.

Thanks.


回答1:


Try one of the following:

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

or

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

Both are syntactically valid. It looks like your issue with Trigger newTrigger = tb.withSchedule(simpleSchedule().withIntervalInSeconds(triggerInSec).build()); is caused by incorrect bracketing. ie build() should not have an extra bracket after it, as it returns a trigger.



来源:https://stackoverflow.com/questions/21375317/spring-quartz-resheduling-or-updating-the-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!