You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/02/26 17:35:38 UTC
svn commit: r748201 -
/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionProcessorInspectCausedExceptionTest.java
Author: davsclaus
Date: Thu Feb 26 16:35:36 2009
New Revision: 748201
URL: http://svn.apache.org/viewvc?rev=748201&view=rev
Log:
Added wiki example for some on exception stuff.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionProcessorInspectCausedExceptionTest.java
- copied, changed from r748160, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRetryUntilTest.java
Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionProcessorInspectCausedExceptionTest.java (from r748160, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRetryUntilTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionProcessorInspectCausedExceptionTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionProcessorInspectCausedExceptionTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRetryUntilTest.java&r1=748160&r2=748201&rev=748201&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRetryUntilTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionProcessorInspectCausedExceptionTest.java Thu Feb 26 16:35:36 2009
@@ -16,43 +16,41 @@
*/
package org.apache.camel.processor.onexception;
-import org.apache.camel.Body;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
-import org.apache.camel.ExchangeException;
-import org.apache.camel.Header;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.impl.JndiRegistry;
-import org.apache.camel.processor.DeadLetterChannel;
/**
- * Unit test for the retry until predicate
+ * Unit test for using a processor to peek the caused exception
*/
-public class OnExceptionRetryUntilTest extends ContextTestSupport {
+public class OnExceptionProcessorInspectCausedExceptionTest extends ContextTestSupport {
- private static int invoked;
+ public void testInspectExceptionByProcessor() throws Exception {
+ getMockEndpoint("mock:error").expectedMessageCount(0);
+ getMockEndpoint("mock:myerror").expectedMessageCount(1);
+
+ try {
+ template.sendBody("direct:start", "Hello World");
+ fail("Should throw exception");
+ } catch (Exception e) {
+ // ok
+ }
- @Override
- protected JndiRegistry createRegistry() throws Exception {
- JndiRegistry jndi = super.createRegistry();
- jndi.bind("myRetryHandler", new MyRetryBean());
- return jndi;
+ assertMockEndpointsSatisfied();
}
- public void testRetryUntil() throws Exception {
- context.addRoutes(new RouteBuilder() {
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
@Override
public void configure() throws Exception {
- errorHandler(deadLetterChannel("mock:error").maximumRedeliveries(0));
+ errorHandler(deadLetterChannel("mock:error").maximumRedeliveries(3));
// START SNIPPET: e1
- // we want to use a predicate for retries so we can determine in our bean
- // when retry should stop
- onException(MyFunctionalException.class)
- .retryUntil(bean("myRetryHandler"))
- .handled(true)
- .transform().constant("Sorry");
+ // here we register exception cause for MyFunctionException
+ // when this exception occur we want it to be processed by our proceesor
+ onException(MyFunctionalException.class).process(new MyFunctionFailureHandler());
// END SNIPPET: e1
from("direct:start").process(new Processor() {
@@ -61,28 +59,24 @@
}
});
}
- });
-
- Object out = template.requestBody("direct:start", "Hello World");
- assertEquals("Sorry", out);
- assertEquals(3, invoked);
+ };
}
// START SNIPPET: e2
- public class MyRetryBean {
-
- // using bean binding we can bind the information from the exchange to the types we have in our method signature
- public boolean retryUntil(@Header(Exchange.REDELIVERY_COUNTER) Integer counter, @Body String body, @ExchangeException Exception causedBy) {
- // NOTE: counter is the redelivery attempt, will start from 1
- invoked++;
+ public static class MyFunctionFailureHandler implements Processor {
- assertEquals("Hello World", body);
- assertTrue(causedBy instanceof MyFunctionalException);
+ public void process(Exchange exchange) throws Exception {
+ // the caused by exception is stored in a property on the exchange
+ Throwable caused = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
+ assertNotNull(caused);
+ // here you can do what you want, but Camel regard this exception as handled, and
+ // this processor as a failurehandler, so it wont do redeliveries. So this is the
+ // end of this route. But if we want to route it somewhere we can just get a
+ // producer template and send it.
- // we can of course do what ever we want to determine the result but this is a unit test so we end after 3 attempts
- return counter < 3;
+ // send it to our mock endpoint
+ exchange.getContext().createProducerTemplate().send("mock:myerror", exchange);
}
}
// END SNIPPET: e2
-
}
\ No newline at end of file