Title: [738] trunk/core/src/main/java/org/servicemix: Fix SM-143: Shutdown does not stop the jbi container

Diff

Modified: trunk/core/src/main/java/org/servicemix/Main.java (737 => 738)

--- trunk/core/src/main/java/org/servicemix/Main.java	2005-11-03 15:44:37 UTC (rev 737)
+++ trunk/core/src/main/java/org/servicemix/Main.java	2005-11-03 19:12:43 UTC (rev 738)
@@ -18,6 +18,8 @@
 package org.servicemix;
 
 import org.servicemix.jbi.config.spring.XBeanProcessor;
+import org.servicemix.jbi.container.SpringJBIContainer;
+import org.springframework.beans.factory.DisposableBean;
 import org.springframework.context.ApplicationContext;
 import org.xbean.spring.context.ClassPathXmlApplicationContext;
 import org.xbean.spring.context.FileSystemXmlApplicationContext;
@@ -74,13 +76,17 @@
                     context = new FileSystemXmlApplicationContext(file);
                 }
             }
-            context.getBean("jbi");
+            SpringJBIContainer container = (SpringJBIContainer) context.getBean("jbi");
+            Object lock = new Object();
+            container.setShutdownLock(lock);
 
             // lets wait until we're killed.
-            Object lock = new Object();
             synchronized (lock) {
                 lock.wait();
             }
