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();
	}

}



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

Java设计模式的常见应用场景

一、Java I/O中的设计模式1、适配器模式适配器模式就是把一个类的接口变换成客户端所能接受的另一种接口,从而使两个接口不匹配而无法在一起工作的两个类能够在一起工作。通常被用在一个项目需要引用一些开...

tomcat Lifecycle设计模式

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

Tomcat中Lifecycle详解(源码阅读)

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

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

Tomcat 8(七)解读Bootstrap介绍过,执行startup.bat,将引发Tomcat一连串组件的启动。其实这一连串启动是通过组件的生命周期(Lifecycle)实现的 今天来看看Lif...

【设计模式基础】行为模式 - 8 -状态(State)

1. 模式意图 允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎是修改了它的类。 在很多情况下,一个对象的行为取决于一个或多个动态变化的属性,这样的属性叫做状态,这样的对象叫做有状态的对...

轻松掌握state设计模式

  • 2009年08月08日 20:06
  • 89KB
  • 下载

java设计模式进阶_state

// // // Generated by StarUML(tm) Java Add-In // // @ Project : Untitled // @ File Name : State.j...

GOF设计模式之STATE(状态)

概述本文将和读者一起来理解GOF设计模式之OBSERVER(代理)。同样,本文将用JAVA对GOF中的示例代码改些,同时附上测试用例。模式结构GOF对STATE设计模式的结构描述如 Contex...

state-----设计模式

1.状态模式,当我们处理一些情况是,要依据别人对象的状态来决定处理的内容,我们呢可以 为不同的状态生成不同的类,在核心类中,我们就可以依据不同的状态来得到不同的处理。2.一个较为复杂的示例:packa...
  • daijope
  • daijope
  • 2011年07月16日 14:38
  • 332

State(状态)设计模式

一、概念 当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。状态模式主要解决的是当控制一个对象状态的条件表达式过于复杂时的情况。把状态的判断逻辑转移到表示不同状态的...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Tomcat 8.x 《设计模式》Lifecycle & State 模式
举报原因:
原因补充:

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