Spring 集成 - imap 空闲接收器问题

Spring integration - imap idle receiver question

我正在使用 spring boot 2.2.4-RELEASE 和 Spring 集成。我正在开发一个简单的 ImapReceiver。

我需要的是:

我写了下面的代码

ImapIdleChannelAdapterSpec imapIdleChannelAdapterSpec = Mail.imapIdleAdapter(connectionUrl.toString())
                    .javaMailProperties(javaMailProperties)
                    .shouldDeleteMessages(deleteMessages)
                    .shouldMarkMessagesAsRead(markMessagesRead)
                    .autoStartup(true)
                    .autoCloseFolder(true)
                    .id(confMailIn.getHost() + "_adapter")
                    .selector(selectFunction);

            if (confMailIn.isRichiedeAutenticazione()) {
                imapIdleChannelAdapterSpec = imapIdleChannelAdapterSpec.javaMailAuthenticator(new CasellaPostaleAuthenticator(cpd.getIndirizzoMail(), cpd.getUsername(), cpd.getPassword()));
            }
            flow = IntegrationFlows
                    .from(imapIdleChannelAdapterSpec)
                    .handle(message ->{

                        publishMailEvent(message);
                    })
                    .get();

publishMailEvent 方法提取所有需要的信息(例如 TO、CC、BCC、邮件主题、附件)并生成自定义 spring 异步事件。

现在,如果我离开 autoCloseFolder(true),我将无法处理该消息,因为我收到 FolderClosedException。如果我设置 autoCloseFolder(false) 一切正常,但我看到一个永无休止的打印

2020-03-06 10:24:37,580 141381 [asyncTaskExecutor-18] DEBUG i.e.t.r.p.w.b.h.MailMessageHandler - Header name Message-ID header value <opec292.20200306102349.32719.160.1.254.63@pec.aruba.it> 
2020-03-06 10:24:37,582 141383 [asyncTaskExecutor-12] TRACE i.e.t.r.p.w.b.h.MailMessageHandler - Aggiungo allegato smime.p7s all'elenco di allegati 
2020-03-06 10:24:37,583 141384 [asyncTaskExecutor-18] TRACE i.e.t.r.p.w.b.h.MailMessageHandler - Aggiungo allegato smime.p7s all'elenco di allegati 
2020-03-06 10:24:37,583 141384 [asyncTaskExecutor-15] TRACE i.e.t.r.p.w.b.h.MailMessageHandler - Aggiungo allegato smime.p7s all'elenco di allegati 
2020-03-06 10:24:37,607 141408 [asyncTaskExecutor-18] TRACE i.e.t.r.p.w.b.s.i.GestioneMailSvcImpl - Mail out con id messaggio <opec292.20200306102349.32719.160.1.254.63@pec.aruba.it> esistente ? false 
2020-03-06 10:24:37,607 141408 [asyncTaskExecutor-12] TRACE i.e.t.r.p.w.b.s.i.GestioneMailSvcImpl - Mail out con id messaggio <opec292.20200306102349.32719.160.1.63@pec.aruba.it> esistente ? false 
2020-03-06 10:24:37,607 141408 [asyncTaskExecutor-15] TRACE i.e.t.r.p.w.b.s.i.GestioneMailSvcImpl - Mail out con id messaggio <opec292.20200306102350.04933.324.2.326.71@pec.aruba.it> esistente ? false 
2020-03-06 17:19:33,850 140094 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:33,938 140182 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:34,021 140265 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:34,107 140351 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:34,190 140434 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:34,276 140520 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:34,362 140606 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:34,453 140697 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:34,537 140781 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:34,621 140865 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:34,703 140947 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:34,788 141032 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:34,874 141118 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:34,960 141204 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:35,051 141295 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:35,133 141377 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:35,216 141460 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:35,301 141545 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:35,386 141630 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:35,468 141712 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:35,553 141797 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:35,635 141879 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:35,718 141962 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:35,801 142045 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:35,885 142129 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:35,976 142220 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:36,062 142306 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:36,145 142389 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:36,233 142477 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:36,324 142568 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:36,410 142654 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:36,493 142737 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:36,574 142818 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:36,656 142900 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:36,743 142987 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:36,831 143075 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:36,918 143162 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:37,000 143244 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:37,083 143327 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:37,165 143409 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:37,247 143491 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:37,333 143577 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:37,431 143675 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:37,521 143765 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:37,609 143853 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:37,699 143943 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:37,806 144050 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:37,897 144141 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:37,993 144237 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:38,079 144323 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:38,181 144425 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:38,263 144507 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:38,346 144590 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 

而且我不知道什么时候关闭文件夹。我可以访问 Closeable 资源,但我应该什么时候关闭文件夹?如果我在事件生成后立即关闭它,在下一条消息中我会再次收到 FolderClosedException。

任何人都可以给我一个提示,让我了解处理我需要的最好方法吗?

谢谢

安杰洛

2020-03-06 17:19:33,938 140182 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 
2020-03-06 17:19:34,021 140265 [scheduling-1] INFO  o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX] 

这是 ImapIdleChannelAdapter 的正常行为,因为它是一个事件驱动组件并且基于计划任务。如果您想控制轮询逻辑,请考虑切换到常规 MailReceivingMessageSource - Mail.imapInboundAdapter().

您确实无法在每封邮件后关闭文件夹。您应该在一段时间内没有新消息并且没有下游处理的空闲期间执行此操作。