Spring + Quartz重新安排或更新触发器? [英] Spring + Quartz resheduling or updating the trigger?
问题描述
我正在使用 Spring 3.2
和 Quartz 2.2
。
我的目标类
和方法
,
public class Producer {
public void executeListener() {
System.out.println(" Test Method . ");
}
}
在我的春天 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秒
。
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
.
我已尝试在 spring controller
中更新触发器,
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";
}
}
但我收到编译时错误触发newTrigger = tb.withSchedule(simpleSchedule()。withIntervalInSeconds(triggerInSec).build());
。
我需要:
我想重新计算
执行的工作executeListener()
动态 2秒
区间的方法。
I want to reshedule
the job to execute the executeListener()
method with the 2 sec
interval dynamically.
谢谢。
推荐答案
请尝试以下方法之一:
Trigger newTrigger = tb.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2)).build();
或
Trigger newTrigger = tb.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(2)).build();
两者在语法上都有效。看起来你的问题是触发newTrigger = tb.withSchedule(simpleSchedule()。withIntervalInSeconds(triggerInSec).build());
是由不正确的包围引起的。即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.
这篇关于Spring + Quartz重新安排或更新触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!