Author: ningjiang
Date: Fri Mar 26 11:33:29 2010
New Revision: 927777

URL: http://svn.apache.org/viewvc?rev=927777&view=rev
Log:
CAMEL-1799 Added TransactionErrorHandler and used ErrorHandlerType enum for 
type attribute.

Added:
    
camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.java
   (with props)
    
camel/trunk/components/camel-jms/src/test/resources/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.xml
   (with props)
    
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/ErrorHandlerType.java
   (with props)
Modified:
    
camel/trunk/components/camel-jms/src/test/resources/org/apache/camel/component/jms/issues/JmsRedeliveryWithInitialRedeliveryDelayTest-context.xml
    
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/ErrorHandlerDefintion.java
    
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/ErrorHandlerDefinitionParser.java

Added: 
camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.java?rev=927777&view=auto
==============================================================================
--- 
camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.java
 (added)
+++ 
camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.java
 Fri Mar 26 11:33:29 2010
@@ -0,0 +1,65 @@
+/**
+ * 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.component.jms.tx;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelSpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * To demonstrate the TransactionErrorHandlerBuilder configured in Spring XML.
+ */
+public class TransactionErrorHandlerCustomerSpringParserTest extends 
CamelSpringTestSupport {
+
+    protected ClassPathXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext(
+            
"/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.xml");
+    }
+
+    protected int getExpectedRouteCount() {
+        return 1;
+    }
+
+    @Test
+    public void testTransactionSuccess() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived("Bye World");
+        // success at 3rd attempt
+        mock.message(0).header("count").isEqualTo(3);
+
+        template.sendBody("activemq:queue:okay", "Hello World");
+
+        mock.assertIsSatisfied();
+    }
+
+    public static class MyProcessor implements Processor {
+        private int count;
+
+        public void process(Exchange exchange) throws Exception {
+            if (++count <= 2) {
+                throw new IllegalArgumentException("Forced Exception number " 
+ count + ", please retry");
+            }
+            exchange.getIn().setBody("Bye World");
+            exchange.getIn().setHeader("count", count);
+        }
+    }
+
+}
\ No newline at end of file

Propchange: 
camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: 
camel/trunk/components/camel-jms/src/test/resources/org/apache/camel/component/jms/issues/JmsRedeliveryWithInitialRedeliveryDelayTest-context.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/test/resources/org/apache/camel/component/jms/issues/JmsRedeliveryWithInitialRedeliveryDelayTest-context.xml?rev=927777&r1=927776&r2=927777&view=diff
==============================================================================
--- 
camel/trunk/components/camel-jms/src/test/resources/org/apache/camel/component/jms/issues/JmsRedeliveryWithInitialRedeliveryDelayTest-context.xml
 (original)
+++ 
camel/trunk/components/camel-jms/src/test/resources/org/apache/camel/component/jms/issues/JmsRedeliveryWithInitialRedeliveryDelayTest-context.xml
 Fri Mar 26 11:33:29 2010
@@ -48,7 +48,7 @@
 
     <bean id="myRedeliveryPolicyConfig" 
class="org.apache.camel.processor.RedeliveryPolicy">
         <property name="maximumRedeliveries" value="4"/>
-        <property name="redeliverDelay" value="50"/>
+        <property name="redeliveryDelay" value="50"/>
         <property name="useExponentialBackOff" value="true"/>
         <property name="backOffMultiplier" value="2"/>
     </bean>

Added: 
camel/trunk/components/camel-jms/src/test/resources/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/test/resources/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.xml?rev=927777&view=auto
==============================================================================
--- 
camel/trunk/components/camel-jms/src/test/resources/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.xml
 (added)
+++ 
camel/trunk/components/camel-jms/src/test/resources/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.xml
 Fri Mar 26 11:33:29 2010
