Author: davsclaus
Date: Sat Jun 30 17:16:58 2012
New Revision: 1355774

URL: http://svn.apache.org/viewvc?rev=1355774&view=rev
Log:
CAMEL-5406: Fixed mock endpoints interceptor to support async routing engine, 
to ensure callback is properly called if async kicks in.

Added:
    
camel/trunk/camel-core/src/test/java/org/apache/camel/issues/ThreadsDoTryCatchInterceptSendToAllEndpointIssueTest.java
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToEndpoint.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToEndpoint.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToEndpoint.java?rev=1355774&r1=1355773&r2=1355774&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToEndpoint.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToEndpoint.java
 Sat Jun 30 17:16:58 2012
@@ -18,6 +18,8 @@ package org.apache.camel.impl;
 
 import java.util.Map;
 
+import org.apache.camel.AsyncCallback;
+import org.apache.camel.AsyncProcessor;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Consumer;
 import org.apache.camel.Endpoint;
@@ -27,6 +29,8 @@ import org.apache.camel.ExchangePattern;
 import org.apache.camel.PollingConsumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
+import org.apache.camel.util.AsyncProcessorConverterHelper;
+import org.apache.camel.util.AsyncProcessorHelper;
 import org.apache.camel.util.ServiceHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -96,7 +100,7 @@ public class InterceptSendToEndpoint imp
 
     public Producer createProducer() throws Exception {
         producer = delegate.createProducer();
-        return new Producer() {
+        return new DefaultAsyncProducer(delegate) {
 
             public Endpoint getEndpoint() {
                 return producer.getEndpoint();
@@ -114,7 +118,8 @@ public class InterceptSendToEndpoint imp
                 return producer.createExchange(exchange);
             }
 
-            public void process(Exchange exchange) throws Exception {
+            @Override
+            public boolean process(Exchange exchange, AsyncCallback callback) {
                 // process the detour so we do the detour routing
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("Sending to endpoint: {} is intercepted and 
detoured to: {} for exchange: {}", new Object[]{getEndpoint(), detour, 
exchange});
@@ -122,16 +127,20 @@ public class InterceptSendToEndpoint imp
                 // add header with the real endpoint uri
                 exchange.getIn().setHeader(Exchange.INTERCEPTED_ENDPOINT, 
delegate.getEndpointUri());
 
+                // detour the exchange using synchronous processing
                 try {
                     detour.process(exchange);
                 } catch (Exception e) {
                     exchange.setException(e);
+                    callback.done(true);
+                    return true;
                 }
 
                 // Decide whether to continue or not; similar logic to the 
Pipeline
                 // check for error if so we should break out
                 if (!continueProcessing(exchange, "skip sending to original 
intended destination: " + getEndpoint(), LOG)) {
-                    return;
+                    callback.done(true);
+                    return true;
                 }
 
                 // determine if we should skip or not
@@ -150,10 +159,15 @@ public class InterceptSendToEndpoint imp
                         exchange.setOut(null);
                     }
 
-                    // route to original destination
-                    producer.process(exchange);
+                    // route to original destination leveraging the 
asynchronous routing engine
+                    AsyncProcessor async = 
AsyncProcessorConverterHelper.convert(producer);
+                    return AsyncProcessorHelper.process(async, exchange, 
callback);
                 } else {
-                    LOG.debug("Stop() means skip sending exchange to original 
intended destination: {} for exchange: {}", getEndpoint(), exchange);
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Stop() means skip sending exchange to 
original intended destination: {} for exchange: {}", getEndpoint(), exchange);
+                    }
+                    callback.done(true);
+                    return true;
                 }
             }
 

Added: 
camel/trunk/camel-core/src/test/java/org/apache/camel/issues/ThreadsDoTryCatchInterceptSendToAllEndpointIssueTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/ThreadsDoTryCatchInterceptSendToAllEndpointIssueTest.java?rev=1355774&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/issues/ThreadsDoTryCatchInterceptSendToAllEndpointIssueTest.java
 (added)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/issues/ThreadsDoTryCatchInterceptSendToAllEndpointIssueTest.java
 Sat Jun 30 17:16:58 2012
@@ -0,0 +1,64 @@
+/**
+ * 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.issues;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.InterceptSendToMockEndpointStrategy;
+
+/**
+ *
+ */
+public class ThreadsDoTryCatchInterceptSendToAllEndpointIssueTest extends 
ContextTestSupport {
+
+    public void testThreadsTryCatch() throws Exception {
+        getMockEndpoint("mock:log:try").expectedMessageCount(1);
+        getMockEndpoint("mock:log:catch").expectedMessageCount(1);
+        getMockEndpoint("mock:log:world").expectedMessageCount(1);
+        getMockEndpoint("mock:log:other").expectedMessageCount(0);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        // mock all endpoints
+        context.addRegisterEndpointCallback(new 
InterceptSendToMockEndpointStrategy("*"));
+
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .threads()
+                    .doTry()
+                        .to("log:try")
+                        .throwException(new IllegalArgumentException("Forced"))
+                    .doCatch(Exception.class)
+                        .to("log:catch")
+                        .choice()
+                            .when(body().contains("World"))
+                                .to("log:world").stop()
+                            .otherwise()
+                                .to("log:other").stop()
+                        .end()
+                    .end();
+            }
+        };
+    }
+}


Reply via email to