Author: davsclaus Date: Wed Apr 7 07:29:39 2010 New Revision: 931456 URL: http://svn.apache.org/viewvc?rev=931456&view=rev Log: CAMEL-2558: Creating producer and consumer template do not throw checked exception. Added to set capacity using setter on ServicePool.
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CamelContextSharedProducerServicePoolTest.java (with props) Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerServicePool.java camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultServicePool.java camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MainSupport.java camel/trunk/camel-core/src/main/java/org/apache/camel/impl/SharedProducerServicePool.java camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ServicePool.java camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MainSupportTest.java camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java?rev=931456&r1=931455&r2=931456&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java Wed Apr 7 07:29:39 2010 @@ -473,9 +473,9 @@ public interface CamelContext extends Se * before starting the template. * * @return the template - * @throws Exception is thrown if error starting the template + * @throws RuntimeCamelException is thrown if error starting the template */ - ProducerTemplate createProducerTemplate() throws Exception; + ProducerTemplate createProducerTemplate(); /** * Creates a new {...@link ProducerTemplate} which is <b>started</b> and therefore ready to use right away. @@ -487,9 +487,9 @@ public interface CamelContext extends Se * * @param maximumCacheSize the maximum cache size * @return the template - * @throws Exception is thrown if error starting the template + * @throws RuntimeCamelException is thrown if error starting the template */ - ProducerTemplate createProducerTemplate(int maximumCacheSize) throws Exception; + ProducerTemplate createProducerTemplate(int maximumCacheSize); /** * Creates a new {...@link ConsumerTemplate} which is <b>started</b> and therefore ready to use right away. @@ -503,9 +503,9 @@ public interface CamelContext extends Se * before starting the template. * * @return the template - * @throws Exception is thrown if error starting the template + * @throws RuntimeCamelException is thrown if error starting the template */ - ConsumerTemplate createConsumerTemplate() throws Exception; + ConsumerTemplate createConsumerTemplate(); /** * Creates a new {...@link ConsumerTemplate} which is <b>started</b> and therefore ready to use right away. @@ -515,9 +515,9 @@ public interface CamelContext extends Se * * @param maximumCacheSize the maximum cache size * @return the template - * @throws Exception is thrown if error starting the template + * @throws RuntimeCamelException is thrown if error starting the template */ - ConsumerTemplate createConsumerTemplate(int maximumCacheSize) throws Exception; + ConsumerTemplate createConsumerTemplate(int maximumCacheSize); /** * Adds the given interceptor strategy Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=931456&r1=931455&r2=931456&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Wed Apr 7 07:29:39 2010 @@ -149,12 +149,13 @@ public class DefaultCamelContext extends private ClassResolver classResolver = new DefaultClassResolver(); private PackageScanClassResolver packageScanClassResolver; // we use a capacity of 100 per endpoint, so for the same endpoint we have at most 100 producers in the pool - // so if we have 6 endpoints in the pool, we have 6 x 100 producers in total + // so if we have 6 endpoints in the pool, we can have 6 x 100 producers in total private ServicePool<Endpoint, Producer> producerServicePool = new SharedProducerServicePool(100); private NodeIdFactory nodeIdFactory = new DefaultNodeIdFactory(); private Tracer defaultTracer; private InflightRepository inflightRepository = new DefaultInflightRepository(); private final List<RouteStartupOrder> routeStartupOrder = new ArrayList<RouteStartupOrder>(); + // start auto assigning route ids using numbering 1000 and upwards private int defaultRouteStartupOrder = 1000; private ShutdownStrategy shutdownStrategy = new DefaultShutdownStrategy(this); private ShutdownRoute shutdownRoute = ShutdownRoute.Default; @@ -937,29 +938,37 @@ public class DefaultCamelContext extends this.delay = delay; } - public ProducerTemplate createProducerTemplate() throws Exception { + public ProducerTemplate createProducerTemplate() { int size = CamelContextHelper.getMaximumCachePoolSize(this); return createProducerTemplate(size); } - public ProducerTemplate createProducerTemplate(int maximumCacheSize) throws Exception { + public ProducerTemplate createProducerTemplate(int maximumCacheSize) { DefaultProducerTemplate answer = new DefaultProducerTemplate(this); answer.setMaximumCacheSize(maximumCacheSize); // start it so its ready to use - answer.start(); + try { + answer.start(); + } catch (Exception e) { + throw ObjectHelper.wrapRuntimeCamelException(e); + } return answer; } - public ConsumerTemplate createConsumerTemplate() throws Exception { + public ConsumerTemplate createConsumerTemplate() { int size = CamelContextHelper.getMaximumCachePoolSize(this); return createConsumerTemplate(size); } - public ConsumerTemplate createConsumerTemplate(int maximumCacheSize) throws Exception { + public ConsumerTemplate createConsumerTemplate(int maximumCacheSize) { DefaultConsumerTemplate answer = new DefaultConsumerTemplate(this); answer.setMaximumCacheSize(maximumCacheSize); // start it so its ready to use - answer.start(); + try { + answer.start(); + } catch (Exception e) { + throw ObjectHelper.wrapRuntimeCamelException(e); + } return answer; } Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerServicePool.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerServicePool.java?rev=931456&r1=931455&r2=931456&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerServicePool.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerServicePool.java Wed Apr 7 07:29:39 2010 @@ -29,6 +29,9 @@ import org.apache.camel.Producer; */ public class DefaultProducerServicePool extends DefaultServicePool<Endpoint, Producer> { + public DefaultProducerServicePool() { + } + public DefaultProducerServicePool(int capacity) { super(capacity); } Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultServicePool.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultServicePool.java?rev=931456&r1=931455&r2=931456&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultServicePool.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultServicePool.java Wed Apr 7 07:29:39 2010 @@ -35,17 +35,23 @@ import org.apache.commons.logging.LogFac public abstract class DefaultServicePool<Key, Service> extends ServiceSupport implements ServicePool<Key, Service> { protected final Log log = LogFactory.getLog(getClass()); protected final ConcurrentHashMap<Key, BlockingQueue<Service>> pool = new ConcurrentHashMap<Key, BlockingQueue<Service>>(); - protected final int capacity; + protected int capacity = 100; + + protected DefaultServicePool() { + } - /** - * The capacity, note this is per key. - * - * @param capacity the capacity per key. - */ public DefaultServicePool(int capacity) { this.capacity = capacity; } + public int getCapacity() { + return capacity; + } + + public void setCapacity(int capacity) { + this.capacity = capacity; + } + public synchronized int size() { int size = 0; for (BlockingQueue<Service> entry : pool.values()) { Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MainSupport.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MainSupport.java?rev=931456&r1=931455&r2=931456&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MainSupport.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MainSupport.java Wed Apr 7 07:29:39 2010 @@ -330,7 +330,7 @@ public abstract class MainSupport extend return camelTemplate; } - protected abstract ProducerTemplate findOrCreateCamelTemplate() throws Exception; + protected abstract ProducerTemplate findOrCreateCamelTemplate(); protected abstract Map<String, CamelContext> getCamelContextMap(); Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/SharedProducerServicePool.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/SharedProducerServicePool.java?rev=931456&r1=931455&r2=931456&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/SharedProducerServicePool.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/SharedProducerServicePool.java Wed Apr 7 07:29:39 2010 @@ -26,6 +26,9 @@ import org.apache.camel.ShutdownableServ */ public class SharedProducerServicePool extends DefaultProducerServicePool implements ShutdownableService { + public SharedProducerServicePool() { + } + public SharedProducerServicePool(int capacity) { super(capacity); } Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ServicePool.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ServicePool.java?rev=931456&r1=931455&r2=931456&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ServicePool.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ServicePool.java Wed Apr 7 07:29:39 2010 @@ -24,17 +24,34 @@ package org.apache.camel.spi; * <p/> * Notice the capacity is <b>per key</b> which means that each key can contain at most * (the capacity) services. The pool can contain an unbounded number of keys. + * <p/> + * By default the capacity is set to 100. * * @version $Revision$ */ public interface ServicePool<Key, Service> { /** + * Sets the capacity, which is capacity <b>per key</b>. + * + * @param capacity the capacity per key + */ + void setCapacity(int capacity); + + /** + * Gets the capacity per key. + * + * @return the capacity per key + */ + int getCapacity(); + + /** * Adds the given service to the pool and acquires it. * * @param key the key * @param service the service * @return the acquired service, is newer <tt>null</tt> + * @throws IllegalStateException if the queue is full (capacity has been reached) */ Service addAndAcquire(Key key, Service service); Added: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CamelContextSharedProducerServicePoolTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CamelContextSharedProducerServicePoolTest.java?rev=931456&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CamelContextSharedProducerServicePoolTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CamelContextSharedProducerServicePoolTest.java Wed Apr 7 07:29:39 2010 @@ -0,0 +1,114 @@ +/** + * 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 org.apache.camel.ContextTestSupport; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Producer; +import org.apache.camel.ServicePoolAware; +import org.apache.camel.spi.ServicePool; + +/** + * @version $Revision$ + */ +public class CamelContextSharedProducerServicePoolTest extends ContextTestSupport { + + private class MyProducer extends DefaultProducer implements ServicePoolAware { + + private boolean start; + private boolean stop; + + public MyProducer(Endpoint endpoint) throws Exception { + super(endpoint); + start(); + } + + public void process(Exchange exchange) throws Exception { + // noop + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + assertEquals("Should not be started twice", false, start); + start = true; + } + + @Override + protected void doStop() throws Exception { + super.doStop(); + assertEquals("Should not be stopped twice", false, stop); + stop = true; + } + } + + public void testSharedProducerServicePool() throws Exception { + // the default capacity + assertEquals(100, context.getProducerServicePool().getCapacity()); + + // change it + context.getProducerServicePool().setCapacity(25); + assertEquals(25, context.getProducerServicePool().getCapacity()); + } + + public void testSharedProducerServicePoolHitMax() throws Exception { + // the default capacity + assertEquals(100, context.getProducerServicePool().getCapacity()); + + // change it + ServicePool<Endpoint, Producer> pool = context.getProducerServicePool(); + pool.setCapacity(3); + assertEquals(3, pool.getCapacity()); + + Endpoint endpoint = context.getEndpoint("mock:foo"); + + assertNull(pool.acquire(endpoint)); + assertEquals(0, pool.size()); + + Producer producer = new MyProducer(endpoint); + producer = pool.addAndAcquire(endpoint, producer); + assertEquals(0, pool.size()); + + Producer producer2 = new MyProducer(endpoint); + producer2 = pool.addAndAcquire(endpoint, producer2); + assertEquals(0, pool.size()); + + Producer producer3 = new MyProducer(endpoint); + producer3 = pool.addAndAcquire(endpoint, producer3); + assertEquals(0, pool.size()); + + pool.release(endpoint, producer); + assertEquals(1, pool.size()); + + pool.release(endpoint, producer2); + assertEquals(2, pool.size()); + + pool.release(endpoint, producer3); + assertEquals(3, pool.size()); + + Producer producer4 = new MyProducer(endpoint); + try { + producer4 = pool.addAndAcquire(endpoint, producer4); + fail("Should throw an exception"); + } catch (IllegalStateException e) { + assertEquals("Queue full", e.getMessage()); + } + assertEquals(3, pool.size()); + } + +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CamelContextSharedProducerServicePoolTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CamelContextSharedProducerServicePoolTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java?rev=931456&r1=931455&r2=931456&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java Wed Apr 7 07:29:39 2010 @@ -76,6 +76,13 @@ public class CustomProducerServicePoolTe private Producer producer; + public void setCapacity(int capacity) { + } + + public int getCapacity() { + return 0; + } + public Producer addAndAcquire(Endpoint endpoint, Producer producer) { if (endpoint instanceof MyEndpoint) { return producer; Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MainSupportTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MainSupportTest.java?rev=931456&r1=931455&r2=931456&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MainSupportTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MainSupportTest.java Wed Apr 7 07:29:39 2010 @@ -31,7 +31,7 @@ public class MainSupportTest extends Con private class MyMainSupport extends MainSupport { - protected ProducerTemplate findOrCreateCamelTemplate() throws Exception { + protected ProducerTemplate findOrCreateCamelTemplate() { return context.createProducerTemplate(); } Modified: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java?rev=931456&r1=931455&r2=931456&view=diff ============================================================================== --- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java (original) +++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java Wed Apr 7 07:29:39 2010 @@ -85,7 +85,6 @@ public class Main extends MainSupport { return instance; } - // Properties // ------------------------------------------------------------------------- protected void setInjector(Injector injector) { @@ -129,7 +128,7 @@ public class Main extends MainSupport { } } - protected ProducerTemplate findOrCreateCamelTemplate() throws Exception { + protected ProducerTemplate findOrCreateCamelTemplate() { if (injector != null) { Set<ProducerTemplate> set = Injectors.getInstancesOf(injector, ProducerTemplate.class); if (!set.isEmpty()) { Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java?rev=931456&r1=931455&r2=931456&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java (original) +++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java Wed Apr 7 07:29:39 2010 @@ -186,7 +186,7 @@ public class Main extends MainSupport { } } - protected ProducerTemplate findOrCreateCamelTemplate() throws Exception { + protected ProducerTemplate findOrCreateCamelTemplate() { String[] names = getApplicationContext().getBeanNamesForType(ProducerTemplate.class); if (names != null && names.length > 0) { return (ProducerTemplate) getApplicationContext().getBean(names[0], ProducerTemplate.class);