Tomcat 8.x 《设计模式》Lifecycle & State 模式

原创 2016年08月31日 00:44:01
package cn.java.note.state;
public interface Lifecycle {

	public static final String BEFORE_INIT_EVENT = "before_init";
	public static final String AFTER_INIT_EVENT = "after_init";
	public static final String START_EVENT = "start";
	public static final String BEFORE_START_EVENT = "before_start";
	public static final String AFTER_START_EVENT = "after_start";
	public static final String STOP_EVENT = "stop";
	public static final String BEFORE_STOP_EVENT = "before_stop";
	public static final String AFTER_STOP_EVENT = "after_stop";
	public static final String AFTER_DESTROY_EVENT = "after_destroy";
	public static final String BEFORE_DESTROY_EVENT = "before_destroy";
	public static final String PERIODIC_EVENT = "periodic";
	public static final String CONFIGURE_START_EVENT = "configure_start";
	public static final String CONFIGURE_STOP_EVENT = "configure_stop";

	public void addLifecycleListener(LifecycleListener listener);
	public LifecycleListener[] findLifecycleListeners();
	public void removeLifecycleListener(LifecycleListener listener);
	public void init() throws LifecycleException;
	public void start() throws LifecycleException;
	public void stop() throws LifecycleException;
	public void destroy() throws LifecycleException;
	public LifecycleState getState();
	public String getStateName();
	public interface SingleUse {
	}
}

package cn.java.note.state;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class LifecycleBase implements Lifecycle {
	private volatile LifecycleState state = LifecycleState.NEW;
	private final List<LifecycleListener> lifecycleListeners = new CopyOnWriteArrayList<>();

	/**
	 * 设置状态
	 */
	protected synchronized void setState(LifecycleState state)
			throws LifecycleException {
		setStateInternal(state, null, true);
	}

	/**
	 * 设置状态
	 */
	protected synchronized void setState(LifecycleState state, Object data) throws LifecycleException {
		setStateInternal(state, data, true);
	}

	/**
	 * 设置状态
	 */
	private synchronized void setStateInternal(LifecycleState state, Object data, boolean check) throws LifecycleException {
		this.state = state;
		String lifecycleEvent = state.getLifecycleEvent(); // 事件名称,如 “before_init” “start”
		if (lifecycleEvent != null) {
			fireLifecycleEvent(lifecycleEvent, data); // 触发“监听器”
		}
	}

	/**
	 * 触发事件监听器
	 * @param type
	 * @param data
	 */
	protected void fireLifecycleEvent(String type, Object data) {
		LifecycleEvent event = new LifecycleEvent(this, type, data);
		for (LifecycleListener listener : lifecycleListeners) {
			listener.lifecycleEvent(event); // 触发事件
		}
	}

	/**
	 * 添加事件监听器
	 */
	@Override
	public void addLifecycleListener(LifecycleListener listener) {
		lifecycleListeners.add(listener);
	}

	/**
	 * 查看事件监听器
	 */
	@Override
	public LifecycleListener[] findLifecycleListeners() {
		return lifecycleListeners.toArray(new LifecycleListener[0]);
	}

	/**
	 * 删除事件监听器
	 */
	@Override
	public void removeLifecycleListener(LifecycleListener listener) {
		lifecycleListeners.remove(listener);
	}

	/**
	 * 初始化
	 */
	@Override
	public final synchronized void init() throws LifecycleException {
		if (!state.equals(LifecycleState.NEW)) {
			// invalidTransition(Lifecycle.BEFORE_INIT_EVENT);
		}
		setStateInternal(LifecycleState.INITIALIZING, null, false); // 触发事件

		// initInternal(); //!!!!

		setStateInternal(LifecycleState.INITIALIZED, null, false); // 触发事件
	}

	/**
	 * 启动
	 */
	@Override
	public final synchronized void start() throws LifecycleException {

		if (LifecycleState.STARTING_PREP.equals(state) || LifecycleState.STARTING.equals(state) ||
				LifecycleState.STARTED.equals(state)) {
			return;
		}

		if (state.equals(LifecycleState.NEW)) {
			init();//!!!调用初始化
		} else if (state.equals(LifecycleState.FAILED)) {
			stop();
		} else if (!state.equals(LifecycleState.INITIALIZED) &&
				!state.equals(LifecycleState.STOPPED)) {
			// invalidTransition(Lifecycle.BEFORE_START_EVENT);
		}

		setStateInternal(LifecycleState.STARTING_PREP, null, false); // 触发事件

		// startInternal(); //触发事件

		if (state.equals(LifecycleState.FAILED)) {
			stop();
		} else if (!state.equals(LifecycleState.STARTING)) {
			// invalidTransition(Lifecycle.AFTER_START_EVENT);
		} else {
			setStateInternal(LifecycleState.STARTED, null, false);// 触发事件
		}
	}

	/**
	 * 停止
	 */
	@Override
	public final synchronized void stop() throws LifecycleException {
		if (LifecycleState.STOPPING_PREP.equals(state) || LifecycleState.STOPPING.equals(state) ||
				LifecycleState.STOPPED.equals(state)) {
			return;
		}

		if (state.equals(LifecycleState.NEW)) {
			state = LifecycleState.STOPPED;
			return;
		}

		if (!state.equals(LifecycleState.STARTED) && !state.equals(LifecycleState.FAILED)) {
			// invalidTransition(Lifecycle.BEFORE_STOP_EVENT);
		}

		if (state.equals(LifecycleState.FAILED)) {
			fireLifecycleEvent(BEFORE_STOP_EVENT, null);
		} else {
			setStateInternal(LifecycleState.STOPPING_PREP, null, false);
		}

		// stopInternal();

		if (!state.equals(LifecycleState.STOPPING) && !state.equals(LifecycleState.FAILED)) {
			// invalidTransition(Lifecycle.AFTER_STOP_EVENT);
		}

		setStateInternal(LifecycleState.STOPPED, null, false);
	}

	/**
	 * 销毁
	 */
	@Override
	public final synchronized void destroy() throws LifecycleException {
		if (LifecycleState.FAILED.equals(state)) {
			stop();
		}

		if (LifecycleState.DESTROYING.equals(state) ||
				LifecycleState.DESTROYED.equals(state)) {
			return;
		}
		if (!state.equals(LifecycleState.STOPPED) &&
				!state.equals(LifecycleState.FAILED) &&
				!state.equals(LifecycleState.NEW) &&
				!state.equals(LifecycleState.INITIALIZED)) {
			// invalidTransition(Lifecycle.BEFORE_DESTROY_EVENT);
		}
		setStateInternal(LifecycleState.DESTROYING, null, false);

		// destroyInternal();

		setStateInternal(LifecycleState.DESTROYED, null, false);
	}

	@Override
	public LifecycleState getState() {
		return state;
	}

	@Override
	public String getStateName() {
		return getState().toString();
	}
}


