Repository: camel Updated Branches: refs/heads/camel-2.17.x 8ad61d39d -> 646b6a107 refs/heads/camel-2.18.x 7ae4a18bd -> df5e6a675 refs/heads/master 70f29ced5 -> 33615d381
CAMEL-10406: vm endpoint should prepare exchange with the CamelContext from the consumer and not from cached endpoint which can be different. Thanks to Xavier Fournet for the unit test. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/33615d38 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/33615d38 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/33615d38 Branch: refs/heads/master Commit: 33615d3811a5034f6156cf81f01fedec45c4ce00 Parents: 70f29ce Author: Claus Ibsen <davscl...@apache.org> Authored: Sat Oct 29 21:54:15 2016 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sat Oct 29 21:54:15 2016 +0200 ---------------------------------------------------------------------- .../apache/camel/component/vm/VmConsumer.java | 33 +++++++++++- .../vm/VmShouldUseConsumerContext.java | 54 ++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/33615d38/camel-core/src/main/java/org/apache/camel/component/vm/VmConsumer.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/vm/VmConsumer.java b/camel-core/src/main/java/org/apache/camel/component/vm/VmConsumer.java index a110ba4..ef4666c 100644 --- a/camel-core/src/main/java/org/apache/camel/component/vm/VmConsumer.java +++ b/camel-core/src/main/java/org/apache/camel/component/vm/VmConsumer.java @@ -16,12 +16,43 @@ */ package org.apache.camel.component.vm; +import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; +import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.component.seda.SedaConsumer; +import org.apache.camel.util.ExchangeHelper; -public class VmConsumer extends SedaConsumer { +public class VmConsumer extends SedaConsumer implements CamelContextAware { + + private CamelContext camelContext; public VmConsumer(VmEndpoint endpoint, Processor processor) { super(endpoint, processor); } + + @Override + public CamelContext getCamelContext() { + return camelContext; + } + + @Override + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + /** + * Strategy to prepare exchange for being processed by this consumer + * + * @param exchange the exchange + * @return the exchange to process by this consumer. + */ + protected Exchange prepareExchange(Exchange exchange) { + // send a new copied exchange with the camel context from this consumer + Exchange newExchange = ExchangeHelper.copyExchangeAndSetCamelContext(exchange, getCamelContext()); + // set the from endpoint + newExchange.setFromEndpoint(getEndpoint()); + return newExchange; + } + } http://git-wip-us.apache.org/repos/asf/camel/blob/33615d38/camel-core/src/test/java/org/apache/camel/component/vm/VmShouldUseConsumerContext.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/vm/VmShouldUseConsumerContext.java b/camel-core/src/test/java/org/apache/camel/component/vm/VmShouldUseConsumerContext.java new file mode 100644 index 0000000..2babebe --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/vm/VmShouldUseConsumerContext.java @@ -0,0 +1,54 @@ +/** + * 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.vm; + +import org.apache.camel.builder.RouteBuilder; + +public class VmShouldUseConsumerContext extends AbstractVmTestSupport { + + public void testConsumerContext() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); + + String out = template2.requestBody("direct:start", "Hello World", String.class); + assertEquals("Hello World", out); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // using "toD" instead of "to" make use of the exchange context to resolve the direct endpoint at route execution. + // if the exchange context is not the excepted one, the toD will fail + from("vm:test").toD("direct:process"); + from("direct:process").to("mock:result"); + } + }; + } + + @Override + protected RouteBuilder createRouteBuilderForSecondContext() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").to("vm:test"); + } + }; + } +} \ No newline at end of file