This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 3e8b368 CAMEL-12131: ProducerCache should not store non-singleton/non pooled producers on CamelContext. 3e8b368 is described below commit 3e8b368357623245161d12a962d7b74ea275d99a 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 ++++++++++++++++++++++ .../camel/component/jt400/Jt400PgmProducer.java | 5 +- 4 files changed, 109 insertions(+), 9 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 + } + } +} diff --git a/components/camel-jt400/src/main/java/org/apache/camel/component/jt400/Jt400PgmProducer.java b/components/camel-jt400/src/main/java/org/apache/camel/component/jt400/Jt400PgmProducer.java index efcd59a..f089424 100644 --- a/components/camel-jt400/src/main/java/org/apache/camel/component/jt400/Jt400PgmProducer.java +++ b/components/camel-jt400/src/main/java/org/apache/camel/component/jt400/Jt400PgmProducer.java @@ -18,22 +18,19 @@ package org.apache.camel.component.jt400; import java.beans.PropertyVetoException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; + import com.ibm.as400.access.AS400; import com.ibm.as400.access.AS400ByteArray; import com.ibm.as400.access.AS400DataType; import com.ibm.as400.access.AS400Message; import com.ibm.as400.access.AS400Text; -import com.ibm.as400.access.CommandCall; import com.ibm.as400.access.ProgramCall; import com.ibm.as400.access.ProgramParameter; import com.ibm.as400.access.ServiceProgramCall; import org.apache.camel.Exchange; import org.apache.camel.InvalidPayloadException; import org.apache.camel.impl.DefaultProducer; -import org.apache.commons.lang3.ArrayUtils; -import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -- To stop receiving notification emails like this one, please contact ['"commits@camel.apache.org" <commits@camel.apache.org>'].