Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top