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/01/03 14:50:34 UTC
svn commit: r730985 - in /activemq/camel/trunk/camel-core/src:
main/java/org/apache/camel/builder/ main/java/org/apache/camel/model/
test/java/org/apache/camel/processor/onexception/
Author: davsclaus
Date: Sat Jan 3 05:50:33 2009
New Revision: 730985
URL: http://svn.apache.org/viewvc?rev=730985&view=rev
Log:
CAMEL-1218: Added more complex wiki samples for exception clause. Added some toString so its easier to read in debugger.
Added:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexRouteTest.java (with props)
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexWithNestedErrorHandlerRouteTest.java (with props)
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionSubRouteTest.java (with props)
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ConstantProcessorBuilder.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorType.java
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ConstantProcessorBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ConstantProcessorBuilder.java?rev=730985&r1=730984&r2=730985&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ConstantProcessorBuilder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ConstantProcessorBuilder.java Sat Jan 3 05:50:33 2009
@@ -17,6 +17,7 @@
package org.apache.camel.builder;
import org.apache.camel.Processor;
+import org.apache.camel.util.ObjectHelper;
/**
* A builder wrapping a {@link Processor}.
@@ -27,10 +28,16 @@
private Processor processor;
public ConstantProcessorBuilder(Processor processor) {
+ ObjectHelper.notNull(processor, "processor");
this.processor = processor;
}
public Processor createProcessor() {
return processor;
}
+
+ @Override
+ public String toString() {
+ return processor.toString();
+ }
}
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java?rev=730985&r1=730984&r2=730985&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java Sat Jan 3 05:50:33 2009
@@ -250,4 +250,8 @@
this.exceptionPolicyStrategy = exceptionPolicyStrategy;
}
+ @Override
+ public String toString() {
+ return "DeadLetterChannelBuilder(" + (deadLetterFactory != null ? deadLetterFactory : defaultDeadLetterEndpoint) + ")";
+ }
}
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorType.java?rev=730985&r1=730984&r2=730985&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorType.java Sat Jan 3 05:50:33 2009
@@ -1463,6 +1463,9 @@
*/
public Type errorHandler(ErrorHandlerBuilder errorHandlerBuilder) {
setErrorHandlerBuilder(errorHandlerBuilder);
+ // TODO: davsclaus. I think we need to set the parent so you can use .end() blocks for
+ // nested error handler with a complex routing
+ //setParent(this);
return (Type) this;
}
Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexRouteTest.java?rev=730985&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexRouteTest.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexRouteTest.java Sat Jan 3 05:50:33 2009
@@ -0,0 +1,117 @@
+package org.apache.camel.processor.onexception;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+
+public class OnExceptionComplexRouteTest extends ContextTestSupport {
+
+ protected MyServiceBean myServiceBean;
+
+ public void testNoError() throws Exception {
+ getMockEndpoint("mock:error").expectedMessageCount(0);
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+
+ template.sendBody("direct:start", "<order><type>myType</type><user>James</user></order>");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testNoError2() throws Exception {
+ getMockEndpoint("mock:error").expectedMessageCount(0);
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+
+ template.sendBody("direct:start2", "<order><type>myType</type><user>James</user></order>");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testFunctionalError() throws Exception {
+ getMockEndpoint("mock:error").expectedMessageCount(1);
+ getMockEndpoint("mock:result").expectedMessageCount(0);
+
+ try {
+ template.sendBody("direct:start", "<order><type>myType</type><user>Func</user></order>");
+ fail("Should have thrown a MyFunctionalException");
+ } catch (RuntimeCamelException e) {
+ assertIsInstanceOf(MyFunctionalException.class, e.getCause());
+ }
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testFunctionalError2() throws Exception {
+ getMockEndpoint("mock:error").expectedMessageCount(0);
+ getMockEndpoint("mock:handled").expectedMessageCount(1);
+ getMockEndpoint("mock:result").expectedMessageCount(0);
+
+ template.sendBody("direct:start2", "<order><type>myType</type><user>Func</user></order>");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testTechnicalError() throws Exception {
+ getMockEndpoint("mock:error").expectedMessageCount(0);
+ getMockEndpoint("mock:tech.error").expectedMessageCount(1);
+ getMockEndpoint("mock:result").expectedMessageCount(0);
+
+ template.sendBody("direct:start", "<order><type>myType</type><user>Tech</user></order>");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testTechnicalError2() throws Exception {
+ getMockEndpoint("mock:error").expectedMessageCount(0);
+ getMockEndpoint("mock:tech.error").expectedMessageCount(1);
+ getMockEndpoint("mock:result").expectedMessageCount(0);
+
+ template.sendBody("direct:start2", "<order><type>myType</type><user>Tech</user></order>");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ myServiceBean = new MyServiceBean();
+ super.setUp();
+ }
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("myServiceBean", myServiceBean);
+ return jndi;
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ // START SNIPPET: e1
+ // global error handler
+ errorHandler(deadLetterChannel("mock:error"));
+
+ // shared for both routes
+ onException(MyTechnicalException.class).handled(true).maximumRedeliveries(2).to("mock:tech.error");
+
+ from("direct:start")
+ // route specific on exception for MyFunctionalException
+ // we MUST use .end() to indicate that this sub block is ended
+ .onException(MyFunctionalException.class).maximumRedeliveries(0).end()
+ .to("bean:myServiceBean")
+ .to("mock:result");
+
+ from("direct:start2")
+ // route specific on exception for MyFunctionalException that is different than the previous route
+ // here we marked it as handled and send it to a different destination mock:handled
+ // we MUST use .end() to indicate that this sub block is ended
+ .onException(MyFunctionalException.class).handled(true).maximumRedeliveries(0).to("mock:handled").end()
+ .to("bean:myServiceBean")
+ .to("mock:result");
+ // END SNIPPET: e1
+ }
+ };
+ }
+}
Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexRouteTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexRouteTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexWithNestedErrorHandlerRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexWithNestedErrorHandlerRouteTest.java?rev=730985&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexWithNestedErrorHandlerRouteTest.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexWithNestedErrorHandlerRouteTest.java Sat Jan 3 05:50:33 2009
@@ -0,0 +1,69 @@
+package org.apache.camel.processor.onexception;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class OnExceptionComplexWithNestedErrorHandlerRouteTest extends OnExceptionComplexRouteTest {
+
+ public void testNoError3() throws Exception {
+ getMockEndpoint("mock:error").expectedMessageCount(0);
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+
+ template.sendBody("direct:start3", "<order><type>myType</type><user>James</user></order>");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testFunctionalError3() throws Exception {
+ getMockEndpoint("mock:error").expectedMessageCount(0);
+ getMockEndpoint("mock:error3").expectedMessageCount(1);
+ getMockEndpoint("mock:result").expectedMessageCount(0);
+
+ template.sendBody("direct:start3", "<order><type>myType</type><user>Func</user></order>");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ // START SNIPPET: e1
+ // global error handler
+ errorHandler(deadLetterChannel("mock:error"));
+
+ // shared for both routes
+ onException(MyTechnicalException.class).handled(true).maximumRedeliveries(2).to("mock:tech.error");
+
+ from("direct:start")
+ // route specific on exception for MyFunctionalException
+ // we MUST use .end() to indicate that this sub block is ended
+ .onException(MyFunctionalException.class).maximumRedeliveries(0).end()
+ .to("bean:myServiceBean")
+ .to("mock:result");
+
+ from("direct:start2")
+ // route specific on exception for MyFunctionalException that is different than the previous route
+ // here we marked it as handled and send it to a different destination mock:handled
+ // we MUST use .end() to indicate that this sub block is ended
+ .onException(MyFunctionalException.class).handled(true).maximumRedeliveries(0).to("mock:handled").end()
+ .to("bean:myServiceBean")
+ .to("mock:result");
+
+ from("direct:start3")
+ // route specific error handler that is different than the global error handler
+ // here we do not redeliver and transform the body to a simple text message with
+ // the exception message using the SimpleLanguage
+ // we MUST use .end() to indicate that this sub block is ended
+ .errorHandler(deadLetterChannel("mock:error3")
+ .maximumRedeliveries(0))
+
+ // route specific on exception for all exception to mark them as handled
+ .onException(Exception.class).handled(true).end()
+ .to("bean:myServiceBean")
+ .to("mock:result");
+ // END SNIPPET: e1
+ }
+ };
+ }
+}
\ No newline at end of file
Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexWithNestedErrorHandlerRouteTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexWithNestedErrorHandlerRouteTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionSubRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionSubRouteTest.java?rev=730985&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionSubRouteTest.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionSubRouteTest.java Sat Jan 3 05:50:33 2009
@@ -0,0 +1,61 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor.onexception;
+
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * Unit test inspired by user forum.
+ */
+public class OnExceptionSubRouteTest extends OnExceptionRouteTest {
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ // START SNIPPET: e1
+ // default should errors go to mock:error
+ errorHandler(deadLetterChannel("mock:error"));
+
+ // here we start the routing with the consumer
+ from("direct:start")
+
+ // if a MyTechnicalException is thrown we will not try to redeliver and we mark it as handled
+ // so the caller does not get a failure
+ // since we have no to then the exchange will continue to be routed to the normal error handler
+ // destination that is mock:error as defined above
+ // we MUST use .end() to indicate that this sub block is ended
+ .onException(MyTechnicalException.class).maximumRedeliveries(0).handled(true).end()
+
+ // if a MyFunctionalException is thrown we do not want Camel to redelivery but handle it our self using
+ // our bean myOwnHandler, then the exchange is not routed to the default error (mock:error)
+ // we MUST use .end() to indicate that this sub block is ended
+ .onException(MyFunctionalException.class).maximumRedeliveries(0).handled(true).to("bean:myOwnHandler").end()
+
+ // here we have the regular routing
+ .choice()
+ .when().xpath("//type = 'myType'").to("bean:myServiceBean")
+ .end()
+ .to("mock:result");
+ // END SNIPPET: e1
+ }
+ };
+ }
+
+
+}
\ No newline at end of file
Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionSubRouteTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionSubRouteTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date