camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christian Müller <christian.muel...@gmail.com>
Subject Re: global onException only works for first to() route item but nothing after that
Date Wed, 19 Dec 2012 21:09:38 GMT
I updated this example for you (you don't have to use inner classes, but
it's easier to share...):

import org.apache.camel.CamelExecutionException;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.JndiRegistry;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class CamelGlobalOnExceptionTest extends CamelTestSupport {

    @Test
    public void test() throws Exception {
        MockEndpoint mockErrorHandler = ((MockEndpoint)
context.getEndpoint("mock:errorHandler"));
        mockErrorHandler.expectedMessageCount(1);

        MockEndpoint mockEnd = ((MockEndpoint)
context.getEndpoint("mock:end"));
        mockEnd.setSleepForEmptyTest(2000);
        mockEnd.expectedMessageCount(0);

        try {
            template.sendBody("direct:start", "Camel");
            fail("CamelExecutionException expected");
        } catch (CamelExecutionException e) {
            // expected
        }

        mockErrorHandler.assertIsSatisfied();
    }

    @Override
    protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry registry = super.createRegistry();

        registry.bind("bean1", new MyBean(false));
        registry.bind("bean2", new MyBean(true));

        return registry;
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new MyRouteBuilder();
    }

    public static class MyRouteBuilder extends RouteBuilder {
        public void configure() throws Exception {
            getContext().setTracing(true);

            onException(Exception.class)
                .handled(false)
                .to("mock:errorHandler")
                .logStackTrace(true);

            from("direct:start")
                .to("bean:bean1")
                .to("bean:bean2")
                .to("mock:end");
        }
    }

    public static class MyBean {

        private boolean shouldThrowAnException;

        public MyBean(boolean shouldThrowAnException) {
            this.shouldThrowAnException = shouldThrowAnException;
        }

        public void process(Exchange exchange) throws Exception {
            if (shouldThrowAnException) {
                throw new Exception("exception forced for unit test");
            }
        }
    }
}

and  use the following log4j.properties file:

log4j.rootLogger=INFO, file, out

# CONSOLE appender not used by default
log4j.appender.out=org.apache.log4j.ConsoleAppender
log4j.appender.out.layout=org.apache.log4j.PatternLayout
log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1}
- %m%n

# File appender
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1}
- %m%n
log4j.appender.file.file=target/test.log
log4j.appender.file.append=true

and got the following log entries:

2012-12-19 22:05:39,311 [main           ] INFO
Tracer                         -
ID-Christian-Muellers-MacBook-Pro-local-55586-1355951138794-0-2 >>>
(route1) from(direct://start) --> bean://bean1 <<< Pattern:InOnly,
Headers:{breadcrumbId=ID-Christian-Muellers-MacBook-Pro-local-55586-1355951138794-0-1},
BodyType:String, Body:Camel
2012-12-19 22:05:39,332 [main           ] INFO
LazyLoadingTypeConverter       - Loaded additional 0 type converters (total
169 type converters) in 0.001 seconds
2012-12-19 22:05:39,336 [main           ] INFO
Tracer                         -
ID-Christian-Muellers-MacBook-Pro-local-55586-1355951138794-0-2 >>>
(route1) bean://bean1 --> bean://bean2 <<< Pattern:InOnly,
Headers:{breadcrumbId=ID-Christian-Muellers-MacBook-Pro-local-55586-1355951138794-0-1},
BodyType:String, Body:Camel
2012-12-19 22:05:39,338 [main           ] INFO
Tracer                         -
ID-Christian-Muellers-MacBook-Pro-local-55586-1355951138794-0-2 >>>
(route1) bean://bean2 --> OnException[Exception] <<< Pattern:InOnly,
Headers:{breadcrumbId=ID-Christian-Muellers-MacBook-Pro-local-55586-1355951138794-0-1,
CamelRedeliveryCounter=0, CamelRedelivered=false}, BodyType:String,
Body:Camel
2012-12-19 22:05:39,338 [main           ] INFO
Tracer                         -
ID-Christian-Muellers-MacBook-Pro-local-55586-1355951138794-0-2 >>>
OnException[Exception] --> mock://errorHandler <<< Pattern:InOnly,
Headers:{breadcrumbId=ID-Christian-Muellers-MacBook-Pro-local-55586-1355951138794-0-1,
CamelRedeliveryCounter=0, CamelRedelivered=false}, BodyType:String,
Body:Camel

This should also work for you...

Best,
Christian

On Wed, Dec 19, 2012 at 7:05 PM, semiosis <camel@louiszuckerman.com> wrote:

> Hi Christian,
>
> I am using the Java DSL, overriding the configure() method in a subclass of
> SpringRouteBuilder.  I added this line to my configure() method, also to
> the
> configure() method in the test case you sent me...
>
>    getContext().setTracing(true)
>
> and added some explicit log4j info messages to the code to verify logging
> is
> working.  All i see are my own messages, nothing from camel.
>
> To recap, my problem is that I am having trouble with exception handling
> after the first bean in my route.  I want to trace execution so that I can
> figure out where that exception is going when it is silently dropped.  But
> I
> can't get tracing to work even in the simple test case.
>
> Thanks again for all the help,
>
> -louis
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/global-onException-only-works-for-first-to-route-item-but-nothing-after-that-tp5724296p5724392.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



--

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message