Updated Branches: refs/heads/camel-2.12.x 845388c3c -> 256577e8c
CAMEL-6948: Releasing a non-singleton producer by ProducerCache should not only stop the producer but also shutdown it as well if it is a ShutdownableService. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/256577e8 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/256577e8 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/256577e8 Branch: refs/heads/camel-2.12.x Commit: 256577e8caacc9d1fc9fb65f0ff1587b1bf47ef7 Parents: 845388c Author: Babak Vahdat <bvah...@apache.org> Authored: Fri Nov 8 21:45:59 2013 +0100 Committer: Babak Vahdat <bvah...@apache.org> Committed: Fri Nov 8 21:47:21 2013 +0100 ---------------------------------------------------------------------- .../org/apache/camel/impl/ProducerCache.java | 6 +++ .../camel/impl/DefaultProducerCacheTest.java | 53 +++++++++++++++++--- 2 files changed, 51 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/256577e8/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java ---------------------------------------------------------------------- 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 6b292c0..b35eca5 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 @@ -30,6 +30,7 @@ import org.apache.camel.Processor; import org.apache.camel.Producer; import org.apache.camel.ProducerCallback; import org.apache.camel.ServicePoolAware; +import org.apache.camel.ShutdownableService; import org.apache.camel.processor.UnitOfWorkProducer; import org.apache.camel.spi.ServicePool; import org.apache.camel.support.ServiceSupport; @@ -137,6 +138,11 @@ public class ProducerCache extends ServiceSupport { } else if (!producer.isSingleton()) { // stop non singleton producers as we should not leak resources producer.stop(); + + // shutdown as well in case the producer is shutdownable + if (producer instanceof ShutdownableService) { + ShutdownableService.class.cast(producer).shutdown(); + } } } http://git-wip-us.apache.org/repos/asf/camel/blob/256577e8/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerCacheTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerCacheTest.java b/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerCacheTest.java index 5ad0f42..803302c 100644 --- a/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerCacheTest.java +++ b/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerCacheTest.java @@ -30,7 +30,8 @@ import org.apache.camel.Producer; */ public class DefaultProducerCacheTest extends ContextTestSupport { - private static final AtomicInteger COUNTER = new AtomicInteger(); + private final AtomicInteger stopCounter = new AtomicInteger(); + private final AtomicInteger shutdownCounter = new AtomicInteger(); public void testCacheProducerAcquireAndRelease() throws Exception { ProducerCache cache = new ProducerCache(this, context); @@ -56,7 +57,7 @@ public class DefaultProducerCacheTest extends ContextTestSupport { assertEquals("Size should be 0", 0, cache.size()); for (int i = 0; i < 8; i++) { - Endpoint e = new MyEndpoint(i); + Endpoint e = new MyEndpoint(true, i); Producer p = cache.acquireProducer(e); cache.releaseProducer(e, p); } @@ -64,19 +65,50 @@ public class DefaultProducerCacheTest extends ContextTestSupport { assertEquals("Size should be 5", 5, cache.size()); // should have stopped the 3 evicted - assertEquals(3, COUNTER.get()); + assertEquals(3, stopCounter.get()); cache.stop(); // should have stopped all 8 - assertEquals(8, COUNTER.get()); + assertEquals(8, stopCounter.get()); + } + + public void testReleaseProducerInvokesStopAndShutdownByNonSingeltonProducers() throws Exception { + ProducerCache cache = new ProducerCache(this, context, 1); + cache.start(); + + assertEquals("Size should be 0", 0, cache.size()); + + for (int i = 0; i < 3; i++) { + Endpoint e = new MyEndpoint(false, i); + Producer p = cache.acquireProducer(e); + cache.releaseProducer(e, p); + } + + assertEquals("Size should be 0", 0, cache.size()); + + // should have stopped all 3 + assertEquals(3, stopCounter.get()); + + // should have shutdown all 3 + assertEquals(3, shutdownCounter.get()); + + cache.stop(); + + // no more stop after stopping the cache + assertEquals(3, stopCounter.get()); + + // no more shutdown after stopping the cache + assertEquals(3, shutdownCounter.get()); } private final class MyEndpoint extends DefaultEndpoint { - private int number; + private final boolean isSingleton; + private final int number; - private MyEndpoint(int number) { + private MyEndpoint(boolean isSingleton, int number) { + this.isSingleton = isSingleton; this.number = number; } @@ -92,7 +124,7 @@ public class DefaultProducerCacheTest extends ContextTestSupport { @Override public boolean isSingleton() { - return true; + return isSingleton; } @Override @@ -114,7 +146,12 @@ public class DefaultProducerCacheTest extends ContextTestSupport { @Override protected void doStop() throws Exception { - COUNTER.incrementAndGet(); + stopCounter.incrementAndGet(); + } + + @Override + protected void doShutdown() throws Exception { + shutdownCounter.incrementAndGet(); } }