使用Spring Integration的Java邮件侦听器:多个应用程序实例未收到邮件 [英] Java mail listener using Spring Integration : mail isn't received by multiple app instances

查看:258
本文介绍了使用Spring Integration的Java邮件侦听器:多个应用程序实例未收到邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Springboot应用程序中使用以下代码:

I'm using below code in a Springboot application:

@Bean
    public IntegrationFlow mailListener() {

        return IntegrationFlows.from(Mail.imapInboundAdapter(receiver()), e -> e.poller(Pollers.fixedRate(60000).maxMessagesPerPoll(-1)))
                .<Message>handle(message -> logMail(message)).get();
    }

    private org.springframework.messaging.Message<?> logMail(org.springframework.messaging.Message<?> message) {
        System.out.println("received a mail********** !");
        // System.out.println(message.getPayload());
        // process message
        return message;
    }

    @Bean
    public ImapMailReceiver receiver() {

        ImapMailReceiver receiver = new ImapMailReceiver(
                "imaps://username:pwd@mail.company.com/INBOX");
        receiver.setShouldMarkMessagesAsRead(true);
        receiver.setJavaMailProperties(javaMailProperties());
        return receiver;
    }

    private Properties javaMailProperties() {
        Properties javaMailProperties = new Properties();

        /*
         * javaMailProperties.setProperty("mail.imap.socketFactory.class",
         * "javax.net.ssl.SSLSocketFactory");
         * javaMailProperties.setProperty("mail.imap.socketFactory.fallback","false");
         * javaMailProperties.setProperty("mail.store.protocol","imaps");
         */
        // javaMailProperties.setProperty("mail.debug","true");

        return javaMailProperties;
    }

此springboot应用程序已部署在2个不同的服务器(开发和阶段)中.每当此电子邮件ID收到新邮件时,开发服务器上或舞台上运行的任何一个应用程序都会收到该电子邮件,而不会同时接收到这两个电子邮件.如何在不同服务器上运行的应用程序的所有实例中检索新邮件?

This springboot applications has been deployed in 2 different servers (dev and stage). Whenever this email ID receives a new mail it will be received by either application that's running on dev server or stage and not both. How do I retrieve new mails in all instances of my applications running on different servers ?

更新:调试日志

