Repository: camel Updated Branches: refs/heads/master 5c13e0d10 -> 6c862043a
CAMEL-7324 Fixed the DynamicRouter prepare exchange issue with thanks to Rene Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/6c862043 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/6c862043 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/6c862043 Branch: refs/heads/master Commit: 6c862043a12f2306711a25b4ac37fb20b2ec077e Parents: 5c13e0d Author: Willem Jiang <willem.ji...@gmail.com> Authored: Fri Mar 28 22:48:00 2014 +0800 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Sun Mar 30 15:05:55 2014 +0800 ---------------------------------------------------------------------- .../org/apache/camel/processor/RoutingSlip.java | 16 ++-- .../camel/processor/DynamicRouterTest.java | 3 +- .../async/AsyncEndpointDynamicRouterTest.java | 83 ++++++++++++++++++++ .../processor/SpringDynamicRouterTest.xml | 1 - 4 files changed, 91 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/6c862043/camel-core/src/main/java/org/apache/camel/processor/RoutingSlip.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/processor/RoutingSlip.java b/camel-core/src/main/java/org/apache/camel/processor/RoutingSlip.java index d4cf126..99b9069 100644 --- a/camel-core/src/main/java/org/apache/camel/processor/RoutingSlip.java +++ b/camel-core/src/main/java/org/apache/camel/processor/RoutingSlip.java @@ -201,11 +201,10 @@ public class RoutingSlip extends ServiceSupport implements AsyncProcessor, Trace break; } - // prepare and process the routing slip - Exchange copy = prepareExchangeForRoutingSlip(current, endpoint); - boolean sync = processExchange(endpoint, copy, exchange, callback, iter); - current = copy; - + //process and prepare the routing slip + boolean sync = processExchange(endpoint, current, exchange, callback, iter); + current = prepareExchangeForRoutingSlip(current, endpoint); + if (!sync) { log.trace("Processing exchangeId: {} is continued being processed asynchronously", exchange.getExchangeId()); // the remainder of the routing slip will be completed async @@ -298,7 +297,7 @@ public class RoutingSlip extends ServiceSupport implements AsyncProcessor, Trace } // continue processing the routing slip asynchronously - Exchange current = exchange; + Exchange current = prepareExchangeForRoutingSlip(exchange, endpoint); while (iter.hasNext(current)) { @@ -333,9 +332,8 @@ public class RoutingSlip extends ServiceSupport implements AsyncProcessor, Trace } // prepare and process the routing slip - Exchange copy = prepareExchangeForRoutingSlip(current, endpoint); - boolean sync = processExchange(endpoint, copy, original, callback, iter); - current = copy; + boolean sync = processExchange(endpoint, current, original, callback, iter); + current = prepareExchangeForRoutingSlip(current, endpoint); if (!sync) { log.trace("Processing exchangeId: {} is continued being processed asynchronously", original.getExchangeId()); http://git-wip-us.apache.org/repos/asf/camel/blob/6c862043/camel-core/src/test/java/org/apache/camel/processor/DynamicRouterTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/processor/DynamicRouterTest.java b/camel-core/src/test/java/org/apache/camel/processor/DynamicRouterTest.java index c18ed46..84884bf 100644 --- a/camel-core/src/test/java/org/apache/camel/processor/DynamicRouterTest.java +++ b/camel-core/src/test/java/org/apache/camel/processor/DynamicRouterTest.java @@ -34,7 +34,6 @@ public class DynamicRouterTest extends ContextTestSupport { getMockEndpoint("mock:a").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:b").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:c").expectedBodiesReceived("Hello World"); - getMockEndpoint("mock:foo").expectedBodiesReceived("Bye World"); getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); template.sendBody("direct:start", "Hello World"); @@ -61,7 +60,7 @@ public class DynamicRouterTest extends ContextTestSupport { .dynamicRouter(method(DynamicRouterTest.class, "slip")); // END SNIPPET: e1 - from("direct:foo").transform(constant("Bye World")).to("mock:foo"); + from("direct:foo").transform(constant("Bye World")); } }; } http://git-wip-us.apache.org/repos/asf/camel/blob/6c862043/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointDynamicRouterTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointDynamicRouterTest.java b/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointDynamicRouterTest.java new file mode 100644 index 0000000..ad3bff9 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointDynamicRouterTest.java @@ -0,0 +1,83 @@ +/** + * 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.async; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; + +public class AsyncEndpointDynamicRouterTest extends ContextTestSupport { + + private static int invoked; + private static List<String> bodies = new ArrayList<String>(); + + public void testAsyncEndpoint() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); + + String reply = template.requestBody("direct:start", "Hello Camel", String.class); + assertEquals("Bye World", reply); + + assertMockEndpointsSatisfied(); + + assertEquals(4, invoked); + assertEquals(4, bodies.size()); + assertEquals("Hello Camel", bodies.get(0)); + assertEquals("Bye Camel", bodies.get(1)); + assertEquals("Bye World", bodies.get(2)); + assertEquals("Bye World", bodies.get(3)); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + context.addComponent("async", new MyAsyncComponent()); + + from("direct:start") + .dynamicRouter(method(AsyncEndpointDynamicRouterTest.class, "slip")); + + from("direct:foo").transform(constant("Bye World")); + } + }; + } + + /** + * Use this method to compute dynamic where we should route next. + * + * @param body the message body + * @return endpoints to go, or <tt>null</tt> to indicate the end + */ + public String slip(String body) { + bodies.add(body); + invoked++; + + if (invoked == 1) { + return "async:bye:camel"; + } else if (invoked == 2) { + return "direct:foo"; + } else if (invoked == 3) { + return "mock:result"; + } + + // no more so return null + return null; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/6c862043/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringDynamicRouterTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringDynamicRouterTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringDynamicRouterTest.xml index 994a3a0..8fc822c 100644 --- a/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringDynamicRouterTest.xml +++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringDynamicRouterTest.xml @@ -37,7 +37,6 @@ <route> <from uri="direct:foo"/> <transform><constant>Bye World</constant></transform> - <to uri="mock:foo"/> </route> </camelContext>