package cn.java.note.state;

import java.util.EventObject;

public final class LifecycleEvent extends EventObject {

	private static final long serialVersionUID = 1L;

	public LifecycleEvent(Lifecycle lifecycle, String type, Object data) {
		super(lifecycle);
		this.type = type;
		this.data = data;
	}

	private final Object data;

	private final String type;


	public Object getData() {
		return data;
	}


	public Lifecycle getLifecycle() {
		return (Lifecycle) getSource();
	}

	public String getType() {
		return this.type;
	}
}

package cn.java.note.state;
public final class LifecycleException extends Exception {

    private static final long serialVersionUID = 1L;

    public LifecycleException() {
        super();
    }

    public LifecycleException(String message) {
        super(message);
    }

    public LifecycleException(Throwable throwable) {
        super(throwable);
    }

    public LifecycleException(String message, Throwable throwable) {
        super(message, throwable);
    }
}

package cn.java.note.state;
public interface LifecycleListener {
    public void lifecycleEvent(LifecycleEvent event);
}

package cn.java.note.state;
/**
 * 生命周期状态
 */
public enum LifecycleState {
    NEW(false, null),
    INITIALIZING(false, Lifecycle.BEFORE_INIT_EVENT),
    INITIALIZED(false, Lifecycle.AFTER_INIT_EVENT),
    STARTING_PREP(false, Lifecycle.BEFORE_START_EVENT),
    STARTING(true, Lifecycle.START_EVENT), // available = true,lifecycleEvent="start"
    STARTED(true, Lifecycle.AFTER_START_EVENT),// available = true,lifecycleEvent="after_start"
    STOPPING_PREP(true, Lifecycle.BEFORE_STOP_EVENT),// available = true,lifecycleEvent="before_stop"
    STOPPING(false, Lifecycle.STOP_EVENT),
    STOPPED(false, Lifecycle.AFTER_STOP_EVENT),
    DESTROYING(false, Lifecycle.BEFORE_DESTROY_EVENT),
    DESTROYED(false, Lifecycle.AFTER_DESTROY_EVENT),
    FAILED(false, null);

    private final boolean available;
    private final String lifecycleEvent;

    private LifecycleState(boolean available, String lifecycleEvent) {
        this.available = available;
        this.lifecycleEvent = lifecycleEvent;
    }

    public boolean isAvailable() {
        return available;
    }

    public String getLifecycleEvent() {
        return lifecycleEvent;
    }
}

package cn.java.note.state;

public class MyLifecycle1 extends LifecycleBase {

	@Override
	public String toString() {
		return "MyLifecycle1 []";
	}
	
}

package cn.java.note.state;

public class MyLifecycle1Listener implements LifecycleListener {

