You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ge...@apache.org on 2008/11/04 14:40:39 UTC

svn commit: r711250 - in /servicemix/components/engines/servicemix-camel/trunk/src/test: java/org/apache/servicemix/camel/ java/org/apache/servicemix/camel/su9/ resources/org/apache/servicemix/camel/su9-src/

Author: gertv
Date: Tue Nov  4 05:40:38 2008
New Revision: 711250

URL: http://svn.apache.org/viewvc?rev=711250&view=rev
Log:
SM-1673: Improve error handling on servicemix-camel

Added:
    servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithFaultHandledTrueSpringDSLTest.java   (with props)
    servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/su9/
    servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/su9/HandleFaultProcessor.java   (with props)
    servicemix/components/engines/servicemix-camel/trunk/src/test/resources/org/apache/servicemix/camel/su9-src/
    servicemix/components/engines/servicemix-camel/trunk/src/test/resources/org/apache/servicemix/camel/su9-src/camel-context.xml   (with props)
Modified:
    servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithErrorHandledTrueSpringDSLTest.java

Modified: servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithErrorHandledTrueSpringDSLTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithErrorHandledTrueSpringDSLTest.java?rev=711250&r1=711249&r2=711250&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithErrorHandledTrueSpringDSLTest.java (original)
+++ servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithErrorHandledTrueSpringDSLTest.java Tue Nov  4 05:40:38 2008
@@ -22,8 +22,6 @@
 import javax.jbi.messaging.InOnly;
 import javax.jbi.messaging.MessageExchange;
 import javax.jbi.messaging.MessagingException;
-import javax.jbi.messaging.NormalizedMessage;
-import javax.jbi.messaging.RobustInOnly;
 import javax.xml.namespace.QName;
 
 import org.apache.servicemix.MessageExchangeListener;

Added: servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithFaultHandledTrueSpringDSLTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithFaultHandledTrueSpringDSLTest.java?rev=711250&view=auto
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithFaultHandledTrueSpringDSLTest.java (added)
+++ servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithFaultHandledTrueSpringDSLTest.java Tue Nov  4 05:40:38 2008
@@ -0,0 +1,90 @@
+/*
+ * 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.servicemix.camel;
+
+import java.util.List;
+
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.Fault;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.xml.namespace.QName;
+
+import org.apache.camel.converter.jaxp.StringSource;
+import org.apache.servicemix.MessageExchangeListener;
+import org.apache.servicemix.client.ServiceMixClient;
+import org.apache.servicemix.components.util.ComponentSupport;
+import org.apache.servicemix.jbi.container.ActivationSpec;
+import org.apache.servicemix.tck.ReceiverComponent;
+
+/**
+ * Tests on handling fault messages with the Camel Exception handler
+ */
+public class JbiInOnlyWithFaultHandledTrueSpringDSLTest extends SpringJbiTestSupport {
+
+    private static final QName TEST_SERVICE = new QName("urn:test", "fault-handled-true");
+
+    private ReceiverComponent receiver;
+    private ReceiverComponent deadLetter;
+
+    @Override
+    protected void setUp() throws Exception {
+        receiver = new ReceiverComponent();
+        deadLetter = new ReceiverComponent();
+
+        super.setUp();
+    }
+
+    public void testFaultHandledByExceptionClause() throws Exception {
+        ServiceMixClient smxClient = getServicemixClient();
+        InOnly exchange = smxClient.createInOnlyExchange();
+        exchange.setEndpoint(jbiContainer.getRegistry().getEndpointsForService(TEST_SERVICE)[0]);
+
+        smxClient.send(exchange);
+
+        exchange = (InOnly) smxClient.receive();
+        assertEquals(ExchangeStatus.DONE, exchange.getStatus());
+
+        receiver.getMessageList().assertMessagesReceived(1);
+        deadLetter.getMessageList().assertMessagesReceived(0);
+    }
+
+    @Override
+    protected String getServiceUnitName() {
+        return "su9";
+    }
+
+    @Override
+    protected void appendJbiActivationSpecs(List<ActivationSpec> activationSpecList) {
+        activationSpecList.add(createActivationSpec(new ReturnFaultComponent(), 
+                                                    new QName("urn:test", "faulty-service")));
+
+        activationSpecList.add(createActivationSpec(receiver, new QName("urn:test", "receiver-service")));
+        activationSpecList.add(createActivationSpec(deadLetter, new QName("urn:test", "deadLetter-service")));
+    }
+
+    protected static class ReturnFaultComponent extends ComponentSupport implements MessageExchangeListener {
+        public void onMessageExchange(MessageExchange exchange) throws MessagingException {
+            if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
+                Fault fault = exchange.createFault();
+                fault.setContent(new StringSource("<fault/>"));
+                fail(exchange, fault);
+            }
+        }
+    }
+}

