Spring集成邮件轮询器 [英] Spring integration mail poller

查看:132
本文介绍了Spring集成邮件轮询器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的邮件适配器配置一个轮询器,只运行一次或以编程方式运行它。

I want to configure a poller for my mail adapter, to run just once or run it programmatically.

这是一个独立的应用程序( java -jar xxxx.jar),所以我想可能有一个选项是将固定利率属性配置为任意最大值然后退出应用程序,即: System.exit(0)

This is a standalone app (java -jar xxxx.jar), so I think maybe one option is configure the fixed-rate attribute, to an arbitrary max value and then exit the application, ie: System.exit(0).

是否有更多替代品或某种'正确' '对于这种情况?'

Are there more alternatives or some kind of 'correct approach', for this case?

这是我的 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 不是一个选项。

PS: Unfortunately imap-idle-channel-adapter is not an option.

推荐答案

我可以向你推荐 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>

但如果你说它是独立的,你应该为整个应用程序注意一些障碍在你决定关闭应用程序之前,真的不应该丢失进程。

However you should take care about some barrier for the entire application if you say that it is standalone one and you really shouldn't lose process before you decide to close the app.

其中一个不错的选择是 CountDownLatch 1 算作一个豆子。您应该在 System.exit(0)之前从 main 等待它,或者最后使用最后一个您的流程:

One of those good choice is CountDownLatch with 1 count as a bean. You should wait on it from your main before System.exit(0) or just use the last one in the end of your process:

<outbound-channel-adapter expression="T(System).exit(0)"/>

但是如果它真的适合你只运行一次适配器,你应该多考虑一下 max-messages-per-poll =1是非常好的选择。

However you should think more if it really is suitable for your to run the adapter only once and if that max-messages-per-poll="1" is really good option.

可能没有留言邮箱,所以 onlyOnceTrigger 可能会在没有好结果的情况下完成,你的应用程序已陷入虚空......

There may be no messages in the mail box, so onlyOnceTrigger may finish without good result for you and your app has strained into the void...

这篇关于Spring集成邮件轮询器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