From b70c8d1cb2869d89500cb1db6ad89a6831ea33bc Mon Sep 17 00:00:00 2001 From: "Adrian T. Co" Date: Wed, 4 Jan 2006 08:48:41 +0000 Subject: [PATCH] Re-included the means to prevent the main thread from exiting until the vm is exited elsewhere (either Ctrl+C, terminateJVM, etc.) git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@365865 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/activemq/broker/Main.java | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/Main.java b/activemq-core/src/main/java/org/apache/activemq/broker/Main.java index 1414868494..2d90fd3e93 100755 --- a/activemq-core/src/main/java/org/apache/activemq/broker/Main.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/Main.java @@ -111,9 +111,10 @@ public class Main { QUERY_TYPE_ID_MAP.setProperty("Topic", "Destination"); }; - private final ArrayList extensions = new ArrayList(); - private final Map queryObjects = new HashMap(); - private final List queryViews = new ArrayList(); + private final List brokers = new ArrayList(); + private final List extensions = new ArrayList(); + private final Map queryObjects = new HashMap(); + private final List queryViews = new ArrayList(); private int taskType = TASK_NONE; private boolean stopAll = false; @@ -444,6 +445,8 @@ public class Main { this.startBroker(this.getConfigUri()); // } } + + waitForShutdown(); } protected void taskStopBrokers(List brokerNames) throws Throwable { @@ -649,6 +652,37 @@ public class Main { } } + public void waitForShutdown() throws Throwable { + // Prevent the main thread from exiting, in case this is the last user thread + final boolean[] shutdown = new boolean[]{false}; + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + synchronized(shutdown) { + shutdown[0]=true; + shutdown.notify(); + } + } + }); + synchronized(shutdown) { + while( !shutdown[0] ) { + shutdown.wait(); + } + } + + // Use reflection to stop the broker in case, the vm was exited via unconventional means + try { + for (Iterator i=brokers.iterator(); i.hasNext();) { + Object broker = i.next(); + Method stop = broker.getClass().getMethod("stop", new Class[] {}); + stop.invoke(broker, new Object[] {}); + } + } catch (InvocationTargetException e) { + throw e.getCause(); + } catch (Throwable e) { + throw e; + } + } + public void stopBroker(MBeanServerConnection server, String brokerName) { ObjectName brokerObjName = null; try {