Simple Object Lifecycle

Tomcat in imitation of the life cycle aspects of code to define what our simple objects related to the life cycle of interfaces

**
 * @author        :weiguangyue
 * 
 * 具有生命周期start和stop方法的服务对象-->启停服务
 */
interface Lifecycle {

   /**
    * @description    : 启动服务
    */
   void start();
   
   /**
    * @description    : 是否已经启动
    */
   boolean isRunning();
   
   /**
    * @description    : 停止服务
    */
   void stop();
   
   /**
    * @description    : 是否已经停止
    */
   boolean isStoped();
}

Then implement an abstract class

/**
 * @author        :weiguangyue
 * 
 * 基础生命周期
 */
abstract class BaseLifecycle implements Lifecycle{
   
   private enum Status{
      UNSTARTED,
      RUNNING,
      STOPED
   }
   private Status currentStatus = Status.UNSTARTED;
   
   protected synchronized void checkRunning() {
      if(this.currentStatus != Status.RUNNING) {
         throw new IllegalStateException("status is not running!!!");
      }
   }
   
   @Override
   public final synchronized boolean isRunning() {
      return this.currentStatus == Status.RUNNING;
   }

   @Override
   public final synchronized boolean isStoped() {
      return this.currentStatus == Status.STOPED;
   }

   @Override
   public final synchronized void start() {
      if(this.currentStatus != Status.UNSTARTED){
         return;
      }
      this.doStart();
      this.currentStatus = Status.RUNNING;
   }
   
   @Override
   public final synchronized void stop() {
      if(this.currentStatus != Status.RUNNING) {
         return;
      }
      this.doStop();
      this.currentStatus = Status.STOPED;
   }

   protected abstract void doStart();

   protected abstract void doStop();
}

We reserve the doStart and doStop method for subclasses to implement, so you can define your own unique initialization code

Next, we have a combined jmx carry out his lifetime mbean

import java.lang.management.ManagementFactory;

import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author        :weiguangyue-1571
 * 
 * 基础生命周期MBean对象,继承此类可快速实现MBean注册
 */
public class BaseLifecycleMBean extends BaseLifecycle{
   
   private static final Logger log = LoggerFactory.getLogger(BaseLifecycleMBean.class);

   @Override
   protected void doStart() {
      this.registerMBean();
   }

   @Override
   protected void doStop() {
      this.unregisterMBean();
   }
   
   private ObjectName getObjectName() {
      String packageName = this.getClass().getPackage().getName();
      String name = String.format("%s:type=%s_%s",packageName,this.getClass().getName(),this.hashCode());
      try {
         ObjectName objectName = new ObjectName(name);
         return objectName;
      } catch (Exception e) {
         //ignore
      }
      return null;
   }
   
   private void unregisterMBean() {
      try {
         ObjectName objectName = this.getObjectName();
         if(objectName != null) {
            MBeanServer server = ManagementFactory.getPlatformMBeanServer();
            server.unregisterMBean(objectName);
         }
      }catch (Exception e) {
         log.error("",e);
      }
   }

   private void registerMBean() {
      try {
         ObjectName objectName = this.getObjectName();
         if(objectName != null) {
            
            MBeanServer server = ManagementFactory.getPlatformMBeanServer();
            if(server.isRegistered(objectName)) {
               server.unregisterMBean(objectName);                   
            }
            server.registerMBean(this,objectName);
         }
      }catch (Exception e) {
         log.error("",e);
      }
   }
}

In this way, those who want to register to jmx among some of the bean, the direct successor BaseLifecycleMBean can, and destroy their initialization logic logic can continue to achieve doStart method and doStop method, however, pay attention also to add super.doStart () and super.doStop () to do so, combined with the life cycle and jmx, it is relatively simple and easy.

However, the code for this article which has a bug, I do not know Tell me what you have not found it, ha ha, you are welcome Tell me what is wrong with the sharp-eyed pointed out :)

Guess you like

Origin www.cnblogs.com/weiguangyue/p/12297182.html