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 2010/05/27 15:04:36 UTC

svn commit: r948819 - in /servicemix/components/engines/servicemix-camel/trunk/src: main/java/org/apache/servicemix/camel/JbiEndpoint.java test/java/org/apache/servicemix/camel/CamelConcurrentProducerCreationTest.java

Author: gertv
Date: Thu May 27 13:04:36 2010
New Revision: 948819

URL: http://svn.apache.org/viewvc?rev=948819&view=rev
Log:
SMXCOMP-751: Ensure that multiple threads can safely use the JBI endpoint Producer

Added:
    servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/CamelConcurrentProducerCreationTest.java
Modified:
    servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiEndpoint.java

Modified: servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiEndpoint.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiEndpoint.java?rev=948819&r1=948818&r2=948819&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiEndpoint.java (original)
+++ servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiEndpoint.java Thu May 27 13:04:36 2010
@@ -51,8 +51,6 @@ public class JbiEndpoint extends Default
 
     private QName operation;
 
-    private JbiProducer producer;
-
     private boolean convertExceptions;
 
     private String serialization;
@@ -79,11 +77,8 @@ public class JbiEndpoint extends Default
         return result;
     }
 
-    public synchronized Producer createProducer() throws Exception {
-        if (producer == null) {
-            producer = new JbiProducer(this);
-        }
-        return producer;
+    public Producer createProducer() throws Exception {
+        return new JbiProducer(this);
     }
 
     protected class JbiProducer extends DefaultProducer {

Added: servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/CamelConcurrentProducerCreationTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/CamelConcurrentProducerCreationTest.java?rev=948819&view=auto
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/CamelConcurrentProducerCreationTest.java (added)
+++ servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/CamelConcurrentProducerCreationTest.java Thu May 27 13:04:36 2010
@@ -0,0 +1,96 @@
+/*
+ * 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 java.util.concurrent.Callable;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.Message;
+import org.apache.camel.Producer;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.servicemix.jbi.container.ActivationSpec;
+import org.apache.servicemix.jbi.container.JBIContainer;
+import org.apache.servicemix.tck.mock.MockMessageExchange;
+
+/**
+ * Test cases to ensure multiple threads can safely create an use the {@link org.apache.camel.Producer}
+ * for our JBI Camel endpoints
+ */
+public class CamelConcurrentProducerCreationTest extends JbiTestSupport {
+
+    private static final int COUNT = 1000;
+
+    public void testConcurrentlyCreateProducers() throws Exception {
+        final MockEndpoint mock = getMockEndpoint("mock:test");
+        mock.expectedMessageCount(COUNT);
+
+        ExecutorService executor = Executors.newFixedThreadPool(25);
+
+        for (int i = 0 ; i < COUNT ; i++) {
+            executor.submit(new Callable<Object>() {
+                public Object call() throws Exception {
+                    doSendExchangeWithProducer("<message />");
+                    return null;
+                }
+            });
+        }
+
+        mock.assertIsSatisfied(30000);
+    }
+
+    private void doSendExchangeWithProducer(String body) throws Exception {
+        String uri ="jbi:endpoint:urn:test:service:endpoint";
+        Endpoint endpoint = camelContext.getEndpoint(uri);
+
+        Exchange exchange = endpoint.createExchange(ExchangePattern.InOnly);
+        Message in = exchange.getIn();
+        in.setBody(body);
+
+        Producer producer = null;
+        try {
+            producer = endpoint.createProducer();
+            producer.start();
+            producer.process(exchange);
+        } finally {
+            if (producer != null) {
+                producer.stop();
+            }
+        }
+    }
+
+    @Override
+    protected RouteBuilder createRoutes() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("jbi:endpoint:urn:test:service:endpoint").to("mock:test");
+            }
+        };
+    }
+
+    @Override
+    protected void appendJbiActivationSpecs(List<ActivationSpec> activationSpecList) {
+        // no additional JBI activation specs required
+    }
+}
\ No newline at end of file