This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-2.20.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.20.x by this push:
     new 94254f8  CAMEL-12131: ProducerCache should not store non-singleton/non 
pooled producers on CamelContext.
94254f8 is described below

commit 94254f82a3f980664677055b476b2457d127cc6b
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Wed Jan 10 14:11:03 2018 +0100

    CAMEL-12131: ProducerCache should not store non-singleton/non pooled 
producers on CamelContext.
---
 .../java/org/apache/camel/impl/ProducerCache.java  | 12 ++-
 .../org/apache/camel/processor/SendProcessor.java  |  4 +-
 .../camel/impl/ProducerCacheNonSingletonTest.java  | 97 ++++++++++++++++++++++
 3 files changed, 108 insertions(+), 5 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java 
b/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java
index 0df400e..cb3ddae 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java
@@ -571,9 +571,15 @@ public class ProducerCache extends ServiceSupport {
             // create a new producer
             try {
                 answer = endpoint.createProducer();
-                // add as service which will also start the service
-                // (false => we and handling the lifecycle of the producer in 
this cache)
-                getCamelContext().addService(answer, false);
+                // add as service to CamelContext so its managed via JMX
+                boolean add = answer.isSingleton() || answer instanceof 
ServicePoolAware;
+                if (add) {
+                    // (false => we and handling the lifecycle of the producer 
in this cache)
+                    getCamelContext().addService(answer, false);
+                } else {
+                    // fallback and start producer manually
+                    ServiceHelper.startService(answer);
+                }
             } catch (Throwable e) {
                 throw new FailedToCreateProducerException(endpoint, e);
             }
diff --git 
a/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java 
b/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java
index d933b59..35ceb21 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java
@@ -241,8 +241,8 @@ public class SendProcessor extends ServiceSupport 
implements AsyncProcessor, Tra
         ServiceHelper.startService(destination);
 
         // this SendProcessor is used a lot in Camel (eg every .to in the 
route DSL) and therefore we
-        // want to optimize for regular producers, by using the producer 
directly instead of the ProducerCache
-        // Only for pooled and non singleton producers we have to use the 
ProducerCache as it supports these
+        // want to optimize for regular producers, by using the producer 
directly instead of the ProducerCache.
+        // Only for pooled and non-singleton producers we have to use the 
ProducerCache as it supports these
         // kind of producer better (though these kind of producer should be 
rare)
 
         Producer producer = producerCache.acquireProducer(destination);
diff --git 
a/camel-core/src/test/java/org/apache/camel/impl/ProducerCacheNonSingletonTest.java
 
b/camel-core/src/test/java/org/apache/camel/impl/ProducerCacheNonSingletonTest.java
new file mode 100644
index 0000000..d588cbc
--- /dev/null
+++ 
b/camel-core/src/test/java/org/apache/camel/impl/ProducerCacheNonSingletonTest.java
@@ -0,0 +1,97 @@
+/**
+ * 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.impl;
+
+import java.util.Map;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+
+public class ProducerCacheNonSingletonTest extends ContextTestSupport {
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    public void testNonSingleton() throws Exception {
+        context.addComponent("dummy", new MyDummyComponent());
+
+        ProducerCache cache = new ProducerCache(this, context);
+        cache.start();
+
+        Endpoint endpoint = context.getEndpoint("dummy:foo");
+        DefaultProducer producer = (DefaultProducer) 
cache.acquireProducer(endpoint);
+        assertNotNull(producer);
+        assertTrue("Should be started", producer.getStatus().isStarted());
+
+        Object found = context.hasService(MyDummyProducer.class);
+        assertNull("Should not store producer on CamelContext", found);
+
+        cache.releaseProducer(endpoint, producer);
+        assertTrue("Should be stopped", producer.getStatus().isStopped());
+
+        cache.stop();
+    }
+
+    public class MyDummyComponent extends DefaultComponent {
+
+        @Override
+        protected Endpoint createEndpoint(String uri, String remaining, 
Map<String, Object> parameters) throws Exception {
+            return new MyDummyEndpoint();
+        }
+    }
+
+    public class MyDummyEndpoint extends DefaultEndpoint {
+
+        @Override
+        public Producer createProducer() throws Exception {
+            return new MyDummyProducer(this);
+        }
+
+        @Override
+        public Consumer createConsumer(Processor processor) throws Exception {
+            return null;
+        }
+
+        @Override
+        public boolean isSingleton() {
+            return false;
+        }
+
+        @Override
+        protected String createEndpointUri() {
+            return "dummy://foo";
+        }
+    }
+
+    private class MyDummyProducer extends DefaultProducer {
+
+        public MyDummyProducer(Endpoint endpoint) {
+            super(endpoint);
+        }
+
+        @Override
+        public void process(Exchange exchange) throws Exception {
+            // noop
+        }
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
['"commits@camel.apache.org" <commits@camel.apache.org>'].

Reply via email to