You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2012/06/15 13:57:11 UTC

svn commit: r1350589 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/directvm/ main/resources/META-INF/services/org/apache/camel/component/ test/java/org/apache/camel/component/directvm/

Author: davsclaus
Date: Fri Jun 15 11:57:10 2012
New Revision: 1350589

URL: http://svn.apache.org/viewvc?rev=1350589&view=rev
Log:
CAMEL-5370: Added direct-vm component to act as synchronous direct calls between multiple camel contexts in the same JVM (eg like direct + vm together). Can be used to support transactions spanning multiple camel contextes / bundles.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmComponent.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmConsumer.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmEndpoint.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmProcessor.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmProducer.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/package.html
      - copied, changed from r1350519, camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/package.html
    camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/direct-vm
      - copied, changed from r1350519, camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/direct
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/AbstractDirectVmTestSupport.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmShouldUseSameThreadTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmTwoCamelContextDuplicateConsumerTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmTwoCamelContextTest.java

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmComponent.java?rev=1350589&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmComponent.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmComponent.java Fri Jun 15 11:57:10 2012
@@ -0,0 +1,89 @@
+/**
+ * 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.directvm;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.impl.DefaultComponent;
+
+/**
+ * Represents the component that manages {@link DirectVmEndpoint}. It holds the
+ * list of named direct-vm endpoints.
+ */
+public class DirectVmComponent extends DefaultComponent {
+
+    private static final AtomicInteger START_COUNTER = new AtomicInteger();
+
+    // must keep a map of consumers on the component to ensure endpoints can lookup old consumers
+    // later in case the DirectEndpoint was re-created due the old was evicted from the endpoints LRUCache
+    // on DefaultCamelContext
+    private static final ConcurrentMap<String, DirectVmConsumer> consumers = new ConcurrentHashMap<String, DirectVmConsumer>();
+
+    @Override
+    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
+        DirectVmEndpoint answer = new DirectVmEndpoint(uri, this);
+        answer.configureProperties(parameters);
+        return answer;
+    }
+
+    public DirectVmConsumer getConsumer(DirectVmEndpoint endpoint) {
+        String key = getConsumerKey(endpoint.getEndpointUri());
+        return consumers.get(key);
+    }
+
+    public void addConsumer(DirectVmEndpoint endpoint, DirectVmConsumer consumer) {
+        String key = getConsumerKey(endpoint.getEndpointUri());
+        DirectVmConsumer existing = consumers.putIfAbsent(key, consumer);
+        if (existing != null) {
+            String contextId = existing.getEndpoint().getCamelContext().getName();
+            throw new IllegalStateException("A consumer " + existing + " already exists from CamelContext: " + contextId + ". Multiple consumers not supported");
+        }
+        consumers.put(key, consumer);
+    }
+
+    public void removeConsumer(DirectVmEndpoint endpoint, DirectVmConsumer consumer) {
+        String key = getConsumerKey(endpoint.getEndpointUri());
+        consumers.remove(key);
+    }
+
+    private static String getConsumerKey(String uri) {
+        if (uri.contains("?")) {
+            // strip parameters
+            uri = uri.substring(0, uri.indexOf('?'));
+        }
+        return uri;
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        START_COUNTER.incrementAndGet();
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        if (START_COUNTER.decrementAndGet() <= 0) {
+            // clear queues when no more direct-vm components in use
+            consumers.clear();
+        }
+    }
+
+}

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmConsumer.java?rev=1350589&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmConsumer.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmConsumer.java Fri Jun 15 11:57:10 2012
@@ -0,0 +1,48 @@
+/**
+ * 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.directvm;
+
+import org.apache.camel.Processor;
+import org.apache.camel.impl.DefaultConsumer;
+
+/**
+ * The direct-vm consumer
+ */
+public class DirectVmConsumer extends DefaultConsumer {
+
+    public DirectVmConsumer(DirectVmEndpoint endpoint, Processor processor) {
+        super(endpoint, processor);
+    }
+
+    @Override
+    public DirectVmEndpoint getEndpoint() {
+        return (DirectVmEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        getEndpoint().getComponent().addConsumer(getEndpoint(), this);
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        getEndpoint().getComponent().removeConsumer(getEndpoint(), this);
+        super.doStop();
+    }
+
+}

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmEndpoint.java?rev=1350589&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmEndpoint.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmEndpoint.java Fri Jun 15 11:57:10 2012
@@ -0,0 +1,57 @@
+/**
+ * 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.directvm;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.impl.DefaultEndpoint;
+
+/**
+ * The direct-vm endpoint.
+ */
+public class DirectVmEndpoint extends DefaultEndpoint {
+
+    public DirectVmEndpoint(String endpointUri, DirectVmComponent component) {
+        super(endpointUri, component);
+    }
+
+    @Override
+    public DirectVmComponent getComponent() {
+        return (DirectVmComponent) super.getComponent();
+    }
+
+    @Override
+    public Producer createProducer() throws Exception {
+        return new DirectVmProducer(this);
+    }
+
+    @Override
+    public Consumer createConsumer(Processor processor) throws Exception {
+        return new DirectVmConsumer(this, new DirectVmProcessor(processor, this));
+    }
+
+    @Override
+    public boolean isSingleton() {
+        return true;
+    }
+
+    public DirectVmConsumer getConsumer() {
+        return getComponent().getConsumer(this);
+    }
+
+}

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmProcessor.java?rev=1350589&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmProcessor.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmProcessor.java Fri Jun 15 11:57:10 2012
@@ -0,0 +1,66 @@
+/**
+ * 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.directvm;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.processor.DelegateProcessor;
+import org.apache.camel.util.ExchangeHelper;
+
+/**
+*
+*/
+public final class DirectVmProcessor extends DelegateProcessor {
+
+    private final DirectVmEndpoint endpoint;
+
+    public DirectVmProcessor(Processor processor, DirectVmEndpoint endpoint) {
+        super(processor);
+        this.endpoint = endpoint;
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        // need to use a copy of the incoming exchange, so we route using this camel context
+        Exchange copy = prepareExchange(exchange);
+        try {
+            getProcessor().process(copy);
+        } finally {
+            // make sure to copy results back
+            ExchangeHelper.copyResults(exchange, copy);
+        }
+    }
+
+    /**
+     * 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 new camel context
+        Exchange newExchange = ExchangeHelper.copyExchangeAndSetCamelContext(exchange, endpoint.getCamelContext());
+        // set the from endpoint
+        newExchange.setFromEndpoint(endpoint);
+        return newExchange;
+    }
+
+    @Override
+    public String toString() {
+        return "DirectVm[" + processor + "]";
+    }
+}

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmProducer.java?rev=1350589&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmProducer.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmProducer.java Fri Jun 15 11:57:10 2012
@@ -0,0 +1,48 @@
+/**
+ * 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.directvm;
+
+import org.apache.camel.CamelExchangeException;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultProducer;
+
+/**
+ * The direct-vm producer
+ */
+public class DirectVmProducer extends DefaultProducer {
+
+    public DirectVmProducer(DirectVmEndpoint endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    public DirectVmEndpoint getEndpoint() {
+        return (DirectVmEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        // send to consumer
+        DirectVmConsumer consumer = getEndpoint().getComponent().getConsumer(getEndpoint());
+        if (consumer == null) {
+            log.warn("No consumers available on endpoint: " + getEndpoint() + " to process: " + exchange);
+            throw new CamelExchangeException("No consumers available on endpoint: " + getEndpoint(), exchange);
+        } else {
+            consumer.getProcessor().process(exchange);
+        }
+    }
+}

Copied: camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/package.html (from r1350519, camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/package.html)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/package.html?p2=camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/package.html&p1=camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/package.html&r1=1350519&r2=1350589&rev=1350589&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/package.html (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/package.html Fri Jun 15 11:57:10 2012
@@ -19,8 +19,12 @@
 </head>
 <body>
 
-The <a href="http://camel.apache.org/direct.html">Direct Component</a> which synchronously invokes
-all the consumers when a producer sends an exchange to the endpoint.  This also known as <i>strait through processing</i>. 
+The <a href="http://camel.apache.org/directvm.html">Direct VM Component</a> which synchronously invokes
+the consumer when a producer sends an exchange to the endpoint.  This also known as <i>strait through processing</i>.
+<p/>
+This component supports messaging within the current JVM; so across CamelContext instances.
+Note that this communication can only take place between ClassLoaders which share the same camel-core.jar.
+So to communicate across web applications you need to put camel-core.jar on the system/boot classpath.
 
 </body>
 </html>

Copied: camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/direct-vm (from r1350519, camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/direct)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/direct-vm?p2=camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/direct-vm&p1=camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/direct&r1=1350519&r2=1350589&rev=1350589&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/direct (original)
+++ camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/direct-vm Fri Jun 15 11:57:10 2012
@@ -15,4 +15,4 @@
 # limitations under the License.
 #
 
-class=org.apache.camel.component.direct.DirectComponent
+class=org.apache.camel.component.directvm.DirectVmComponent

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/AbstractDirectVmTestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/AbstractDirectVmTestSupport.java?rev=1350589&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/AbstractDirectVmTestSupport.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/AbstractDirectVmTestSupport.java Fri Jun 15 11:57:10 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.component.directvm;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.util.ServiceHelper;
+import org.junit.After;
+import org.junit.Before;
+
+/**
+ *
+ */
+public class AbstractDirectVmTestSupport extends ContextTestSupport {
+    protected CamelContext context2;
+    protected ProducerTemplate template2;
+
+    @Override
+    @Before
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        context2 = new DefaultCamelContext();
+        template2 = context2.createProducerTemplate();
+
+        ServiceHelper.startServices(template2, context2);
+
+        // add routes after CamelContext has been started
+        RouteBuilder routeBuilder = createRouteBuilderForSecondContext();
+        if (routeBuilder != null) {
+            context2.addRoutes(routeBuilder);
+        }
+    }
+
+    @Override
+    @After
+    protected void tearDown() throws Exception {
+        ServiceHelper.stopServices(context2, template2);
+
+        super.tearDown();
+    }
+
+    protected RouteBuilder createRouteBuilderForSecondContext() throws Exception {
+        return null;
+    }
+
+}

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmShouldUseSameThreadTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmShouldUseSameThreadTest.java?rev=1350589&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmShouldUseSameThreadTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmShouldUseSameThreadTest.java Fri Jun 15 11:57:10 2012
@@ -0,0 +1,63 @@
+/**
+ * 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.directvm;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ *
+ */
+public class DirectVmShouldUseSameThreadTest extends ContextTestSupport {
+
+    private static long id;
+
+    public void testUseSameThread() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+
+        template.sendBody("direct-vm:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                final ThreadLocal<String> local = new ThreadLocal<String>();
+
+                from("direct-vm:start").process(new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        local.set("Hello");
+                        id = Thread.currentThread().getId();
+                    }
+                }).to("direct-vm:foo");
+
+                from("direct-vm:foo").process(new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        assertEquals("Hello", local.get());
+                        assertEquals(id, Thread.currentThread().getId());
+                    }
+                }).to("mock:result");
+            }
+        };
+    }
+
+}
\ No newline at end of file

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmTwoCamelContextDuplicateConsumerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmTwoCamelContextDuplicateConsumerTest.java?rev=1350589&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmTwoCamelContextDuplicateConsumerTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmTwoCamelContextDuplicateConsumerTest.java Fri Jun 15 11:57:10 2012
@@ -0,0 +1,92 @@
+/**
+ * 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.directvm;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+
+/**
+ *
+ */
+public class DirectVmTwoCamelContextDuplicateConsumerTest extends AbstractDirectVmTestSupport {
+
+    public void testThirdClash() throws Exception {
+        CamelContext third = new DefaultCamelContext();
+        third.addRoutes(createRouteBuilderForThirdContext());
+        try {
+            third.start();
+            fail("Should have thrown exception");
+        } catch (IllegalStateException e) {
+            assertEquals("A consumer Consumer[direct-vm://foo] already exists from CamelContext: camel-1. Multiple consumers not supported", e.getMessage());
+        }
+
+        // stop first camel context then
+        context.stop();
+
+        // and start the 3rd which should work now
+        third.start();
+
+        MockEndpoint mock = third.getEndpoint("mock:third", MockEndpoint.class);
+        mock.expectedMessageCount(1);
+
+        template2.sendBody("direct:start", "Hello World");
+
+        mock.assertIsSatisfied();
+
+        third.stop();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct-vm:foo")
+                    .transform(constant("Bye World"))
+                    .log("Running on Camel ${camelId} on thread ${threadName} with message ${body}")
+                    .to("mock:result");
+            }
+        };
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilderForSecondContext() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .log("Running on Camel ${camelId} on thread ${threadName} with message ${body}")
+                    .to("direct-vm:foo");
+            }
+        };
+    }
+
+    protected RouteBuilder createRouteBuilderForThirdContext() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct-vm:foo")
+                    .transform(constant("Bye World"))
+                    .log("Running on Camel ${camelId} on thread ${threadName} with message ${body}")
+                    .to("mock:third");
+            }
+        };
+    }
+
+}

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmTwoCamelContextTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmTwoCamelContextTest.java?rev=1350589&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmTwoCamelContextTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmTwoCamelContextTest.java Fri Jun 15 11:57:10 2012
@@ -0,0 +1,60 @@
+/**
+ * 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.directvm;
+
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ *
+ */
+public class DirectVmTwoCamelContextTest extends AbstractDirectVmTestSupport {
+
+    public void testTwoCamelContext() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
+
+        String out = template2.requestBody("direct:start", "Hello World", String.class);
+        assertEquals("Bye World", out);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct-vm:foo")
+                    .transform(constant("Bye World"))
+                    .log("Running on Camel ${camelId} on thread ${threadName} with message ${body}")
+                    .to("mock:result");
+            }
+        };
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilderForSecondContext() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .log("Running on Camel ${camelId} on thread ${threadName} with message ${body}")
+                    .to("direct-vm:foo");
+            }
+        };
+    }
+
+}