@@ -0,0 +1,96 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xmlns:camel="http://camel.apache.org/schema/spring";
+       xsi:schemaLocation="
+         http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+         http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd";>
+
+    <!-- START SNIPPET: e1 -->
+    <!-- here we define our camel context -->
+    <camel:camelContext id="myroutes">
+
+        <!-- first route with transaction error handler -->
+        <!-- here we refer to our transaction error handler we define in this 
Spring XML file -->
+        <!-- in this route the transactionErrorHandler is used -->
+        <camel:route errorHandlerRef="transactionErrorHandler">
+            <!-- 1: from the jms queue -->
+            <camel:from uri="activemq:queue:okay"/>
+            <!-- 2: setup the transactional boundaries to require a 
transaction -->
+            <camel:policy ref="required"/>
+            <!-- 3: call our business logic that is myProcessor -->
+            <camel:process ref="myProcessor"/>
+            <!-- 4: if success then send it to the mock -->
+            <camel:to uri="mock:result"/>
+        </camel:route>
+
+        <!-- 2nd route with no error handling -->
+        <!-- this route doens't use error handler, in fact the spring bean 
with id noErrorHandler -->
+        <camel:route errorHandlerRef="noErrorHandler">
+            <camel:from uri="activemq:queue:bad"/>
+            <camel:to uri="log:bad"/>
+        </camel:route>
+
+    </camel:camelContext>
+    <!-- END SNIPPET: e1 -->
+
+    <bean id="myProcessor" 
class="org.apache.camel.component.jms.tx.JMSTransactionalClientTest$MyProcessor"/>
+
+    <!-- START SNIPPET: e2 -->
+    <!-- camel policy we refer to in our route -->
+    <bean id="required" 
class="org.apache.camel.spring.spi.SpringTransactionPolicy">
+        <property name="transactionTemplate" ref="PROPAGATION_REQUIRED"/>
+    </bean>
+
+    <!-- the standard spring transaction template for required -->
+    <bean id="PROPAGATION_REQUIRED" 
class="org.springframework.transaction.support.TransactionTemplate">
+        <property name="transactionManager" ref="jmsTransactionManager"/>
+    </bean>
+
+    <!-- the transaction error handle we refer to from the route -->
+    <camel:errorHandler id="transactionErrorHandler" 
type="TransactionErrorHandler">
+        <camel:transactionTemplate>
+               <refbean value="PROPAGATION_REQUIRED"/>
+        </camel:transactionTemplate>
+    </camel:errorHandler>
+
+    <!-- the no error handler -->
+    <camel:errorHandler id="noErrorHandler" type="NoErrorHandler"/>
+    <!-- END SNIPPET: e2 -->
+
+    <bean id="jmsConnectionFactory" 
class="org.apache.activemq.ActiveMQConnectionFactory">
+        <property name="brokerURL" 
value="vm://localhost?broker.persistent=false&amp;broker.useJmx=false"/>
+    </bean>
+
+    <bean id="jmsTransactionManager" 
class="org.springframework.jms.connection.JmsTransactionManager">
+        <property name="connectionFactory" ref="jmsConnectionFactory"/>
+    </bean>
+
+    <bean id="jmsConfig" 
class="org.apache.camel.component.jms.JmsConfiguration">
+        <property name="connectionFactory" ref="jmsConnectionFactory"/>
+        <property name="transactionManager" ref="jmsTransactionManager"/>
+        <property name="transacted" value="true"/>
+        <property name="concurrentConsumers" value="1"/>
+    </bean>
+
+    <bean id="activemq" class="org.apache.camel.component.jms.JmsComponent">
+        <property name="configuration" ref="jmsConfig"/>
+    </bean>
+
+</beans>

Propchange: 
camel/trunk/components/camel-jms/src/test/resources/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-jms/src/test/resources/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: 
camel/trunk/components/camel-jms/src/test/resources/org/apache/camel/component/jms/tx/TransactionErrorHandlerCustomerSpringParserTest.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Modified: 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/ErrorHandlerDefintion.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/ErrorHandlerDefintion.java?rev=927777&r1=927776&r2=927777&view=diff
==============================================================================
--- 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/ErrorHandlerDefintion.java
 (original)
+++ 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/ErrorHandlerDefintion.java
 Fri Mar 26 11:33:29 2010
@@ -38,7 +38,7 @@ import org.apache.camel.model.config.Pro
 @XmlAccessorType(XmlAccessType.FIELD)
 public class ErrorHandlerDefintion extends IdentifiedType {
     @XmlAttribute(required = true)
-    private String type;
+    private ErrorHandlerType type;
     @XmlAttribute(required = false)
     private String deadLetterUri;
     @XmlAttribute(required = false)
@@ -53,6 +53,7 @@ public class ErrorHandlerDefintion exten
         @XmlElement(name = "exceptionPolicyStrategy",  required = false),
         @XmlElement(name = "onRedelivery", required = false),
         @XmlElement(name = "failureProcessor", required = false),
+        @XmlElement(name = "transactionTemplate", required = false),
         @XmlElement(name = "deadLetter", required = false)})
     private List beans; 
 

