Q: Spring integration - imap idle receiver question I'm using spring boot 2.2.4-RELEASE and Spring integration. I'm developing a simple ImapReceiver.
What I need is the following:


*

*I can configure several Imap listeners (I use the spring integration DSL)

*I get the MimeMessage 

*I get all the information and the attachments 

*I handle these information and attachments by putting them in DB and
Alfresco


I wrote the following code
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();

The publishMailEvent method extracts all the needed information (e.g. TO, CC, BCC, mail subject, attachments) and generates a custom spring async event. 
Now if I leave autoCloseFolder(true) I can't handle the message because I get a FolderClosedException. If I set autoCloseFolder(false) alll works but I see a neverending print 
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] 

Moreover I can't figure when to close the folder. I can access to the Closeable resource, but when should I close the Folder? If I close it just after event generation, on the next message I get again FolderClosedException.
May anyone give me a tip to understand the best way to handle what I need?
Thank you
Angelo

A: 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] 

That's normal behavior for ImapIdleChannelAdapter because it is an event-driven component and is based on a scheduled task. If you would like to control a polling logic, consider to switch to regular MailReceivingMessageSource - Mail.imapInboundAdapter().
You indeed can't close a folder after each message. You should do that during some idle period when no new messages for some time and nothing to process downstream.
