增加SERVER_EXECUTOR线程池的资源注入

This commit is contained in:
Redkale
2018-01-12 09:14:10 +08:00
parent 21af487aab
commit 1cd5fe0b02
4 changed files with 32 additions and 7 deletions

View File

@@ -92,6 +92,11 @@ public final class Application {
*/ */
public static final String RESNAME_SERVER_ROOT = Server.RESNAME_SERVER_ROOT; public static final String RESNAME_SERVER_ROOT = Server.RESNAME_SERVER_ROOT;
/**
* 当前Server的线程池
*/
public static final String RESNAME_SERVER_EXECUTOR = Server.RESNAME_SERVER_EXECUTOR;
//本地IP地址 //本地IP地址
final InetAddress localAddress; final InetAddress localAddress;

View File

@@ -140,6 +140,10 @@ public abstract class NodeServer {
} }
//必须要进行初始化, 构建Service时需要使用Context中的ExecutorService //必须要进行初始化, 构建Service时需要使用Context中的ExecutorService
server.init(this.serverConf); server.init(this.serverConf);
//init之后才有Executor
resourceFactory.register(Server.RESNAME_SERVER_EXECUTOR, Executor.class, server.getExecutor());
resourceFactory.register(Server.RESNAME_SERVER_EXECUTOR, ExecutorService.class, server.getExecutor());
resourceFactory.register(Server.RESNAME_SERVER_EXECUTOR, ThreadPoolExecutor.class, server.getExecutor());
initResource(); //给 DataSource、CacheSource 注册依赖注入时的监听回调事件。 initResource(); //给 DataSource、CacheSource 注册依赖注入时的监听回调事件。
String interceptorClass = this.serverConf.getValue("interceptor", ""); String interceptorClass = this.serverConf.getValue("interceptor", "");

View File

@@ -31,6 +31,8 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
public static final String RESNAME_SERVER_ROOT = "SERVER_ROOT"; public static final String RESNAME_SERVER_ROOT = "SERVER_ROOT";
public static final String RESNAME_SERVER_EXECUTOR = "SERVER_EXECUTOR";
protected final Logger logger = Logger.getLogger(this.getClass().getSimpleName()); protected final Logger logger = Logger.getLogger(this.getClass().getSimpleName());
//------------------------------------------------------------- //-------------------------------------------------------------
@@ -146,6 +148,10 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
this.prepare.destroy(context, config); this.prepare.destroy(context, config);
} }
public ThreadPoolExecutor getExecutor() {
return executor;
}
public InetSocketAddress getSocketAddress() { public InetSocketAddress getSocketAddress() {
return address; return address;
} }

View File

@@ -6,7 +6,8 @@
package org.redkale.service; package org.redkale.service;
import java.util.concurrent.*; import java.util.concurrent.*;
import org.redkale.net.WorkThread; import javax.annotation.Resource;
import org.redkale.net.*;
/** /**
* *
@@ -14,16 +15,25 @@ import org.redkale.net.WorkThread;
*/ */
public abstract class AbstractService implements Service { public abstract class AbstractService implements Service {
//如果开启了SNCP此处线程池为SncpServer的线程池
@Resource(name = Server.RESNAME_SERVER_EXECUTOR)
private ExecutorService serverExecutor;
protected void runAsync(Runnable runner) { protected void runAsync(Runnable runner) {
Thread thread = Thread.currentThread(); if (serverExecutor != null) {
if (thread instanceof WorkThread) { serverExecutor.execute(runner);
((WorkThread) thread).runAsync(runner);
} else { } else {
ForkJoinPool.commonPool().execute(runner); Thread thread = Thread.currentThread();
if (thread instanceof WorkThread) {
((WorkThread) thread).runAsync(runner);
} else {
ForkJoinPool.commonPool().execute(runner);
}
} }
} }
protected ExecutorService getExecutor() { protected ExecutorService getExecutor() {
if (serverExecutor != null) return serverExecutor;
Thread thread = Thread.currentThread(); Thread thread = Thread.currentThread();
if (thread instanceof WorkThread) { if (thread instanceof WorkThread) {
return ((WorkThread) thread).getExecutor(); return ((WorkThread) thread).getExecutor();