	@Override
	public void lifecycleEvent(LifecycleEvent event) {
		MyLifecycle1 myLifecycle1 = (MyLifecycle1) event.getLifecycle();
//		System.out.println(myLifecycle1);
		if (event.getType().equals(Lifecycle.BEFORE_INIT_EVENT)) { 
			beforeInit();
		} else if (event.getType().equals(Lifecycle.AFTER_INIT_EVENT)) {
		} else if (event.getType().equals(Lifecycle.BEFORE_START_EVENT)) {
		} else if (event.getType().equals(Lifecycle.START_EVENT)) {
		} else if (event.getType().equals(Lifecycle.AFTER_START_EVENT)) {
		} else if (event.getType().equals(Lifecycle.BEFORE_STOP_EVENT)) {
		} else if (event.getType().equals(Lifecycle.STOP_EVENT)) {
		} else if (event.getType().equals(Lifecycle.AFTER_STOP_EVENT)) {
		} else if (event.getType().equals(Lifecycle.BEFORE_DESTROY_EVENT)) {
		} else if (event.getType().equals(Lifecycle.AFTER_DESTROY_EVENT)) {
		}
		System.out.println("trigger event : " + event.getType());
	}

	protected synchronized void beforeInit() {

	}

}

package cn.java.note.state;

public class Test {

	public static void main(String[] args) throws LifecycleException {
		MyLifecycle1 myLifecycle = new MyLifecycle1();
		myLifecycle.addLifecycleListener(new MyLifecycle1Listener());
		myLifecycle.init();
		myLifecycle.start();
		myLifecycle.stop();
		myLifecycle.destroy();
	}

}



版权声明:本文为博主原创文章,未经博主允许不得转载。

React Native 学习笔记十二(嵌入原生应用 甚是坑啊)

1.首先 创建一个android studio 项目 我创建的叫HelloReactDemo2(随便起的) 2 .按照官网的介绍来做     在Terminal中输入 npm init      会给...
  • Youth_never_go_away
  • Youth_never_go_away
  • 2016年09月20日 11:58
  • 3262

Tomcat 8(九)解读Tomcat组件的生命周期(Lifecycle)

Tomcat 8(七)解读Bootstrap介绍过,执行startup.bat,将引发Tomcat一连串组件的启动。其实这一连串启动是通过组件的生命周期(Lifecycle)实现的 今天来看看Lif...
  • flyliuweisky547
  • flyliuweisky547
  • 2014年04月24日 20:27
  • 3045

Tomcat 设计模式总结(Tomcat源代码阅读系列之八)

本篇我们将来分析一下Tomcat中所涉及到设计模式,本文我们将主要来分析外观模式,观察者模式,责任链模式,模板方法模式,命令模式。  在开始本文之前,笔者先说明一下对于设计模式的一点看法。笔者曾经经...
  • jiaomingliang
  • jiaomingliang
  • 2015年08月11日 20:55
  • 1875

Tomcat8.x 《设计模式》Facade设计模式的使用->getServletContext()

Tomcat8.x Facade设计模式的使用 // 请求对象 class org.apache.catalina.connector.Request{ public Servlet...
  • zhouzhiande
  • zhouzhiande
  • 2016年08月06日 11:46
  • 118

Tomcat 8.x 《设计模式》Lifecycle & State 模式

package cn.java.note.state; public interface Lifecycle { public static final String BEFORE_INIT_EV...
  • zhouzhiande
  • zhouzhiande
  • 2016年08月31日 00:44
  • 409

React Native 十三:植入原生应用

由于React并没有假设你其余部分的技术栈—它通常只作为MVC模型中的V存在—它也很容易嵌入到一个并非由React Native开发的应用中。 一、需求 1.一个已有的、基于gradle构建的And...
  • p106786860
  • p106786860
  • 2016年05月14日 23:32
  • 6976

tomcat Lifecycle设计模式

现实生活中大部分的事物都有生命周期,就像人的生老病死一样。 在编程中也有很多对象是具有生命周期的,从初始化、运行、回收等 会经历几个不同的阶段。 在tomcat中容器相关的好多组建都实现了Lifecy...
  • is_zhoufeng
  • is_zhoufeng
  • 2015年11月02日 23:43
  • 2764

Tomcat中Lifecycle详解(源码阅读)

在tomcat中,每一个组件生命周期都是需要统一管理的,一般是由调用该组件的组件来启动或停止当前组建,如connector组件控制processor组件启动和停止,因此每个组件主要类都会继承Lifec...
  • u010149189
  • u010149189
  • 2016年05月06日 00:06
  • 1018

React中文文档之State and Lifecycle

state 和 生命周期 到目前为止,我们仅仅学习了一种方式来更新UI。 我们调用 'ReactDOM.render()' 来改变输出渲染: function tick() { const eleme...
  • beyond__devil
  • beyond__devil
  • 2016年11月17日 22:53
  • 399

Tomcat8源码分析系列-启动分析(一) Lifecycle

Lifecycle在其他框架中也很常见,比如spring,它常用于具有生命周期的组件,由Lifecycle控制组件的初始化、启动、销毁等动作,方便应用程序获取、释放某些资源,或者是触发某些特定的事件。...
  • Dwade_mia
  • Dwade_mia
  • 2018年01月13日 14:53
  • 59
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Tomcat 8.x 《设计模式》Lifecycle & State 模式
举报原因:
原因补充:

(最多只允许输入30个字)