增加ApplicationListener功能
This commit is contained in:
@@ -71,7 +71,13 @@
|
|||||||
<source name="redis" value="org.redkalex.cache.RedisCacheSource" xxx="16">
|
<source name="redis" value="org.redkalex.cache.RedisCacheSource" xxx="16">
|
||||||
<node addr="127.0.0.1" port="7070"/>
|
<node addr="127.0.0.1" port="7070"/>
|
||||||
</source>
|
</source>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Application启动的监听事件,可配置多个节点
|
||||||
|
value: 类名,必须是ApplicationListener的子类
|
||||||
|
-->
|
||||||
|
<listener value="org.redkalex.xxx.XXXApplicationListener"/>
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
【节点全局唯一】
|
【节点全局唯一】
|
||||||
全局的参数配置, 可以通过@Resource(name="property.xxxxxx") 进行注入<property>的信息, 被注解的字段类型只能是String、primitive class
|
全局的参数配置, 可以通过@Resource(name="property.xxxxxx") 进行注入<property>的信息, 被注解的字段类型只能是String、primitive class
|
||||||
|
|||||||
@@ -131,6 +131,9 @@ public final class Application {
|
|||||||
//日志
|
//日志
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
|
|
||||||
|
//监听事件
|
||||||
|
private final List<ApplicationListener> listeners = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
//服务启动时间
|
//服务启动时间
|
||||||
private final long startTime = System.currentTimeMillis();
|
private final long startTime = System.currentTimeMillis();
|
||||||
|
|
||||||
@@ -487,6 +490,15 @@ public final class Application {
|
|||||||
}
|
}
|
||||||
sncpTransportFactory.addGroupInfo(ginfo);
|
sncpTransportFactory.addGroupInfo(ginfo);
|
||||||
}
|
}
|
||||||
|
for (AnyValue conf : resources.getAnyValues("listener")) {
|
||||||
|
final String listenClass = conf.getValue("value", "");
|
||||||
|
if (listenClass.isEmpty()) continue;
|
||||||
|
Class clazz = Class.forName(listenClass);
|
||||||
|
if (!ApplicationListener.class.isAssignableFrom(clazz)) continue;
|
||||||
|
ApplicationListener listener = (ApplicationListener) clazz.newInstance();
|
||||||
|
listener.init(config);
|
||||||
|
this.listeners.add(listener);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------
|
//------------------------------------------------------------------------
|
||||||
}
|
}
|
||||||
@@ -783,6 +795,9 @@ public final class Application {
|
|||||||
application.init();
|
application.init();
|
||||||
application.startSelfServer();
|
application.startSelfServer();
|
||||||
try {
|
try {
|
||||||
|
for (ApplicationListener listener : application.listeners) {
|
||||||
|
listener.preStart(application);
|
||||||
|
}
|
||||||
application.start();
|
application.start();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
application.logger.log(Level.SEVERE, "Application start error", e);
|
application.logger.log(Level.SEVERE, "Application start error", e);
|
||||||
@@ -801,6 +816,14 @@ public final class Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void shutdown() throws Exception {
|
private void shutdown() throws Exception {
|
||||||
|
for (ApplicationListener listener : this.listeners) {
|
||||||
|
try {
|
||||||
|
listener.preShutdown(this);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.log(Level.WARNING, listener.getClass() + " preShutdown erroneous", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
servers.stream().forEach((server) -> {
|
servers.stream().forEach((server) -> {
|
||||||
try {
|
try {
|
||||||
server.shutdown();
|
server.shutdown();
|
||||||
|
|||||||
45
src/org/redkale/boot/ApplicationListener.java
Normal file
45
src/org/redkale/boot/ApplicationListener.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package org.redkale.boot;
|
||||||
|
|
||||||
|
import org.redkale.util.AnyValue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Application启动和关闭时的监听事件 <br>
|
||||||
|
* 只能通过application.xml配置
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 详情见: https://redkale.org
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
*/
|
||||||
|
public interface ApplicationListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化方法
|
||||||
|
*
|
||||||
|
* @param config 配置参数
|
||||||
|
*/
|
||||||
|
default void init(AnyValue config) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Application 在运行start前调用
|
||||||
|
*
|
||||||
|
* @param application Application
|
||||||
|
*/
|
||||||
|
default void preStart(Application application) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Application 在运行shutdown前调用
|
||||||
|
*
|
||||||
|
* @param application Application
|
||||||
|
*/
|
||||||
|
default void preShutdown(Application application) {
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user