Package org.jboss.jms.server

Source Code of org.jboss.jms.server.MessagingClusterHealthMBean$NodeRecovery

/*
* JBoss, Home of Professional Open Source
* Copyright 2005-2011, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/


package org.jboss.jms.server;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.management.ObjectName;

import org.jboss.logging.Logger;
import org.jboss.messaging.core.contract.PersistenceManager;
import org.jboss.messaging.core.impl.postoffice.MessagingPostOffice;
import org.jboss.messaging.util.JMXAccessor;
import org.jboss.system.ServiceMBeanSupport;

/**
* A MessagingClusterHealthMBean
*
* @author <a href="mailto:hgao@redhat.com">Howard Gao</a>
*
* Created Jan 31, 2011 8:52:02 PM
*
*
*/
public class MessagingClusterHealthMBean extends ServiceMBeanSupport
{
   private static final Logger log = Logger.getLogger(MessagingClusterHealthMBean.class);
  
   private String serverPeer;
   private String postOffice;
   private List<ObjectName> connectionFactories;
   private List<ObjectName> destinations;
   private String persistenceManager;
  
   private boolean nodeStopped;
  
   private NodeRecovery nodeRecovery;
  
   private long shutdownDelay;

   public void startService() throws Exception
   {
      connectionFactories = new ArrayList<ObjectName>();
      destinations = new ArrayList<ObjectName>();
     
      log.info(this + " started.");
   }
  
   public void stopService() throws Exception
   {
      setServerPeer(null);
      setPostOffice(null);
      setPersistenceManager(null);
      connectionFactories.clear();
      destinations.clear();
     
      if (nodeRecovery != null)
      {
         nodeRecovery.abandon();
         nodeRecovery = null;
      }
      log.info(this + " stopped.");
   }
  
   public Object getInstance()
   {
      return this;
   }
  
   public synchronized void stopNodeOnDBFailure() throws Exception
   {
      if (nodeStopped) return;

      log.info("Stopping JBM node on DB failure...");
           
      //destinations
      stopServices(destinations);
     
      //connection factories
      stopServices(connectionFactories);
     
      //post office
      ObjectName postOfficeServiceName = new ObjectName(postOffice);
      getShutdownDelay(postOfficeServiceName);
      stopService(postOfficeServiceName);
     
      //server peer
      ObjectName serverPeerName = new ObjectName(serverPeer);
      stopService(serverPeerName);
     
      nodeStopped = true;
     
      nodeRecovery = new NodeRecovery();
     
      nodeRecovery.start();
     
      log.info("JBM node is stopped.");
   }

   private void getShutdownDelay(ObjectName postOfficeServiceName)
   {
      try
      {
         long nodeRefreshInterval = (Long)server.getAttribute(postOfficeServiceName, "NodeStateRefreshInterval");
         shutdownDelay = nodeRefreshInterval * 2;
      }
      catch (Exception e)
      {
         log.warn("Failed to get node refresh interval, use default.", e);
         shutdownDelay = 30000;
      }
   }

   private void stopService(ObjectName service) throws Exception
   {
      try
      {
         server.invoke(service, "stop", new Object[0], new String[0]);
         log.debug("Service " + serviceName + " stopped.");
      }
      catch (Exception e)
      {
         log.warn("Error stopping service " + serviceName, e);
      }     
   }

   private void stopServices(List<ObjectName> services) throws Exception
   {
      Iterator<ObjectName> iter = services.iterator();
     
      while (iter.hasNext())
      {
         ObjectName serviceName = iter.next();
         stopService(serviceName);
      }
   }

   public void setServerPeer(String serverPeer)
   {
      this.serverPeer = serverPeer;
   }

   public String getServerPeer()
   {
      return serverPeer;
   }

   public void setPostOffice(String postOffice)
   {
      this.postOffice = postOffice;
   }

   public String getPostOffice()
   {
      return postOffice;
   }

