使用Spring Integration的Java邮件监听器:多个应用程序实例无法接收邮件。

问题描述 投票:0回答:1

我在一个Springboot应用程序中使用了以下代码。

@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:[email protected]/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应用被部署在两个不同的服务器上(dev和stage)。每当这个邮件ID收到新邮件时,它将被运行在dev服务器或stage上的任何一个应用程序接收,而不是两者都接收。我如何在不同服务器上运行的所有应用实例中检索新邮件?

更新 :调试日志

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%[email protected]/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 "<[email protected]>" "<[email protected]>") 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)" <[email protected]>
To: ~receiver name <[email protected]>
Subject: test message
Thread-Topic: test message
Thread-Index: AdY6s3z4MSq0ceMSRqCEtXhoAxXllAABK3Aw
Date: Thu, 4 Jun 2020 17:36:21 -0400
Message-ID:
 <[email protected]>
References:
 <[email protected]>
In-Reply-To:
 <[email protected]>
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
java spring-integration javamail spring-integration-dsl spring-dsl
1个回答
1
投票

这是因为 receiver.setShouldMarkMessagesAsRead(true);.

如果你把它改成false,我想两个都会得到它;问题是然后你将不得不以某种方式手动将其标记为seen,以防止下次应用程序启动时重新获取它。

你应该也可以使用自定义的 SearchTermStrategy. 默认策略包括

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.

ImapMailReceiver.DefaultSearchTermStrategy 以了解默认的工作方式。

© www.soinside.com 2019 - 2024. All rights reserved.