+            if (context instanceof DisposableBean) {
+                ((DisposableBean) context).destroy();
+            }
         }
         catch (Exception e) {
             System.out.println("Caught: " + e);

Modified: trunk/core/src/main/java/org/servicemix/jbi/container/EnvironmentContext.java (737 => 738)

--- trunk/core/src/main/java/org/servicemix/jbi/container/EnvironmentContext.java	2005-11-03 15:44:37 UTC (rev 737)
+++ trunk/core/src/main/java/org/servicemix/jbi/container/EnvironmentContext.java	2005-11-03 19:12:43 UTC (rev 738)
@@ -29,6 +29,7 @@
 import org.servicemix.jbi.util.FileUtil;
 
 import javax.jbi.JBIException;
+import javax.jbi.management.LifeCycleMBean;
 import javax.management.JMException;
 import javax.management.MBeanAttributeInfo;
 
@@ -60,6 +61,7 @@
     private boolean dumpStats = false;
     private Timer statsTimer;
     private TimerTask timerTask;
+    private JBIContainer container;
 
 
     /**
@@ -150,9 +152,11 @@
      * @param container
      * @param rootDirPath
      */
-    public void init(JBIContainer container, String rootDirPath) {
+    public void init(JBIContainer container, String rootDirPath) throws JBIException {
+        this.container = container;
         jbiRootDir = new File(rootDirPath + File.separator + container.getName());
         buildDirectoryStructure();
+        container.getManagementContext().registerSystemService(this, LifeCycleMBean.class);
     }
 
     /**
@@ -199,6 +203,7 @@
             statsTimer.cancel();
         }
         envMap.clear();
+        container.getManagementContext().unregisterMBean(this);
     }
 
     /**

Modified: trunk/core/src/main/java/org/servicemix/jbi/container/JBIContainer.java (737 => 738)

--- trunk/core/src/main/java/org/servicemix/jbi/container/JBIContainer.java	2005-11-03 15:44:37 UTC (rev 737)
+++ trunk/core/src/main/java/org/servicemix/jbi/container/JBIContainer.java	2005-11-03 19:12:43 UTC (rev 738)
@@ -38,7 +38,6 @@
 import org.servicemix.jbi.framework.ComponentMBeanImpl;
 import org.servicemix.jbi.framework.ComponentNameSpace;
 import org.servicemix.jbi.framework.DeploymentService;
-import org.servicemix.jbi.framework.FrameworkInstallationService;
 import org.servicemix.jbi.framework.InstallationService;
 import org.servicemix.jbi.framework.LocalComponentConnector;
 import org.servicemix.jbi.framework.Registry;
@@ -55,7 +54,6 @@
 import javax.jbi.component.ComponentLifeCycle;
 import javax.jbi.component.ServiceUnitManager;
 import javax.jbi.management.DeploymentException;
-import javax.jbi.management.DeploymentServiceMBean;
 import javax.jbi.management.LifeCycleMBean;
 import javax.jbi.messaging.MessagingException;
 import javax.jbi.servicedesc.ServiceEndpoint;
@@ -401,12 +399,6 @@
             autoDeployService.init(this);
             // register self with the ManagementContext
             managementContext.registerSystemService(this, LifeCycleMBean.class, getName());
-            managementContext.registerSystemService(environmentContext, LifeCycleMBean.class);
-            managementContext.registerSystemService(installationService, FrameworkInstallationService.class);
-            managementContext.registerSystemService(deploymentService, DeploymentServiceMBean.class);
-            managementContext.registerSystemService(autoDeployService, LifeCycleMBean.class);
-            managementContext.registerSystemService(registry, LifeCycleMBean.class);
-            managementContext.registerSystemService(broker, LifeCycleMBean.class);
             log.info("ServiceMix JBI Container (http://servicemix.org/) name: " + getName() + " running version: "
                     + EnvironmentContext.getVersion());
         }
@@ -460,11 +452,13 @@
             autoDeployService.shutDown();
             registry.shutDown();
             broker.shutDown();
-            managementContext.shutDown();
             environmentContext.shutDown();
             installationService.shutDown();
             deploymentService.shutDown();
+            // shutdown the management context last, because it will close the mbean server 
             super.shutDown();
+            managementContext.unregisterMBean(this);
+            managementContext.shutDown();
         }
     }
 
@@ -680,12 +674,7 @@
             LocalComponentConnector lcc = registry.getLocalComponentConnector(cns);
             if (lcc != null) {
                 environmentContext.unreregister(lcc, true);
-                try {
-                    managementContext.unregisterMBean(lcc.getMbeanName());
-                }
-                catch (JMException e) {
-                    throw new JBIException(e);
-                }
+                managementContext.unregisterMBean(lcc.getMbeanName());
             }
             registry.deregisterComponent(component);
             log.info("Deactivating component for name: " + id + " component: " + component);

Modified: trunk/core/src/main/java/org/servicemix/jbi/container/SpringJBIContainer.java (737 => 738)

--- trunk/core/src/main/java/org/servicemix/jbi/container/SpringJBIContainer.java	2005-11-03 15:44:37 UTC (rev 737)
+++ trunk/core/src/main/java/org/servicemix/jbi/container/SpringJBIContainer.java	2005-11-03 19:12:43 UTC (rev 738)
@@ -33,6 +33,7 @@
     private BeanFactory beanFactory;
     private ApplicationContext applicationContext;
     private String[] deployArchives;
+    private Object shutdownLock;
 
     public void afterPropertiesSet() throws Exception {
         init();
@@ -165,7 +166,19 @@
 	}
 
     public void destroy() throws Exception {
-        shutDown();
+        super.shutDown();
     }
 
+    public void shutDown() throws JBIException {
+        if (shutdownLock != null) {
+            synchronized (shutdownLock) {
+                shutdownLock.notify();
+            }
+        }
+    }
+
+    public void setShutdownLock(Object lock) {
+        this.shutdownLock = lock;
+    }
+
 }

Modified: trunk/core/src/main/java/org/servicemix/jbi/framework/AutoDeploymentService.java (737 => 738)

--- trunk/core/src/main/java/org/servicemix/jbi/framework/AutoDeploymentService.java	2005-11-03 15:44:37 UTC (rev 737)
+++ trunk/core/src/main/java/org/servicemix/jbi/framework/AutoDeploymentService.java	2005-11-03 19:12:43 UTC (rev 738)
@@ -36,6 +36,7 @@
 
 import javax.jbi.JBIException;
 import javax.jbi.management.DeploymentException;
+import javax.jbi.management.LifeCycleMBean;
 import javax.management.JMException;
 import javax.management.MBeanAttributeInfo;
 import javax.resource.spi.work.Work;
@@ -145,6 +146,7 @@
         FileUtil.deleteFile(environmentContext.getTmpDir());
         this.buildState(environmentContext.getInstallationDir());
         this.buildState(environmentContext.getDeploymentDir());
+        container.getManagementContext().registerSystemService(this, LifeCycleMBean.class);
     }
 
     /**
@@ -158,6 +160,7 @@
         if (statsTimer != null) {
             statsTimer.cancel();
         }
+        container.getManagementContext().unregisterMBean(this);
     }
 
     /**

Modified: trunk/core/src/main/java/org/servicemix/jbi/framework/DeploymentService.java (737 => 738)

--- trunk/core/src/main/java/org/servicemix/jbi/framework/DeploymentService.java	2005-11-03 15:44:37 UTC (rev 737)
+++ trunk/core/src/main/java/org/servicemix/jbi/framework/DeploymentService.java	2005-11-03 19:12:43 UTC (rev 738)
@@ -70,14 +70,21 @@
      * Initialize the Service
      * 
      * @param container
+     * @throws JBIException 
      * @throws DeploymentException
      */
-    public void init(JBIContainer container) {
+    public void init(JBIContainer container) throws JBIException {
         this.container = container;
         this.environmentContext = container.getEnvironmentContext();
         buildState();
+        container.getManagementContext().registerSystemService(this, DeploymentServiceMBean.class);
     }
 
+    public void shutDown() throws JBIException {
+        super.shutDown();
+        container.getManagementContext().unregisterMBean(this);
+    }
+    
     /**
      * Get the description
      * 

Modified: trunk/core/src/main/java/org/servicemix/jbi/framework/InstallationService.java (737 => 738)

--- trunk/core/src/main/java/org/servicemix/jbi/framework/InstallationService.java	2005-11-03 15:44:37 UTC (rev 737)
+++ trunk/core/src/main/java/org/servicemix/jbi/framework/InstallationService.java	2005-11-03 19:12:43 UTC (rev 738)
@@ -61,7 +61,6 @@
     private JBIContainer container;
     private EnvironmentContext environmentContext;
     private ManagementContext managementContext;
-    private DeploymentService deploymentService;
     private ClassLoaderService classLoaderService = new ClassLoaderService();
     private Map installers = new ConcurrentHashMap();
 
@@ -137,7 +136,6 @@
      */
     public boolean unloadInstaller(String componentName, boolean isToBeDeleted) {
         boolean result = false;
-        ComponentNameSpace cns = new ComponentNameSpace(container.getName(), componentName, componentName);
         try {
             InstallerMBeanImpl installer = (InstallerMBeanImpl) installers.remove(componentName);
             result = installer != null;
@@ -203,14 +201,15 @@
      * Initialize the Service
      * 
      * @param container
+     * @throws JBIException 
      * @throws DeploymentException
      */
-    public void init(JBIContainer container) {
+    public void init(JBIContainer container) throws JBIException {
         this.container = container;
         this.environmentContext = container.getEnvironmentContext();
         this.managementContext = container.getManagementContext();
-        this.deploymentService = container.getDeploymentService();
         buildState();
+        container.getManagementContext().registerSystemService(this, FrameworkInstallationService.class);
     }
 
     /**
@@ -574,4 +573,9 @@
 		context.setEnvironment(env);
         return context;
     }
+
+    public void shutDown() throws JBIException {
+        super.shutDown();
+        container.getManagementContext().unregisterMBean(this);
+    }
 }

Modified: trunk/core/src/main/java/org/servicemix/jbi/framework/Registry.java (737 => 738)

--- trunk/core/src/main/java/org/servicemix/jbi/framework/Registry.java	2005-11-03 15:44:37 UTC (rev 737)
+++ trunk/core/src/main/java/org/servicemix/jbi/framework/Registry.java	2005-11-03 19:12:43 UTC (rev 738)
@@ -35,6 +35,7 @@
 import javax.jbi.JBIException;
 import javax.jbi.component.Component;
 import javax.jbi.component.ComponentContext;
+import javax.jbi.management.LifeCycleMBean;
 import javax.jbi.servicedesc.ServiceEndpoint;
 import javax.management.ObjectName;
 import javax.xml.namespace.QName;
@@ -88,6 +89,7 @@
         this.container = container;
         this.componentRegistry.setContainerName(container.getName());
         this.subscriptionRegistry.init(this);
+        container.getManagementContext().registerSystemService(this, LifeCycleMBean.class);
     }
 
     /**
@@ -134,6 +136,7 @@
             }
         }
         super.shutDown();
+        container.getManagementContext().unregisterMBean(this);
     }
 
     protected InternalEndpoint matchEndpointByName(ServiceEndpoint[] endpoints, String endpointName) {

Modified: trunk/core/src/main/java/org/servicemix/jbi/management/BaseStandardMBean.java (737 => 738)

--- trunk/core/src/main/java/org/servicemix/jbi/management/BaseStandardMBean.java	2005-11-03 15:44:37 UTC (rev 737)
+++ trunk/core/src/main/java/org/servicemix/jbi/management/BaseStandardMBean.java	2005-11-03 19:12:43 UTC (rev 738)
@@ -354,6 +354,7 @@
      * Called after removal from the MBeanServer
      */
     public void postDeregister() {
+        executorService.shutdown();
     }
 
     /**

Modified: trunk/core/src/main/java/org/servicemix/jbi/management/ManagementContext.java (737 => 738)

--- trunk/core/src/main/java/org/servicemix/jbi/management/ManagementContext.java	2005-11-03 15:44:37 UTC (rev 737)
+++ trunk/core/src/main/java/org/servicemix/jbi/management/ManagementContext.java	2005-11-03 19:12:43 UTC (rev 738)
@@ -206,6 +206,15 @@
      */
     public void shutDown() throws JBIException {
         super.shutDown();
+        // Unregister all mbeans
+        Object[] beans = beanMap.keySet().toArray();
+        for (int i = 0; i < beans.length; i++) {
+            try {
+                unregisterMBean(beans[i]);
+            } catch (Exception e) {
+                log.debug("Could not unregister mbean", e);
+            }
+        }
         if (connectorServer != null) {
             try {
                 connectorServer.stop();
@@ -543,16 +552,20 @@
      * @param name
      * @throws JMException
      */
-    public void unregisterMBean(ObjectName name) throws JMException {
-        if (beanServer != null && beanServer.isRegistered(name)) {
-            beanServer.unregisterMBean(name);
-            for (Iterator i = beanMap.entrySet().iterator();i.hasNext();) {
-                Map.Entry entry = (Map.Entry) i.next();
-                if (entry.getValue().equals(name)) {
-                    beanMap.remove(entry.getKey());
-                    break;
+    public void unregisterMBean(ObjectName name) throws JBIException {
+        try {
+            if (beanServer != null && beanServer.isRegistered(name)) {
+                beanServer.unregisterMBean(name);
+                for (Iterator i = beanMap.entrySet().iterator();i.hasNext();) {
+                    Map.Entry entry = (Map.Entry) i.next();
+                    if (entry.getValue().equals(name)) {
+                        beanMap.remove(entry.getKey());
+                        break;
+                    }
                 }
             }
+        } catch (JMException e) {
+            throw new JBIException("Could not unregister mbean", e);
         }
     }
 
@@ -562,10 +575,14 @@
      * @param bean
      * @throws JMException
      */
-    public void unregisterMBean(Object bean) throws JMException {
-        ObjectName name = (ObjectName) beanMap.remove(bean);
-        if (name != null && beanServer != null) {
-            beanServer.unregisterMBean(name);
+    public void unregisterMBean(Object bean) throws JBIException {
+        try {
+            ObjectName name = (ObjectName) beanMap.remove(bean);
+            if (name != null && beanServer != null) {
+                beanServer.unregisterMBean(name);
+            }
+        } catch (JMException e) {
+            throw new JBIException("Could not unregister mbean", e);
         }
     }
 

Modified: trunk/core/src/main/java/org/servicemix/jbi/nmr/Broker.java (737 => 738)

--- trunk/core/src/main/java/org/servicemix/jbi/nmr/Broker.java	2005-11-03 15:44:37 UTC (rev 737)
+++ trunk/core/src/main/java/org/servicemix/jbi/nmr/Broker.java	2005-11-03 19:12:43 UTC (rev 738)
@@ -45,6 +45,7 @@
 
 import javax.jbi.JBIException;
 import javax.jbi.component.Component;
+import javax.jbi.management.LifeCycleMBean;
 import javax.jbi.messaging.MessagingException;
 import javax.jbi.messaging.MessageExchange.Role;
 import javax.jbi.servicedesc.ServiceEndpoint;
@@ -139,6 +140,7 @@
         if (flow != subscriptionManager.getFlow()) {
         	subscriptionManager.getFlow().init(this, "subscription");
         }
+        container.getManagementContext().registerSystemService(this, LifeCycleMBean.class);
     }
 
     /**
@@ -206,6 +208,7 @@
         	subscriptionManager.getFlow().shutDown();
         }
         super.shutDown();
+        container.getManagementContext().unregisterMBean(this);
     }
 
     /**

Modified: trunk/core/src/main/java/org/servicemix/jbi/nmr/flow/seda/SedaFlow.java (737 => 738)

--- trunk/core/src/main/java/org/servicemix/jbi/nmr/flow/seda/SedaFlow.java	2005-11-03 15:44:37 UTC (rev 737)
+++ trunk/core/src/main/java/org/servicemix/jbi/nmr/flow/seda/SedaFlow.java	2005-11-03 19:12:43 UTC (rev 738)
@@ -236,7 +236,7 @@
         try {
             broker.getManagementContext().unregisterMBean(queue.getObjectName());
         }
-        catch (JMException e) {
+        catch (JBIException e) {
             log.error("Failed to unregister SedaQueue: " + queue + " from the ManagementContext");
         }
     }

Reply via email to