From e41be74339c49749727db15ec751324869fc6951 Mon Sep 17 00:00:00 2001 From: Robert Davies Date: Tue, 18 Mar 2008 06:45:25 +0000 Subject: [PATCH] Use ThreadPool for stopping services git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@638238 13f79535-47bb-0310-9956-ffa450edef68 --- .../simple/SimpleDiscoveryAgent.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/activemq-core/src/main/java/org/apache/activemq/transport/discovery/simple/SimpleDiscoveryAgent.java b/activemq-core/src/main/java/org/apache/activemq/transport/discovery/simple/SimpleDiscoveryAgent.java index 22b6e7372c..6547fa8263 100755 --- a/activemq-core/src/main/java/org/apache/activemq/transport/discovery/simple/SimpleDiscoveryAgent.java +++ b/activemq-core/src/main/java/org/apache/activemq/transport/discovery/simple/SimpleDiscoveryAgent.java @@ -18,6 +18,10 @@ package org.apache.activemq.transport.discovery.simple; import java.io.IOException; import java.net.URI; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import org.apache.activemq.command.DiscoveryEvent; @@ -35,7 +39,7 @@ import org.apache.commons.logging.LogFactory; public class SimpleDiscoveryAgent implements DiscoveryAgent { private final static Log LOG = LogFactory.getLog(SimpleDiscoveryAgent.class); - + private static final ThreadPoolExecutor ASYNC_TASKS; private long initialReconnectDelay = 1000; private long maxReconnectDelay = 1000 * 30; private long backOffMultiplier = 2; @@ -106,7 +110,7 @@ public class SimpleDiscoveryAgent implements DiscoveryAgent { if (event.failed.compareAndSet(false, true)) { listener.onServiceRemove(event); - Thread thread = new Thread() { + ASYNC_TASKS.execute(new Runnable() { public void run() { // We detect a failed connection attempt because the service @@ -160,9 +164,7 @@ public class SimpleDiscoveryAgent implements DiscoveryAgent { listener.onServiceAdd(event); } - }; - thread.setDaemon(true); - thread.start(); + }); } } @@ -213,5 +215,16 @@ public class SimpleDiscoveryAgent implements DiscoveryAgent { public void setUseExponentialBackOff(boolean useExponentialBackOff) { this.useExponentialBackOff = useExponentialBackOff; } + + static { + ASYNC_TASKS = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 30, TimeUnit.SECONDS, new SynchronousQueue(), new ThreadFactory() { + public Thread newThread(Runnable runnable) { + Thread thread = new Thread(runnable, "Simple Discovery Agent: "+runnable); + thread.setDaemon(true); + return thread; + } + }); + } + }