Added: 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/ErrorHandlerType.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/ErrorHandlerType.java?rev=927777&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/ErrorHandlerType.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/ErrorHandlerType.java
 Fri Mar 26 11:33:29 2010
@@ -0,0 +1,29 @@
+/**
+ * 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.spring;
+
+import javax.xml.bind.annotation.XmlEnum;
+
+/**
+ * Used to configure the errorHandler type
+ *
+ * @version $Revision$
+ */
+...@xmlenum
+public enum ErrorHandlerType {
+   DefaultErrorHandler, DeadLetterChannel, LoggingErrorHandler, 
NoErrorHandler, TransactionErrorHandler
+}

Propchange: 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/ErrorHandlerType.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/ErrorHandlerType.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/ErrorHandlerDefinitionParser.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/ErrorHandlerDefinitionParser.java?rev=927777&r1=927776&r2=927777&view=diff
==============================================================================
--- 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/ErrorHandlerDefinitionParser.java
 (original)
+++ 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/ErrorHandlerDefinitionParser.java
 Fri Mar 26 11:33:29 2010
@@ -29,6 +29,8 @@ import org.apache.camel.builder.LoggingE
 import org.apache.camel.builder.NoErrorHandlerBuilder;
 import org.apache.camel.processor.RedeliveryPolicy;
 import org.apache.camel.spring.CamelEndpointFactoryBean;
+import org.apache.camel.spring.ErrorHandlerType;
+import org.apache.camel.spring.spi.TransactionErrorHandlerBuilder;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -53,21 +55,24 @@ public class ErrorHandlerDefinitionParse
     }
 
     protected Class getBeanClass(Element element) {
-        String type = element.getAttribute("type");
+        ErrorHandlerType type = 
ErrorHandlerType.valueOf(element.getAttribute("type"));
         Class clazz = null;
-        if (type.equals("NoErrorHandler")) {
+        if (type.equals(ErrorHandlerType.NoErrorHandler)) {
             clazz = NoErrorHandlerBuilder.class;
         }
-        if (type.equals("DeadLetterChannel")) {
+        if (type.equals(ErrorHandlerType.DeadLetterChannel)) {
             clazz = DeadLetterChannelBuilder.class;
             
         }
-        if (type.equals("LoggingErrorHandler")) {
+        if (type.equals(ErrorHandlerType.LoggingErrorHandler)) {
             clazz = LoggingErrorHandlerBuilder.class;
         }
-        if (type.equals("DefaultErrorHandler")) {
+        if (type.equals(ErrorHandlerType.DefaultErrorHandler)) {
             clazz = DefaultErrorHandlerBuilder.class;
         }
+        if (type.equals(ErrorHandlerType.TransactionErrorHandler)) {
+            clazz = TransactionErrorHandlerBuilder.class;
+        }
         if (clazz == null) {
             throw new IllegalArgumentException("Cannot find the ErrorHandle 
with type " + type);
         }
@@ -83,12 +88,14 @@ public class ErrorHandlerDefinitionParse
     @Override
     protected void doParse(Element element, ParserContext parserContext, 
BeanDefinitionBuilder builder) {
         super.doParse(element, parserContext, builder);
-        String type = element.getAttribute("type");
-        if (type.equals("NoErrorHandler") || 
type.equals("LoggingErrorHandler")) {
+        ErrorHandlerType type = 
ErrorHandlerType.valueOf(element.getAttribute("type"));
+        if (type.equals(ErrorHandlerType.NoErrorHandler) || 
type.equals(ErrorHandlerType.LoggingErrorHandler)) {
             // don't need to parser other stuff
             return;
         }
-        if (type.equals("DefaultErrorHandler") || 
type.equals("DeadLetterChannel")) {
+        if (type.equals(ErrorHandlerType.DefaultErrorHandler) 
+            || type.equals(ErrorHandlerType.DeadLetterChannel) 
+            || type.equals(ErrorHandlerType.TransactionErrorHandler)) {
             NodeList list = element.getChildNodes();
             int size = list.getLength();
             for (int i = 0; i < size; i++) {


Reply via email to