【问题标题】:spring integration to read emails with multiple instances of spring boot applicationspring 集成以使用多个 spring boot 应用程序实例读取电子邮件
【发布时间】:2022-12-14 10:40:28
【问题描述】:

我使用以下配置运行一个 spring boot 应用程序来读取来自给定电子邮件帐户的电子邮件。然而,这个 springboot 应用程序是容器化的,因此我们在给定时间运行了这个应用程序的多个实例。

我的问题是,多个消费者[应用程序实例]是否会从电子邮件帐户获取相同的消息进行处理,或者如果消费者中的任何人阅读它,其他消费者[应用程序实例]将无法使用它?我试图避免重复处理消息。

配置 xml -


<int-mail:inbound-channel-adapter id="imapAdapter"
                                      store-uri="imaps://abc.com/INBOX"
                                      channel="receiveChannel"
                                      should-delete-messages="false"
                                      should-mark-messages-as-read="true"
                                      java-mail-properties="javaMailProperties"
                                      auto-startup="true">
        <int:poller max-messages-per-poll="1" fixed-rate="600000" />

    </int-mail:inbound-channel-adapter>

    <util:properties id="javaMailProperties">
        <prop key="mail.imap.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
        <prop key="mail.imap.socketFactory.fallback">false</prop>
        <prop key="mail.store.protocol">imaps</prop>
        <prop key="mail.debug">false</prop>
        <prop key="mail.smtp.ssl.protocols">TLSv1.2</prop>
    </util:properties>

    <bean id="mailService" class="com.xpressbees.poller.EmailPoller"/>

    <int:service-activator id="serviceActivator" input-channel="receiveChannel" ref="mailService" method="handleMail"/>


【问题讨论】:

    标签: java spring-boot containers spring-integration


    【解决方案1】:

    should-mark-messages-as-read="true" 足以让 IMAP 邮件接收者跳过其他实例上的这些邮件或后续获取:NotTerm notSeen = new NotTerm(new FlagTerm(new Flags(Flags.Flag.SEEN), true));

    你观察到其他一些行为吗?

    更新

    您还可以使用 Spring Integration 中的领导者选举功能,其中只有一个活动实例将拉取邮箱:https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#leadership-event-handling。另一个功能是使用幂等接收器建议来防止重复:https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#idempotent-receiver

    【讨论】:

    • 是的,我认为这也是一个好方法,当消费者在某些情况下阅读电子邮件时,所有其他实例都会更新电子邮件标志,因为它们实现了观察者模式,仅此而已;)
    • 谢谢 Artem,我没有看到任何关于这种行为的文档,所以想在这里问问。如果我发现不同的行为,我会更新这张票。
    • 将消息标记为 read = "true" 不会阻止多节点场景中的重复消息处理,因此我们使用带有调度程序的 spring shedlock 仅处理一次。
    • 您还可以使用 Spring Integration 的领导者选举功能,其中只有一个活动实例将拉取邮箱:docs.spring.io/spring-integration/docs/current/reference/html/…。另一个功能是使用幂等接收器建议来防止重复:docs.spring.io/spring-integration/docs/current/reference/html/…
    • 嗨@ArtemBilan,你能用你最后的评论编辑上面的答案吗?然后我可以接受它作为最终答案。
    猜你喜欢
    • 2019-03-04
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 2018-05-31
    • 1970-01-01
    相关资源
    最近更新 更多