Search code examples
javaspring-integration-dsl

How to catch AuthenticationFailedException that appeared in ImapMailReceiver


private IntegrationFlowRegistration getIntegrationFlowRegistration(){
  IntegrationFlow flow = IntegrationFlows
            .from(Mail.imapIdleAdapter(createImapMailReceiver(imapUrl))
                    .autoStartup(true)
            )
            .handle(System.out::println)
            .get();
    return this.flowContext.registration(flow).register();

}

I create dynamically IntegrationFlowRegistration for different customers and sometimes credentials are incorrect. I would like to catch AuthenticationFailedException and log it but can't find a way to catch it. Could you please give me a hint of how it can't be done? I read DSL documentation but didn't find a way or good example there.


Solution

  • Add an ApplicationListener<ImapIdleExceptionEvent> bean, or an @EventListener method that receives that event type.

    @EventListener
    public void imap(ImapIdleExceptionEvent event) {
    ...
    }
    

    https://docs.spring.io/spring-integration/docs/current/reference/html/mail.html#imap-idle-and-lost-connections

    Beginning with the 3.0 release, the IMAP idle adapter emits application events (specifically ImapIdleExceptionEvent instances) when exceptions occur. This allows applications to detect and act on those exceptions. You can obtain the events by using an <int-event:inbound-channel-adapter> or any ApplicationListener configured to receive an ImapIdleExceptionEvent or one of its super classes.