This commit is contained in:
RedKale
2016-04-13 14:34:48 +08:00
parent 43ef1fb22f
commit 04098f976b
3 changed files with 54 additions and 11 deletions

View File

@@ -87,6 +87,7 @@
responsePoolSize Response池的大小默认: CPU核数*256 responsePoolSize Response池的大小默认: CPU核数*256
readTimeoutSecond: 读操作超时秒数, 默认0 表示永久不超时 readTimeoutSecond: 读操作超时秒数, 默认0 表示永久不超时
writeTimeoutSecond: 写操作超时秒数, 默认0 表示永久不超时 writeTimeoutSecond: 写操作超时秒数, 默认0 表示永久不超时
nodeInterceptor: 启动/关闭NodeServer时被调用的拦截器实现类必须是org.redkale.boot.NodeInterceptor的子类默认为null
--> -->
<server protocol="HTTP" host="127.0.0.1" port="6060" root="root" lib=""> <server protocol="HTTP" host="127.0.0.1" port="6060" root="root" lib="">

View File

@@ -0,0 +1,21 @@
/*
* 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;
/**
*
* @author zhangjx
*/
public class NodeInterceptor {
public void preStart(NodeServer server) {
}
public void preShutdown(NodeServer server) {
}
}

View File

@@ -6,6 +6,7 @@
package org.redkale.boot; package org.redkale.boot;
import java.io.*; import java.io.*;
import static java.lang.Class.forName;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@@ -65,6 +66,8 @@ public abstract class NodeServer {
protected AnyValue serverConf; protected AnyValue serverConf;
protected NodeInterceptor interceptor;
protected final Set<ServiceWrapper> localServiceWrappers = new LinkedHashSet<>(); protected final Set<ServiceWrapper> localServiceWrappers = new LinkedHashSet<>();
protected final Set<ServiceWrapper> remoteServiceWrappers = new LinkedHashSet<>(); protected final Set<ServiceWrapper> remoteServiceWrappers = new LinkedHashSet<>();
@@ -139,7 +142,11 @@ public abstract class NodeServer {
} }
initResource(); //给 DataSource、CacheSource 注册依赖注入时的监听回调事件。 initResource(); //给 DataSource、CacheSource 注册依赖注入时的监听回调事件。
String interceptorClass = config.getValue("nodeInterceptor", "");
if (!interceptorClass.isEmpty()) {
Class clazz = forName(interceptorClass);
this.interceptor = (NodeInterceptor) clazz.newInstance();
}
ClassFilter<Servlet> servletFilter = createServletClassFilter(); ClassFilter<Servlet> servletFilter = createServletClassFilter();
ClassFilter<Service> serviceFilter = createServiceClassFilter(); ClassFilter<Service> serviceFilter = createServiceClassFilter();
long s = System.currentTimeMillis(); long s = System.currentTimeMillis();
@@ -203,7 +210,7 @@ public abstract class NodeServer {
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.SEVERE, "DataSource inject error", e); logger.log(Level.SEVERE, "DataSource inject error", e);
} }
},DataSource.class); }, DataSource.class);
resourceFactory.register((ResourceFactory rf, final Object src, final String resourceName, Field field, final Object attachment) -> { resourceFactory.register((ResourceFactory rf, final Object src, final String resourceName, Field field, final Object attachment) -> {
try { try {
if (field.getAnnotation(Resource.class) == null) return; if (field.getAnnotation(Resource.class) == null) return;
@@ -252,7 +259,7 @@ public abstract class NodeServer {
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.SEVERE, "DataSource inject error", e); logger.log(Level.SEVERE, "DataSource inject error", e);
} }
},CacheSource.class); }, CacheSource.class);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -283,7 +290,7 @@ public abstract class NodeServer {
} else { } else {
service = Sncp.createRemoteService(entry.getName(), getExecutor(), type, this.sncpAddress, loadTransport(groups)); service = Sncp.createRemoteService(entry.getName(), getExecutor(), type, this.sncpAddress, loadTransport(groups));
} }
if(SncpClient.parseMethod(type).isEmpty()) continue; //class没有可用的方法 通常为BaseService if (SncpClient.parseMethod(type).isEmpty()) continue; //class没有可用的方法 通常为BaseService
final ServiceWrapper wrapper = new ServiceWrapper(type, service, entry.getName(), localed ? this.sncpGroup : null, groups, entry.getProperty()); final ServiceWrapper wrapper = new ServiceWrapper(type, service, entry.getName(), localed ? this.sncpGroup : null, groups, entry.getProperty());
for (final Class restype : wrapper.getTypes()) { for (final Class restype : wrapper.getTypes()) {
if (resourceFactory.find(wrapper.getName(), restype) == null) { if (resourceFactory.find(wrapper.getName(), restype) == null) {
@@ -473,10 +480,12 @@ public abstract class NodeServer {
} }
public void start() throws IOException { public void start() throws IOException {
if (interceptor != null) interceptor.preStart(this);
server.start(); server.start();
} }
public void shutdown() throws IOException { public void shutdown() throws IOException {
if (interceptor != null) interceptor.preShutdown(this);
final StringBuilder sb = logger.isLoggable(Level.INFO) ? new StringBuilder() : null; final StringBuilder sb = logger.isLoggable(Level.INFO) ? new StringBuilder() : null;
localServiceWrappers.forEach(y -> { localServiceWrappers.forEach(y -> {
long s = System.currentTimeMillis(); long s = System.currentTimeMillis();
@@ -490,4 +499,16 @@ public abstract class NodeServer {
server.shutdown(); server.shutdown();
} }
public <T extends Server> T getServer() {
return (T) server;
}
public Set<ServiceWrapper> getLocalServiceWrappers() {
return new LinkedHashSet<>(localServiceWrappers);
}
public Set<ServiceWrapper> getRemoteServiceWrappers() {
return new LinkedHashSet<>(remoteServiceWrappers);
}
} }