我想为我的邮件适配器配置一个轮询器,只运行一次或以编程方式运行它。
这是一个独立的应用(java -jar xxxx.jar)
,所以我认为可能有一个选项是将fixed-rate
属性配置为任意最大值,然后退出应用程序,即:System.exit(0)
。< / p>
对于这种情况,还有更多替代方案或某种“正确方法”吗?
这是我的integration-context.xml
:
<int-mail:inbound-channel-adapter id="imapAdapter"
store-uri="imaps://${imap.user}:${imap.password}@${imap.server.ip}:${imap.server.port}/inbox"
channel="receiveChannel"
auto-startup="true"
should-delete-messages="false"
should-mark-messages-as-read="false"
java-mail-properties="javaMailProperties"
mail-filter-expression="subject matches '(?i)*UNSUSCRIBE*'">
<int:poller max-messages-per-poll="1" fixed-rate="5000"/>
</int-mail:inbound-channel-adapter>
PS:不幸的是imap-idle-channel-adapter
不是一种选择。
答案 0 :(得分:1)
我可以向你推荐OnlyOnceTrigger
:
@Bean
public Trigger onlyOnceTrigger() {
return new Trigger() {
private final AtomicBoolean invoked = new AtomicBoolean();
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
return this.invoked.getAndSet(true) ? null : new Date();
}
};
}
哪个应注入该适配器的<int:poller>
。
但是如果您说它是独立的应用程序,那么您应该关注整个应用程序的某些barrier
,并且在决定关闭应用程序之前,您确实不应该丢失进程。
其中一个好的选择是CountDownLatch
,1
计为bean。您应该在main
前System.exit(0)
等待,或者在流程结束时使用最后一个:
<outbound-channel-adapter expression="T(System).exit(0)"/>
但是如果它真的适合你只运行一次适配器并且max-messages-per-poll="1"
是非常好的选择,你应该多想想。
邮箱中可能没有邮件,因此onlyOnceTrigger
可能会在没有良好结果的情况下完成,而您的应用已陷入虚空......