   public void setPersistenceManager(String persistenceManager)
   {
      this.persistenceManager = persistenceManager;
   }

   public String getPersistenceManager()
   {
      return persistenceManager;
   }
  
   private boolean checkDBConnection() throws Exception
   {
      ObjectName pmName = new ObjectName(persistenceManager);
     
      PersistenceManager pm = (PersistenceManager)JMXAccessor.getJMXAttributeOverSecurity(server, pmName, "Instance");

      boolean isConnOK = false;
      try
      {
         isConnOK = pm.checkConnection();
      }
      catch (Exception e)
      {
         log.warn("failed to check connection", e);
         isConnOK = false;
      }
      return isConnOK;
   }
  
   private synchronized void restartJBMNode() throws Exception
   {
      if (!nodeStopped) return;
     
      makeSureDelay();
     
      log.info("Restarting JBM node...");
     
      //start server peer
      ObjectName spName = new ObjectName(serverPeer);
      startService(spName);
     
      //starat post office
      ObjectName poName = new ObjectName(postOffice);
      startService(poName);
     
      //wait for jgroups back
      MessagingPostOffice pm = (MessagingPostOffice)JMXAccessor.getJMXAttributeOverSecurity(server, poName, "Instance");
      while (!pm.waitForJGroups())
      {
         log.info("Failed to start post office due to JGroups failure, retrying...");
         this.stopService(poName);
         makeSureDelay();
         this.startService(poName);
         pm = (MessagingPostOffice)JMXAccessor.getJMXAttributeOverSecurity(server, poName, "Instance");
      }
     
      List<ObjectName> copy = new ArrayList<ObjectName>(connectionFactories);
      connectionFactories.clear();
      //start cf
      startServices(copy);
     
      copy = new ArrayList<ObjectName>(destinations);
      destinations.clear();
      //destinations
      startServices(copy);

      nodeStopped = false;
     
      log.info("JBM node restarted.");
   }
  
   private void makeSureDelay()
   {
      long delay = shutdownDelay;
     
      long stopTime = System.currentTimeMillis();
      while (delay > 0)
      {
         try
         {
            Thread.sleep(delay);
         }
         catch (InterruptedException e)
         {
         }
         delay = shutdownDelay - (System.currentTimeMillis() - stopTime);
      }
     
   }
  
   private void startService(ObjectName serviceName) throws Exception
   {
      server.invoke(serviceName, "start", new Object[0], new String[0]);
      log.debug("Service " + serviceName + " started.");
   }

   private void startServices(List<ObjectName> services) throws Exception
   {
      Iterator<ObjectName> iter = services.iterator();
     
      while (iter.hasNext())
      {
         ObjectName serviceName = iter.next();
         startService(serviceName);
      }
   }

   private class NodeRecovery extends Thread
   {
      private boolean keepTrying = true;
     
      public NodeRecovery()
      {
         this.setDaemon(true);
      }
     
      public synchronized void abandon()
      {
         keepTrying = false;
         notify();
      }

      public synchronized void run()
      {
         try
         {
            while (keepTrying)
            {
               if (checkDBConnection())
               {
                  restartJBMNode();
                  break;
               }
               try
               {
                  wait(5000);
               }
               catch (InterruptedException e)
               {
                  //ignore.
               }
            }
         }
         catch (Exception e)
         {
            log.error("Getting exception, stop recovery.", e);
         }
      }
   }

   public void registerFactory(ObjectName serviceName)
   {
      synchronized(connectionFactories)
      {
         connectionFactories.add(serviceName);
         log.info("Registered connection factory " + serviceName);
      }
   }

   public void registerDestination(ObjectName serviceName)
   {
      synchronized(destinations)
      {
         destinations.add(serviceName);
         log.info("Registered destination " + serviceName);
      }
   }
}
TOP

Related Classes of org.jboss.jms.server.MessagingClusterHealthMBean$NodeRecovery

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.