add: 添加NbAppEventListener,可拦截NbApp的各种事件

This commit is contained in:
Wendal Chen 2018-03-22 15:57:54 +08:00
parent 771a406cf5
commit fd18877d09
3 changed files with 63 additions and 1 deletions

View File

@ -3,8 +3,10 @@
# dev 迭代中 # dev 迭代中
* 变更: * 变更:
* update: 完善shiro集成, by 大鲨鱼 * update: 完善shiro集成, by 大鲨鱼
* add: starter-ssdb by 道坤 * add: starter-ssdb by 道坤
* add: 添加NbAppEventListener,可以拦截NbApp的各种事件
# 2.1.4 "平凡之路" # 2.1.4 "平凡之路"

View File

@ -20,7 +20,9 @@ import org.nutz.boot.env.SystemPropertiesEnvHolder;
import org.nutz.boot.ioc.IocLoaderProvider; import org.nutz.boot.ioc.IocLoaderProvider;
import org.nutz.boot.resource.ResourceLoader; import org.nutz.boot.resource.ResourceLoader;
import org.nutz.boot.resource.impl.SimpleResourceLoader; import org.nutz.boot.resource.impl.SimpleResourceLoader;
import org.nutz.boot.tools.NbAppEventListener;
import org.nutz.boot.tools.PropDocReader; import org.nutz.boot.tools.PropDocReader;
import org.nutz.boot.tools.NbAppEventListener.EventType;
import org.nutz.ioc.Ioc2; import org.nutz.ioc.Ioc2;
import org.nutz.ioc.IocLoader; import org.nutz.ioc.IocLoader;
import org.nutz.ioc.ObjectProxy; import org.nutz.ioc.ObjectProxy;
@ -85,6 +87,8 @@ public class NbApp extends Thread {
protected boolean prepared; protected boolean prepared;
protected Object lock = new Object(); protected Object lock = new Object();
protected NbAppEventListener listener = new NbAppEventListener() {};
/** /**
* 创建一个NbApp,把调用本构造方法的类作为mainClass * 创建一个NbApp,把调用本构造方法的类作为mainClass
@ -181,18 +185,26 @@ public class NbApp extends Thread {
Stopwatch sw = Stopwatch.begin(); Stopwatch sw = Stopwatch.begin();
// 各种预备操作 // 各种预备操作
listener.whenPrepare(this, EventType.before);
this.prepare(); this.prepare();
listener.whenPrepare(this, EventType.after);
// 依次启动 // 依次启动
try { try {
listener.whenInitAppContext(this, EventType.before);
ctx.init(); ctx.init();
listener.whenInitAppContext(this, EventType.after);
listener.whenStartServers(this, EventType.before);
ctx.startServers(); ctx.startServers();
listener.whenStartServers(this, EventType.after);
if (ctx.getMainClass().getAnnotation(IocBean.class) != null) if (ctx.getMainClass().getAnnotation(IocBean.class) != null)
ctx.getIoc().get(ctx.getMainClass()); ctx.getIoc().get(ctx.getMainClass());
sw.stop(); sw.stop();
listener.afterAppStated(this);
log.infof("NB started : %sms", sw.du()); log.infof("NB started : %sms", sw.du());
synchronized (lock) { synchronized (lock) {
lock.wait(); lock.wait();
@ -220,19 +232,28 @@ public class NbApp extends Thread {
if (prepared) if (prepared)
return; return;
// 初始化上下文 // 初始化上下文
listener.whenPrepareBasic(this, EventType.before);
this.prepareBasic(); this.prepareBasic();
listener.whenPrepareBasic(this, EventType.after);
// 打印Banner,暂时不可配置具体的类 // 打印Banner,暂时不可配置具体的类
new SimpleBannerPrinter().printBanner(ctx); new SimpleBannerPrinter().printBanner(ctx);
// 配置信息要准备好 // 配置信息要准备好
listener.whenPrepareConfigureLoader(this, EventType.before);
this.prepareConfigureLoader(); this.prepareConfigureLoader();
listener.whenPrepareConfigureLoader(this, EventType.before);
// 创建IocLoader体系 // 创建IocLoader体系
listener.whenPrepareIocLoader(this, EventType.before);
prepareIocLoader(); prepareIocLoader();
listener.whenPrepareIocLoader(this, EventType.after);
// 加载各种starter // 加载各种starter
listener.whenPrepareStarterClassList(this, EventType.before);
prepareStarterClassList(); prepareStarterClassList();
listener.whenPrepareStarterClassList(this, EventType.before);
// 打印配置文档 // 打印配置文档
if (printProcDoc) { if (printProcDoc) {
@ -242,10 +263,14 @@ public class NbApp extends Thread {
} }
// 创建Ioc容器 // 创建Ioc容器
listener.whenPrepareIoc(this, EventType.before);
prepareIoc(); prepareIoc();
listener.whenPrepareIoc(this, EventType.after);
// 生成Starter实例 // 生成Starter实例
listener.whenPrepareStarterInstance(this, EventType.before);
prepareStarterInstance(); prepareStarterInstance();
listener.whenPrepareStarterInstance(this, EventType.before);
prepared = true; prepared = true;
} }
@ -425,4 +450,9 @@ public class NbApp extends Thread {
} }
} }
public void setListener(NbAppEventListener listener) {
if (listener == null)
throw new NullPointerException("NULL NbAppEventListener!!!");
this.listener = listener;
}
} }

View File

@ -0,0 +1,30 @@
package org.nutz.boot.tools;
import org.nutz.boot.NbApp;
public interface NbAppEventListener {
default void whenPrepare(NbApp app, EventType et) {};
default void whenPrepareBasic(NbApp app, EventType et) {};
default void whenPrepareConfigureLoader(NbApp app, EventType et) {};
default void whenPrepareIocLoader(NbApp app, EventType et) {};
default void whenPrepareStarterClassList(NbApp app, EventType et) {};
default void whenPrepareIoc(NbApp app, EventType et) {};
default void whenPrepareStarterInstance(NbApp app, EventType et) {};
default void whenInitAppContext(NbApp app, EventType et) {};
default void whenStartServers(NbApp app, EventType et) {}
default void afterAppStated(NbApp app) {};
enum EventType {
before, after
}
}