增加SERVER_EXECUTOR线程池的资源注入
This commit is contained in:
@@ -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;
|
||||||
|
|
||||||
|
|||||||
@@ -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", "");
|
||||||
|
|||||||
@@ -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());
|
||||||
|
|
||||||
//-------------------------------------------------------------
|
//-------------------------------------------------------------
|
||||||
@@ -90,7 +92,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
|
|||||||
|
|
||||||
//最大连接数
|
//最大连接数
|
||||||
protected int maxconns;
|
protected int maxconns;
|
||||||
|
|
||||||
protected Server(long serverStartTime, String protocol, PrepareServlet<K, C, R, P, S> servlet) {
|
protected Server(long serverStartTime, String protocol, PrepareServlet<K, C, R, P, S> servlet) {
|
||||||
this.serverStartTime = serverStartTime;
|
this.serverStartTime = serverStartTime;
|
||||||
this.protocol = protocol;
|
this.protocol = protocol;
|
||||||
@@ -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;
|
||||||
}
|
}
|
||||||
@@ -192,7 +198,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
|
|||||||
}
|
}
|
||||||
serverChannel.bind(address, backlog);
|
serverChannel.bind(address, backlog);
|
||||||
serverChannel.setMaxconns(this.maxconns);
|
serverChannel.setMaxconns(this.maxconns);
|
||||||
serverChannel.accept();
|
serverChannel.accept();
|
||||||
final String threadName = "[" + Thread.currentThread().getName() + "] ";
|
final String threadName = "[" + Thread.currentThread().getName() + "] ";
|
||||||
logger.info(threadName + this.getClass().getSimpleName() + ("TCP".equalsIgnoreCase(protocol) ? "" : ("." + protocol)) + " listen: " + address
|
logger.info(threadName + this.getClass().getSimpleName() + ("TCP".equalsIgnoreCase(protocol) ? "" : ("." + protocol)) + " listen: " + address
|
||||||
+ ", threads: " + threads + ", bufferCapacity: " + bufferCapacity + ", bufferPoolSize: " + bufferPoolSize + ", responsePoolSize: " + responsePoolSize
|
+ ", threads: " + threads + ", bufferCapacity: " + bufferCapacity + ", bufferPoolSize: " + bufferPoolSize + ", responsePoolSize: " + responsePoolSize
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user