How do you deal with exceptions thrown by a converter? I have set up
some onException clauses but they only seem to kick in if an exception
is thrown from a endpoint or a processor.
When I run the following code I can see it retrying the default number
of times and then the DeadLetterChannel kick in. My mock assertions
fail as neither queue recieves any messages. I can see the sysout in
the converter being printed, so it is being invoked.
Am I doing something incorrect here? I would have thought that there
was a strong posibility for a converter to need to throw an exception,
in my case here it would be if the message was missing mandatory fields
such as telephone number or email.
/////Converter
@Converter
public static LocalDateTime toLocalDateTime(final Object localDateTime)
{
System.out.println("Converting to local date time");
throw new IllegalArgumentException("Bad data");
}
/////Routes
Routes routes = new RouteBuilder() {
@Override
public void configure() throws Exception {
onException(IllegalArgumentException.class).handled(true).to("mock:exception");
from("direct:test").convertBodyTo(LocalDateTime.class).to("mock:end");
}
};
/////Test
MockEndpoint endpoint = (MockEndpoint)
camelContext.getEndpoint("mock:end");
endpoint.expectedMessageCount(0);
MockEndpoint endpoint2 = (MockEndpoint)
camelContext.getEndpoint("mock:exception");
endpoint2.expectedMessageCount(1);
camelContext.createProducerTemplate().sendBody("direct:test", "test");
endpoint.assertIsSatisfied();
endpoint2.assertIsSatisfied();