diff --git a/activemq-core/src/main/java/org/apache/activemq/xbean/XBeanBrokerService.java b/activemq-core/src/main/java/org/apache/activemq/xbean/XBeanBrokerService.java
index f07c72b44b..446445a8f4 100644
--- a/activemq-core/src/main/java/org/apache/activemq/xbean/XBeanBrokerService.java
+++ b/activemq-core/src/main/java/org/apache/activemq/xbean/XBeanBrokerService.java
@@ -22,6 +22,7 @@ import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
+import org.springframework.context.ConfigurableApplicationContext;
/**
* An ActiveMQ Message Broker. It consists of a number of transport
@@ -41,6 +42,7 @@ public class XBeanBrokerService extends BrokerService implements InitializingBea
private boolean start = true;
private ApplicationContext applicationContext = null;
+ private boolean destroyApplicationContextOnShutdown = false;
public XBeanBrokerService() {
}
@@ -49,6 +51,15 @@ public class XBeanBrokerService extends BrokerService implements InitializingBea
if (start) {
start();
}
+ if (destroyApplicationContextOnShutdown) {
+ addShutdownHook(new Runnable() {
+ public void run() {
+ if (applicationContext instanceof ConfigurableApplicationContext) {
+ ((ConfigurableApplicationContext) applicationContext).close();
+ }
+ }
+ });
+ }
}
public void destroy() throws Exception {
@@ -68,6 +79,14 @@ public class XBeanBrokerService extends BrokerService implements InitializingBea
this.start = start;
}
+ /**
+ * Sets whether the broker should shutdown the ApplicationContext when the broker is stopped.
+ * The broker can be stopped because the underlying JDBC store is unavailable for example.
+ */
+ public void setDestroyApplicationContextOnShutdown(boolean destroy) {
+ this.destroyApplicationContextOnShutdown = destroy;
+ }
+
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
diff --git a/activemq-core/src/main/resources/activemq.xsd b/activemq-core/src/main/resources/activemq.xsd
index 8b4e98118f..83dbb6bea6 100644
--- a/activemq-core/src/main/resources/activemq.xsd
+++ b/activemq-core/src/main/resources/activemq.xsd
@@ -824,6 +824,14 @@ with JUnit tests you may wish to start and stop the broker explicitly yourself.
]]>
+