![](/img/trans.png)
[英]quartz spring integration scheduling,can we set the cron trigger dynamically
[英]spring integration + cron + quartz in cluster?
我有一個由cron表達式觸發的spring集成流程,如下所示:
<int-ftp:inbound-channel-adapter id="my-input-endpoint" ...>
<int:poller trigger="my-trigger"/>
</int-ftp:inbound-channel-adapter>
<bean id="my-trigger"
class="org.springframework.scheduling.support.CronTrigger">
<constructor-arg value="0 * * * * *" />
</bean>
它工作正常。 但是現在我必須擴展實現以使其集群就緒(在同一時間點僅在一個集群節點上執行作業)。
我希望在集群模式下使用Quartz框架(在數據庫中保持作業狀態)來觸發此集成流程。 Quartz提供了一個開箱即用的漂亮解決方案。 唯一的問題是如何將Quartz與現有的inbout-channer-adapter集成? “poller”的“trigger”屬性只接受org.springframework.scheduling.Trigger的子類。 我找不到“poller trigger”和Quartz框架之間的任何橋梁。
提前謝謝了!
這是一種方式......
將入站適配器上的自動啟動屬性設置為false。
創建一個只觸發一次的自定義觸發器,立即...
public static class FireOnceTrigger implements Trigger {
boolean done;
public Date nextExecutionTime(TriggerContext triggerContext) {
if (done) {
return null;
}
done = true;
return new Date();
}
public void reset() {
done = false;
}
}
在您的石英作業中,獲取對觸發器和SourcePollingChannelAdapter
的引用。
當石英觸發器發射時,有石英工作
來自加里的解決方案。 這是我的春天背景:
<int-ftp:inbound-channel-adapter id="my-endpoint"
auto-startup="false">
<int:poller trigger="my-endpoint-trigger"/>
</int-ftp:inbound-channel-adapter>
<bean id="my-endpoint-trigger" class="com.my.FireOnceTrigger"/>
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="my-job-trigger" />
</list>
</property>
<property name="schedulerContextAsMap">
<map>
<entry key="inputEndpoint"><ref bean="my-input-endpoint" /></entry>
<entry key="inputEndpointTrigger"><ref bean="my-endpoint-trigger" /></entry>
</map>
</property>
</bean>
<bean id="my-job-trigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="cronExpression" value="0 * * * * ?" />
<property name="jobDetail" ref="my-job" />
</bean>
<bean name="my-job" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.my.MyActivatorJob " />
</bean>
和MyActivatorJob類:
public class MyActivatorJob extends QuartzJobBean implements {
private AbstractEndpoint inputEndpoint;
private FireOnceTrigger inputEndpointTrigger;
public void setInputEndpoint(final AbstractEndpoint pInputEndpoint) {
this.inputEndpoint = pInputEndpoint;
}
public void setInputEndpointTrigger(final FireOnceTrigger pInputEndpointTrigger) {
this.inputEndpointTrigger = pInputEndpointTrigger;
}
@Override
protected void executeInternal(final JobExecutionContext pParamJobExecutionContext)
throws JobExecutionException {
inputEndpoint.stop();
inputEndpointTrigger.reset();
inputEndpoint.start();
}
}
作為下一步,必須重構這個spring上下文,以便用更靈活的東西替換schedulerContextAsMap的用法,並能夠定義更多的作業來激活和停用許多不同的端點。
謝謝加里到目前為止!
嘗試按照您的建議整合石英和彈簧,但遇到了另外兩個問題:
1.)使用Quartz 2.x和Spring 3.x時出現IncompatibleClassChangeError異常。 這是一個已知的問題,但我沒有找到任何解決方案。
2.)將其他spring bean注入Quarz作業實例。 我找到了一些解決方案,但沒有人適合我。 我試過用的那個
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobFactory">
<bean class="org.springframework.scheduling.quartz.SpringBeanJobFactory" />
</property>
<property name="triggers">
...
</property>
<property name="schedulerContextAsMap">
<map>
<entry key="inputEndpoint" value-ref="my-endpoint" />
</map>
</property>
</bean>
將其他bean注入作業但在將此屬性添加到SchedulerFactoryBean后,作業未被執行(我沒有看到任何異常)。 刪除屬性“schedulerContextAsMap”會使作業再次運行。
我沒有嘗試過,但看到Quartz 2和Spring兼容性問題似乎已在Spring 3.1.1中得到修復。 請參閱https://jira.springsource.org/browse/SPR-8889
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.