Propchange: servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithFaultHandledTrueSpringDSLTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/su9/HandleFaultProcessor.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/su9/HandleFaultProcessor.java?rev=711250&view=auto
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/su9/HandleFaultProcessor.java (added)
+++ servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/su9/HandleFaultProcessor.java Tue Nov  4 05:40:38 2008
@@ -0,0 +1,90 @@
+/*
+ * 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.servicemix.camel.su9;
+
+import org.apache.camel.AsyncCallback;
+import org.apache.camel.AsyncProcessor;
+import org.apache.camel.CamelException;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.processor.DelegateProcessor;
+import org.apache.camel.util.AsyncProcessorHelper;
+
+public class HandleFaultProcessor extends DelegateProcessor implements AsyncProcessor {
+    
+    @Override
+    public String toString() {
+        return "HandleFaultProcessor(" + processor + ")";
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        AsyncProcessorHelper.process(this, exchange);
+    }
+
+    public boolean process(final Exchange exchange, final AsyncCallback callback) {
+        if (processor == null) {
+            // no processor so nothing to process, so return
+            callback.done(true);
+            return true;
+        }      
+
+        if (processor instanceof AsyncProcessor) {
+            return ((AsyncProcessor)processor).process(exchange, new AsyncCallback() {
+                
+                public void done(boolean doneSynchronously) {
+                    callback.done(doneSynchronously);
+                    Message faultMessage = exchange.getFault(false);
+                    if (faultMessage != null) {
+                        final Object faultBody = faultMessage.getBody();
+                        if (faultBody != null) {
+                            faultMessage.setBody(null); // Reset it since we are handling it.
+                            if (faultBody instanceof Throwable) {
+                                exchange.setException((Throwable)faultBody);
+                            } else {
+                                exchange.setException(new CamelException("Message contains fault of type "
+                                    + faultBody.getClass().getName() + ":\n" + faultBody));
+                            }
+                        }
+                    }                    
+                }                
+            });
+        }
+        
+        try {
+            processor.process(exchange);
+        } catch (Throwable e) {
+            exchange.setException(e);
+        }
+        
+        final Message faultMessage = exchange.getFault(false);
+        if (faultMessage != null) {
+            final Object faultBody = faultMessage.getBody();
+            if (faultBody != null) {
+                faultMessage.setBody(null); // Reset it since we are handling it.
+                if (faultBody instanceof Throwable) {
+                    exchange.setException((Throwable)faultBody);
+                } else {
+                    exchange.setException(new CamelException("Message contains fault of type "
+                        + faultBody.getClass().getName() + ":\n" + faultBody));
+                }
+            }
+        }
+        callback.done(true);
+        return true;
+    }
+}

Propchange: servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/su9/HandleFaultProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: servicemix/components/engines/servicemix-camel/trunk/src/test/resources/org/apache/servicemix/camel/su9-src/camel-context.xml
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/test/resources/org/apache/servicemix/camel/su9-src/camel-context.xml?rev=711250&view=auto
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/test/resources/org/apache/servicemix/camel/su9-src/camel-context.xml (added)
+++ servicemix/components/engines/servicemix-camel/trunk/src/test/resources/org/apache/servicemix/camel/su9-src/camel-context.xml Tue Nov  4 05:40:38 2008
@@ -0,0 +1,59 @@
+<?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.
+
+-->
+<!-- START SNIPPET: camel -->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
+       http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
+    ">
+
+  <camelContext  xmlns="http://activemq.apache.org/camel/schema/spring">
+    <route errorHandlerRef="deadLetterErrorHandler">
+      <from uri="jbi:service:urn:test:fault-handled-true"/>
+      <onException>
+        <exception>org.apache.servicemix.jbi.FaultException</exception>
+        <redeliveryPolicy maximumRedeliveries="0"/>
+        <handled>
+          <constant>true</constant>
+        </handled>
+        <to uri="jbi:service:urn:test:receiver-service?mep=in-only"/>
+      </onException>
+      <interceptor ref="handleFaultProcessor">
+        <to uri="jbi:service:urn:test:faulty-service?mep=in-only"/>
+      </interceptor>
+    </route>
+  </camelContext>
+
+  <bean id="deadLetterErrorHandler" class="org.apache.camel.builder.DeadLetterChannelBuilder">
+    <property name="defaultDeadLetterEndpointUri" value="jbi:service:urn:test:deadLetter-service?mep=in-only"/>
+    <property name="redeliveryPolicy" ref="redeliveryPolicyConfig" />
+  </bean>
+    
+  <bean id="redeliveryPolicyConfig" class="org.apache.camel.processor.RedeliveryPolicy">
+    <property name="maximumRedeliveries" value="3" />
+    <property name="initialRedeliveryDelay" value="2000" />
+    <property name="useExponentialBackOff" value="true" />
+    <property name="backOffMultiplier" value="2" />
+  </bean>
+
+  <bean id="handleFaultProcessor" class="org.apache.servicemix.camel.su9.HandleFaultProcessor" />
+
+</beans>

Propchange: servicemix/components/engines/servicemix-camel/trunk/src/test/resources/org/apache/servicemix/camel/su9-src/camel-context.xml
------------------------------------------------------------------------------
    svn:eol-style = native