我正在使用spring integration mail来接收来自几个可配置账户的邮件信息。
我使用的是IMAP服务器和闲置配置。
我写了以下代码。
@Autowired
private IntegrationFlowContext flowContext;
IntegrationFlow flow = null;
String userFlag = confMailIn.getHost() + "_idle_adapter";
ImapIdleChannelAdapterSpec imapIdleChannelAdapterSpec = Mail.imapIdleAdapter(connectionUrl.toString())
.javaMailProperties(javaMailProperties)
.shouldDeleteMessages(deleteMessages)
.shouldMarkMessagesAsRead(markMessagesRead)
.autoStartup(true)
.autoCloseFolder(false)
.userFlag(userFlag)
.id(userFlag)
//.searchTermStrategy(this::notSeenTerm)
.selector(selectFunction);
if (confMailIn.isRichiedeAutenticazione()) {
imapIdleChannelAdapterSpec = imapIdleChannelAdapterSpec.javaMailAuthenticator(new CasellaPostaleAuthenticator(cpd.getIndirizzoMail(), cpd.getUsername(), cpd.getPassword()));
}
flow = IntegrationFlows
.from(imapIdleChannelAdapterSpec)
.handle(message ->{
//Prendo il closable del messaggio e valorizzo i l'elenco di closeale da chiudere
Closeable closeable = StaticMessageHeaderAccessor.getCloseableResource(message);
if( !closeables.containsKey(cpd.getIndirizzoMail()) ) {
closeables.put(cpd.getIndirizzoMail(), closeable);
}
publishMailEvent(message);
})
.get();
flowContext.registration(flow).id(flowId).register();
我把自动关闭文件夹设置为false,因为如果设置为true,我就不能处理邮件了,因为我收到的是 FolderClosedException
. 所以我收集了所有的 Closeable
当Spring上下文关闭时,我关闭它们(在最好的情况下......永远不会:)。
到目前为止还不错... 我注册了流程,它开始工作。但我注意到,一段时间后,它停止接收邮件信息,我需要重新启动我的服务,并再次工作一段时间。我需要重新启动我的服务,然后它又能工作一段时间。
当它工作时,我看到这些日志。
2020-04-20 16:32:57,427 25199841 [scheduling-1] DEBUG o.s.i.mail.ImapIdleChannelAdapter - waiting for mail
2020-04-20 16:32:57,460 25199874 [scheduling-1] INFO o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX]
2020-04-20 16:32:57,460 25199874 [scheduling-1] DEBUG o.s.i.mail.ImapMailReceiver - This email server does not support RECENT flag, but it does support USER flags which will be used to prevent duplicates during email fetch. This receiver instance uses flag: imapmail.libero.it_idle_adapter
2020-04-20 16:32:57,476 25199890 [scheduling-1] DEBUG o.s.i.mail.ImapMailReceiver - found 0 new messages
2020-04-20 16:32:57,476 25199890 [scheduling-1] DEBUG o.s.i.mail.ImapMailReceiver - Received 0 messages
2020-04-20 16:32:57,476 25199890 [scheduling-1] DEBUG o.s.i.mail.ImapIdleChannelAdapter - received 0 mail messages
2020-04-20 16:32:57,476 25199890 [scheduling-1] DEBUG o.s.i.mail.ImapIdleChannelAdapter - Task completed successfully. Re-scheduling it again right away.
但过了一段时间后,它就停止了书写. 我感觉这与标志选项有关,但我不知道如何解决这个问题。
任何提示都是非常欢迎的
谢谢你了
Angelo
请在这里提出一个GH问题 https:/github.comspring-projectsspring-integrationissues。.
作为一个变通办法,你可能需要遵循docs中建议的方法。https:/docs.spring.iospring-integrationdocs5.3.0.M4referencehtmlmail.html#mail-tx-sync。. 见 Mover
. 我的意思是你需要考虑到 重开 文件夹,以便能够处理邮件中的附件。