DEBUG IMAPS: IMAPProtocol noop
A20 NOOP
A20 OK NOOP completed.
A21 LIST "" INBOX
* LIST (\Marked \HasNoChildren) "/" INBOX
A21 OK LIST completed.
2020-06-05 03:06:39.003 DEBUG 15568 --- [   scheduling-1] o.s.integration.mail.ImapMailReceiver    : opening folder [imaps://user.name%40company.com@mail.ad.company.com/INBOX]
DEBUG IMAPS: connection available -- size: 1
A22 SELECT INBOX
* 1438 EXISTS
* 1 RECENT
* FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
* OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
* OK [UNSEEN 228] Is the first unseen message
* OK [UIDVALIDITY 2278797] UIDVALIDITY value
* OK [UIDNEXT 2855] The next unique identifier value
A22 OK [READ-WRITE] SELECT completed.
2020-06-05 03:06:39.752  INFO 15568 --- [   scheduling-1] o.s.integration.mail.ImapMailReceiver    : attempting to receive mail from folder [INBOX]
2020-06-05 03:06:39.752 DEBUG 15568 --- [   scheduling-1] o.s.integration.mail.ImapMailReceiver    : This email server does not support RECENT or USER flags. System flag 'Flag.FLAGGED' will be used to prevent duplicates during email fetch.
A23 SEARCH NOT (ANSWERED) NOT (DELETED) NOT (SEEN) NOT (FLAGGED) ALL
* SEARCH 1438
A23 OK SEARCH completed.
2020-06-05 03:06:40.488 DEBUG 15568 --- [   scheduling-1] o.s.integration.mail.ImapMailReceiver    : found 1 new messages
A24 FETCH 1438 (ENVELOPE INTERNALDATE RFC822.SIZE FLAGS BODYSTRUCTURE)
* 1438 FETCH (ENVELOPE ("Thu, 4 Jun 2020 17:36:21 -0400" "test message" (("sender,name (Company)" NIL "Sender.name" "company.com")) NIL NIL (("~receiver name" NIL "user.name" "company.com")) NIL NIL "<KL1P101MB0152E33B47someid@KL1P101MB0152.NAMP101.PROD.OUTLOOK.COM>" "<KL1P101MB0152E33B4someid@KL1P101MB0152.NAMP101.PROD.OUTLOOK.COM>") INTERNALDATE "04-Jun-2020 17:36:28 -0400" RFC822.SIZE 2638 FLAGS (\Recent) BODYSTRUCTURE ("text" "html" ("charset" "us-ascii") NIL NIL "7BIT" 0 0 NIL NIL "en-US" NIL))
A24 OK FETCH completed.
2020-06-05 03:06:41.536 DEBUG 15568 --- [   scheduling-1] o.s.integration.mail.ImapMailReceiver    : Received 1 messages
2020-06-05 03:06:41.536 DEBUG 15568 --- [   scheduling-1] o.s.integration.mail.ImapMailReceiver    : USER flags are not supported by this mail server. Flagging message with system flag
A25 STORE 1438 +FLAGS (\Flagged)
* 1438 FETCH (FLAGS (\Flagged \Recent))
A25 OK STORE completed.
A26 FETCH 1438 (BODY.PEEK[]<0.16384>)
* 1438 FETCH (BODY[]<0> {3198}
Received: from ALPMBHT04.e2k.ad.company.com (3.159.19.197) by
 ALPURAPA30.e2k.ad.company.com (3.159.16.198) with Microsoft SMTP Server (TLS) id
 14.3.487.0; Thu, 4 Jun 2020 17:36:28 -0400
Received: from APC01-HK2-obe.outbound.protection.outlook.com (10.38.143.172)
 by mail.o365.company.com (3.159.19.197) with Microsoft SMTP Server (TLS) id
 14.3.487.0; Thu, 4 Jun 2020 17:36:28 -0400
Received: from KL1P101MB0152.NAMP101.PROD.OUTLOOK.COM (2603:1096:808:3::27) by
 KL1P101MB0151.NAMP101.PROD.OUTLOOK.COM (2603:1096:808:3::26) with Microsoft
 SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id
 15.20.3045.25; Thu, 4 Jun 2020 21:36:21 +0000
Received: from KL1P101MB0152.NAMP101.PROD.OUTLOOK.COM ([129.75.125.219]) by
 KL1P101MB0152.NAMP101.PROD.OUTLOOK.COM ([129.75.125.219]) with mapi id
 15.20.3045.029; Thu, 4 Jun 2020 21:36:21 +0000
From: "sender,name (Company)" <Sender.name@company.com>
To: ~receiver name <user.name@company.com>
Subject: test message
Thread-Topic: test message
Thread-Index: AdY6s3z4MSq0ceMSRqCEtXhoAxXllAABK3Aw
Date: Thu, 4 Jun 2020 17:36:21 -0400
Message-ID:
 <KL1P101MB0152E33B47someid@KL1P101MB0152.NAMP101.PROD.OUTLOOK.COM>
References:
 <KL1P101MB0152E33B47someid@KL1P101MB0152.NAMP101.PROD.OUTLOOK.COM>
In-Reply-To:
 <KL1P101MB0152E33B47someid@KL1P101MB0152.NAMP101.PROD.OUTLOOK.COM>
Accept-Language: en-IN, en-US
Content-Language: en-US
X-MS-Exchange-Organization-AuthAs: Internal
X-MS-Exchange-Organization-AuthMechanism: 04
X-MS-Exchange-Organization-AuthSource: KL1P101MB0152.NAMP101.PROD.OUTLOOK.COM
X-MS-Has-Attach:
X-MS-Exchange-Organization-SCL: -1
X-Message-Flag: Follow up
X-MS-TNEF-Correlator:
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0

<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
    {font-family:"Cambria Math";
    panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
    {font-family:Calibri;
    panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
    {margin:0cm;
    margin-bottom:.0001pt;
    font-size:11.0pt;
    font-family:"Calibri",sans-serif;
    mso-fareast-language:EN-US;}
.MsoChpDefault
    {mso-style-type:export-only;
    font-size:10.0pt;}
@page WordSection1
    {size:612.0pt 792.0pt;
    margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
    {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-IN" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">Test content<o:p></o:p></p>
</div>
</body>
</html>
)
A26 OK FETCH completed.
A27 EXAMINE INBOX
* 1438 EXISTS
* 0 RECENT
* FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
* OK [PERMANENTFLAGS ()] Permanent flags
* OK [UNSEEN 228] Is the first unseen message
* OK [UIDVALIDITY 2278797] UIDVALIDITY value
* OK [UIDNEXT 2856] The next unique identifier value
A27 OK [READ-ONLY] EXAMINE completed.
A28 CLOSE
A28 OK CLOSE completed.
DEBUG IMAPS: added an Authenticated connection -- size: 1
2020-06-05 03:06:44.788 DEBUG 15568 --- [   scheduling-1] o.s.i.mail.MailReceivingMessageSource    : received mail message [org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@3904c3]
2020-06-05 03:06:44.796 DEBUG 15568 --- [   scheduling-1] o.s.i.e.SourcePollingChannelAdapter      : Poll resulted in Message: GenericMessage [payload=org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@3904c3, headers={id=7c50d96e-36da-b55f-9862-2215117ef304, timestamp=1591306604796}]
2020-06-05 03:06:44.798 DEBUG 15568 --- [   scheduling-1] o.s.integration.channel.DirectChannel    : preSend on channel 'bean 'mailListener.channel#0'; defined in: 'class path resource [com//mail/MailPoller.class]'; from source: 'bean method mailListener'', message: GenericMessage [payload=org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@3904c3, headers={id=7c50d96e-36da-b55f-9862-2215117ef304, timestamp=1591306604796}]
got a new mail
2020-06-05 03:06:44.799 DEBUG 15568 --- [   scheduling-1] o.s.integration.channel.DirectChannel    : postSend (sent=true) on channel 'bean 'mailListener.channel#0'; defined in: 'class path resource [com/mail/MailPoller.class]'; from source: 'bean method mailListener'', message: GenericMessage [payload=org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@3904c3, headers={id=7c50d96e-36da-b55f-9862-2215117ef304, timestamp=1591306604796}]
DEBUG IMAPS: IMAPProtocol noop

推荐答案

是因为receiver.setShouldMarkMessagesAsRead(true);.

如果将其更改为false,我认为两者都会得到它;问题是,您将必须手动将其标记为SEEN,以防止下次启动应用程序时重新获取它.

If you change it to false, I think both will get it; the problem is then you will have to manually mark it as SEEN somehow to prevent re-fetching it next time the apps start.

您还应该能够使用自定义的SearchTermStrategy.默认策略包括

You should also be able to use a custom SearchTermStrategy. The default strategy includes

if (supportedFlags.contains(Flags.Flag.SEEN)) {
    NotTerm notSeen = new NotTerm(new FlagTerm(new Flags(Flags.Flag.SEEN), true));
    if (searchTerm == null) {
        searchTerm = notSeen;
    }
    else {
        searchTerm = new AndTerm(searchTerm, notSeen);
    }
}

使用自定义搜索字词,您还应该能够在每个实例中使用不同的userFlag;使每个消息都可以将该消息标记为已读(只要您的IMAP服务器支持Flag.USER.

With a custom search term, you should also be able to use a different userFlag in each instance; enabling each to mark the message as read that way (as long as your IMAP server supports Flag.USER.

有关默认密码的工作方式,请参见ImapMailReceiver.DefaultSearchTermStrategy.

See ImapMailReceiver.DefaultSearchTermStrategy for how the default one works.

这篇关于使用Spring Integration的Java邮件侦听器:多个应用程序实例未收到邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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