added the ability to get a callback when the broker is shutdown.  Also added an option to System.exit when the broker is shutdown.




git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@655639 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Hiram R. Chirino 2008-05-12 20:54:31 +00:00
parent 56b6d3174a
commit b4f65bafac
5 changed files with 109 additions and 12 deletions

View File

@ -104,6 +104,7 @@ public class Main {
try { try {
app.runTaskClass(tokens); app.runTaskClass(tokens);
System.exit(0);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
System.out.println("Could not load class: " + e.getMessage()); System.out.println("Could not load class: " + e.getMessage());
try { try {
@ -114,8 +115,10 @@ public class Main {
} }
} catch (MalformedURLException e1) { } catch (MalformedURLException e1) {
} }
System.exit(1);
} catch (Throwable e) { } catch (Throwable e) {
System.out.println("Failed to execute main task. Reason: " + e); System.out.println("Failed to execute main task. Reason: " + e);
System.exit(1);
} }
} }

View File

@ -22,6 +22,7 @@ import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.activemq.broker.BrokerFactory; import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.BrokerService;
@ -93,11 +94,14 @@ public class StartCommand extends AbstractCommand {
// Prevent the main thread from exiting unless it is terminated // Prevent the main thread from exiting unless it is terminated
// elsewhere // elsewhere
waitForShutdown();
} catch (Exception e) { } catch (Exception e) {
context.printException(new RuntimeException("Failed to execute start task. Reason: " + e, e)); context.printException(new RuntimeException("Failed to execute start task. Reason: " + e, e));
throw new Exception(e); throw new Exception(e);
} }
// The broker start up fine. If this unblocks it's cause they were stopped
// and this would occur because of an internal error (like the DB going offline)
waitForShutdown();
} }
/** /**
@ -122,15 +126,34 @@ public class StartCommand extends AbstractCommand {
final boolean[] shutdown = new boolean[] { final boolean[] shutdown = new boolean[] {
false false
}; };
Runtime.getRuntime().addShutdownHook(new Thread() { Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() { public void run() {
synchronized (shutdown) { for (Iterator<BrokerService> i = brokers.iterator(); i.hasNext();) {
shutdown[0] = true; try {
shutdown.notify(); BrokerService broker = i.next();
broker.stop();
} catch (Exception e) {
}
} }
} }
}); });
final AtomicInteger brokerCounter = new AtomicInteger(brokers.size());
for (BrokerService bs : brokers) {
bs.addShutdownHook(new Runnable() {
public void run() {
// When the last broker lets us know he is closed....
if( brokerCounter.decrementAndGet() == 0 ) {
synchronized (shutdown) {
shutdown[0] = true;
shutdown.notify();
}
}
}
});
}
// Wait for any shutdown event // Wait for any shutdown event
synchronized (shutdown) { synchronized (shutdown) {
while (!shutdown[0]) { while (!shutdown[0]) {
@ -141,11 +164,6 @@ public class StartCommand extends AbstractCommand {
} }
} }
// Stop each broker
for (Iterator<BrokerService> i = brokers.iterator(); i.hasNext();) {
BrokerService broker = i.next();
broker.stop();
}
} }
/** /**

View File

@ -170,7 +170,9 @@ public class BrokerService implements Service {
private boolean dedicatedTaskRunner; private boolean dedicatedTaskRunner;
private boolean cacheTempDestinations=false;//useful for failover private boolean cacheTempDestinations=false;//useful for failover
private int timeBeforePurgeTempDestinations = 5000; private int timeBeforePurgeTempDestinations = 5000;
private List<Runnable> shutdownHooks= new ArrayList<Runnable>();
private boolean systemExitOnShutdown;
private int systemExitOnShutdownExitCode;
static { static {
String localHostName = "localhost"; String localHostName = "localhost";
@ -425,6 +427,15 @@ public class BrokerService implements Service {
} }
try { try {
if( systemExitOnShutdown ) {
addShutdownHook(new Runnable(){
public void run() {
System.exit(systemExitOnShutdownExitCode);
}
});
}
processHelperProperties(); processHelperProperties();
BrokerRegistry.getInstance().bind(getBrokerName(), this); BrokerRegistry.getInstance().bind(getBrokerName(), this);
@ -510,6 +521,15 @@ public class BrokerService implements Service {
stopped.set(true); stopped.set(true);
stoppedLatch.countDown(); stoppedLatch.countDown();
LOG.info("ActiveMQ JMS Message Broker (" + getBrokerName() + ", " + brokerId + ") stopped"); LOG.info("ActiveMQ JMS Message Broker (" + getBrokerName() + ", " + brokerId + ") stopped");
synchronized(shutdownHooks) {
for (Runnable hook : shutdownHooks) {
try {
hook.run();
} catch ( Throwable e ) {
stopper.onException(hook, e);
}
}
}
stopper.throwFirstException(); stopper.throwFirstException();
} }
@ -1912,4 +1932,25 @@ public class BrokerService implements Service {
this.regionBroker = regionBroker; this.regionBroker = regionBroker;
} }
public void addShutdownHook(Runnable hook) {
synchronized(shutdownHooks) {
shutdownHooks.add(hook);
}
}
public void removeShutdownHook(Runnable hook) {
synchronized(shutdownHooks) {
shutdownHooks.remove(hook);
}
}
public boolean isSystemExitOnShutdown() {
return systemExitOnShutdown;
}
public void setSystemExitOnShutdown(boolean systemExitOnShutdown) {
this.systemExitOnShutdown = systemExitOnShutdown;
}
} }

View File

@ -473,7 +473,7 @@ public class JDBCPersistenceAdapter extends DataSourceSupport implements Persist
try { try {
brokerService.stop(); brokerService.stop();
} catch (Exception e) { } catch (Exception e) {
LOG.warn("Failed to stop broker"); LOG.warn("Failure occured while stopping broker");
} }
} }

View File

@ -52,6 +52,9 @@ public class BrokerFactoryBean implements FactoryBean, InitializingBean, Disposa
private ResourceXmlApplicationContext context; private ResourceXmlApplicationContext context;
private ApplicationContext parentContext; private ApplicationContext parentContext;
private boolean systemExitOnShutdown;
private int systemExitOnShutdownExitCode;
public BrokerFactoryBean() { public BrokerFactoryBean() {
} }
@ -101,6 +104,14 @@ public class BrokerFactoryBean implements FactoryBean, InitializingBean, Disposa
if (broker == null) { if (broker == null) {
throw new IllegalArgumentException("The configuration has no BrokerService instance for resource: " + config); throw new IllegalArgumentException("The configuration has no BrokerService instance for resource: " + config);
} }
if( systemExitOnShutdown ) {
broker.addShutdownHook(new Runnable(){
public void run() {
System.exit(systemExitOnShutdownExitCode);
}
});
}
if (start) { if (start) {
broker.start(); broker.start();
} }
@ -135,4 +146,28 @@ public class BrokerFactoryBean implements FactoryBean, InitializingBean, Disposa
this.start = start; this.start = start;
} }
public boolean isSystemExitOnStop() {
return systemExitOnShutdown;
}
public void setSystemExitOnStop(boolean systemExitOnStop) {
this.systemExitOnShutdown = systemExitOnStop;
}
public boolean isSystemExitOnShutdown() {
return systemExitOnShutdown;
}
public void setSystemExitOnShutdown(boolean systemExitOnShutdown) {
this.systemExitOnShutdown = systemExitOnShutdown;
}
public int getSystemExitOnShutdownExitCode() {
return systemExitOnShutdownExitCode;
}
public void setSystemExitOnShutdownExitCode(int systemExitOnShutdownExitCode) {
this.systemExitOnShutdownExitCode = systemExitOnShutdownExitCode;
}
} }