Author: davsclaus Date: Tue Feb 10 12:42:42 2009 New Revision: 742953 URL: http://svn.apache.org/viewvc?rev=742953&view=rev Log: CAMEL-699: Added example for traceable unit of work
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceableUnitOfWorkTest.java (contents, props changed) - copied, changed from r742867, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceInterceptorTest.java Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceableUnitOfWorkTest.java (from r742867, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceInterceptorTest.java) URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceableUnitOfWorkTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceableUnitOfWorkTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceInterceptorTest.java&r1=742867&r2=742953&rev=742953&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceInterceptorTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceableUnitOfWorkTest.java Tue Feb 10 12:42:42 2009 @@ -16,46 +16,80 @@ */ package org.apache.camel.processor; +import java.util.List; + import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; +import org.apache.camel.model.ProcessorType; import org.apache.camel.processor.interceptor.Tracer; +import org.apache.camel.spi.TraceableUnitOfWork; /** * @version $Revision$ */ -public class TraceInterceptorTest extends ContextTestSupport { +public class TraceableUnitOfWorkTest extends ContextTestSupport { - // START SNIPPET: e1 public void testSendingSomeMessages() throws Exception { - template.sendBodyAndHeader("direct:start", "Hello London", "to", "James"); - template.sendBodyAndHeader("direct:start", "This is Copenhagen calling", "from", "Claus"); + Object out = template.requestBody("direct:start", "Hello London"); + assertEquals("Failed at: bean:bar", out); + } + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("foo", new MyFooBean()); + jndi.bind("bar", new MyBarBean()); + return jndi; } + // START SNIPPET: e1 protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { - // add tracer as an interceptor so it will log the exchange executions at runtime - // this can aid us to understand/see how the exchanges is routed etc. - getContext().addInterceptStrategy(new Tracer()); - - from("direct:start"). - process(new Processor() { - public void process(Exchange exchange) throws Exception { - // do nothing - } - - @Override - public String toString() { - return "MyProcessor"; - } - }). - to("mock:a"). - to("mock:b"); + // must enable tracer to trace the route path taken during runtime + context.addInterceptStrategy(new Tracer()); + + // let our my error processor handle all exceptions + onException(Exception.class).handled(true).process(new MyErrorProcessor()); + + // our route where an exception can be thrown from either foo or bar bean + // so we have enable tracing so we can check it at runtime to get the actual + // node path taken + from("direct:start").to("bean:foo").to("bean:bar"); } }; } // END SNIPPET: e1 -} + // START SNIPPET: e2 + private class MyErrorProcessor implements Processor { + public void process(Exchange exchange) throws Exception { + // cast to TraceableUnitOfWork so we can work on the intercepted node path + TraceableUnitOfWork tuow = (TraceableUnitOfWork) exchange.getUnitOfWork(); + + // get the list of intercepted nodes + List<ProcessorType> list = tuow.getInterceptedNodes(); + // get the 2nd last as the last is me (MyErrorProcessor) + ProcessorType last = list.get(list.size() - 2); + + // set error message + exchange.getFault().setBody("Failed at: " + last.getLabel()); + } + } + // END SNIPPET: e2 + + public class MyFooBean { + public void process(Exchange exchange) throws Exception { + exchange.getOut().setBody("Foo okay"); + } + } + + public class MyBarBean { + public void process(Exchange exchange) throws Exception { + throw new IllegalArgumentException("Damm Bar"); + } + } +} \ No newline at end of file Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceableUnitOfWorkTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceableUnitOfWorkTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceableUnitOfWorkTest.java ------------------------------------------------------------------------------ svn:mergeinfo =