'Apache Camel: Catch the authentication failed error

I have a route to monitor the email by Apache Camel. If the username or password were changed. I want to show a message to the user in monitoring system about this changes - like authentication failed ...

Currently I am not able to catch the error in the route. I do not know what I am doing wrong. With the code below I never go into the doCatch block.

How can I catch the authentication failed error in the doCatch block?

Error:

The following error is being thrown in the console

> 2022-04-18 11:01:01 [Camel (camel-1) thread #6 - imaps://imap.web.de]
> WARN  o.a.c.component.mail.MailConsumer - Consumer
> Consumer[imaps://imap.web.de?closeFolder=false&disconnect=false&password=xxxxxx&subject=FK100+-+daily&unseen=true&username=XXXXX]
> failed polling endpoint:
> imaps://imap.web.de?closeFolder=false&disconnect=false&password=xxxxxx&subject=FK100+-+daily&unseen=true&username=XXXXX.
> Will try again at next poll. Caused by:
> [javax.mail.AuthenticationFailedException - authentication failed]
> javax.mail.AuthenticationFailedException: authentication failed   at
> com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:708)   at
> javax.mail.Service.connect(Service.java:364)  at
> org.apache.camel.component.mail.MailConsumer.ensureIsConnected(MailConsumer.java:603)
>   at
> org.apache.camel.component.mail.MailConsumer.poll(MailConsumer.java:128)
>   at
> org.apache.camel.support.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:202)
>   at
> org.apache.camel.support.ScheduledPollConsumer.run(ScheduledPollConsumer.java:116)
>   at
> java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
>   at
> java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305)
>   at
> java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
>   at
> java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
>   at
> java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
>   at java.base/java.lang.Thread.run(Thread.java:833)

Code:

@Component
public class EmailPollingRoute extends RouteBuilder {

    static final Logger LOGGER = LoggerFactory.getLogger(EmailPollingRoute.class);

    @Override
    public void configure() throws Exception {

        //@formatter:off
        from("direct:processPollingEmail")
        .routeId("routeId_EmailPollingRoute") 
        .doTry()
          .log("doTry")
          .pollEnrich("imaps://imap.web.de"
                + "?username=XXXXX&"
                + "password=XXXXX&"
                + "unseen=true&"
                + "closeFolder=false&"
                + "disconnect=false&"
                + "subject=FK100 - daily")

        .doCatch(Exception.class)
            .process(new Processor() {
                @Override
                public void process(Exchange exchange) throws Exception {
                    LOGGER.info("EmailPollingRoute - doCatch - authentication failed");
                    //final Throwable ex = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
                    //exchange.getIn().setBody(ex.getMessage());
                }
            })
             
        .doFinally()
             .log("doFinally")
        .endDoTry();
    }
}


Solution 1:[1]

So I could catch the error after adding the following attribute to the url bridgeErrorHandler=true

.pollEnrich("imaps://imap.web.de"
                + "?username=XXXXX&"
                + "password=XXXXX&"
                + "unseen=true&"
                + "closeFolder=false&"
                + "disconnect=false&"
                +"bridgeErrorHandler=true&"
                + "subject=FK100 - daily")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tree