dubbo默认是使用netty的,在初始化NettyServer的构造方法时,对ChannelHandler 进行了包装。
public NettyServer(URL url, ChannelHandler handler) {
super(url, ChannelHandlers.wrap(handler,
ExecutorUtil.setThreadName(url, "DubboServerHandler")));
}
最后通过MultiMessageHandler->HeartbeatHandler->通过ExtensionLoader根据配置参数寻找具体的Dispatcher,调用Dispatcher.dispatch得到最终的ChannelHandler
protected ChannelHandler wrapInternal(ChannelHandler handler,
URL url) {
return new MultiMessageHandler(new HeartbeatHandler(
ExtensionLoader.getExtensionLoader(Dispatcher.class)
.getAdaptiveExtension().dispatch(handler, url)));
}
配置还是采用SPI方式.
@SPI(AllDispatcher.NAME)
public interface Dispatcher {
@Adaptive({
Constants.DISPATCHER_KEY, "dispather",
"channel.handler"})
ChannelHandler dispatch(ChannelHandler handler, URL url);
}
@SPI
public interface ChannelHandler {
/** 连接
**/
void connected(Channel channel) ;
/**断开连接
**/
void disconnected(Channel channel) ;
/**发送数据
**/
void sent(Channel channel, Object message) ;
/**接收数据
**/
void received(Channel channel, Object message) ;
/**连接出现异常
**/
void caught(Channel channel, Throwable exception) ;
}
AllDispatcher(默认all)
在连接、断开连接、接收数据以及连接出现异常时都是交给线程池去处理的。而发送数据直接交给IO去处理
public class AllDispatcher implements Dispatcher {
public static final String NAME = "all";
public ChannelHandler dispatch(ChannelHandler handler, URL url) {
return new AllChannelHandler(handler, url);
}
}
public class AllChannelHandler extends WrappedChannelHandler {
public AllChannelHandler(ChannelHandler handler, URL url) {
super(handler, url);
}
public void connected(Channel channel) {
ExecutorService cexecutor = getExecutorService();
try{
cexecutor.execute(new ChannelEventRunnable(channel,
handler ,ChannelState.CONNECTED));
}catch (Throwable t) {
throw new ExecutionException();
}
}
public void disconnected(Channel channel) {
ExecutorService cexecutor = getExecutorService();
try{
cexecutor.execute(new ChannelEventRunnable(channel,
handler ,ChannelState.DISCONNECTED));
}catch (Throwable t) {
throw new ExecutionException();
}
}
public void received(Channel channel, Object message) {
ExecutorService cexecutor = getExecutorService();
try {
cexecutor.execute(new ChannelEventRunnable(channel,
handler, ChannelState.RECEIVED, message));
} catch (Throwable t) {
throw new ExecutionException();
}
}
public void caught(Channel channel, Throwable exception) {
ExecutorService cexecutor = getExecutorService();
try{
cexecutor.execute(new ChannelEventRunnable(channel,
handler ,ChannelState.CAUGHT, exception));
}catch (Throwable t) {
throw new ExecutionException();
}
}
private ExecutorService getExecutorService() {
ExecutorService cexecutor = executor;
if (cexecutor == null || cexecutor.isShutdown()) {
cexecutor = Executors.newCachedThreadPool(new
NamedThreadFactory("DubboSharedHandler", true));
}
return cexecutor;
}
}
而 发送数据则是由父类WrappedChannelHandler,直接交给handler
public void sent(Channel channel, Object message) {
handler.sent(channel, message);
}
而线程池初始化也是交给父类去实现的
executor = (ExecutorService) ExtensionLoader.getExtensionLoader(ThreadPool.class).getAdaptiveExtension().getExecutor(url);
DirectDispatcher(direct)
直接交给ChannelHandler去处理
所有消息都不派发到线程池,全部在 IO 线程上直接执行。
public class DirectDispatcher implements Dispatcher {
public static final String NAME = "direct";
public ChannelHandler dispatch(ChannelHandler handler,
URL url) {
return handler;
}
}
MessageOnlyChannelHandler(message)
只有在接收数据才交给线程池,别的还是交给ChannelHandler 去处理
public class MessageOnlyChannelHandler extends WrappedChannelHandler {
public MessageOnlyChannelHandler(ChannelHandler handler,
URL url) {
super(handler, url);
}
public void received(Channel channel, Object message) {
ExecutorService cexecutor = executor;
if (cexecutor == null || cexecutor.isShutdown()) {
cexecutor = SHARED_EXECUTOR;
}
try {
cexecutor.execute(new ChannelEventRunnable(channel,
handler, ChannelState.RECEIVED, message));
} catch (Throwable t) {
throw new ExecutionException();
}
}
}
ExecutionChannelHandler(execution)
和All类似
public class ExecutionChannelHandler extends WrappedChannelHandler {
public ExecutionChannelHandler(ChannelHandler handler, URL url) {
super(handler, url);
}
public void connected(Channel channel) {
executor.execute(new ChannelEventRunnable(channel, handler ,ChannelState.CONNECTED));
}
public void disconnected(Channel channel) {
executor.execute(new ChannelEventRunnable(channel, handler ,ChannelState.DISCONNECTED));
}
public void received(Channel channel, Object message) {
executor.execute(new ChannelEventRunnable(channel, handler, ChannelState.RECEIVED, message));
}
public void caught(Channel channel, Throwable exception) t {
executor.execute(new ChannelEventRunnable(channel, handler ,ChannelState.CAUGHT, exception));
}
}
ConnectionOrderedChannelHandler(connection)
在 IO 线程上,将连接断开事件放入队列,有序逐个执行,
其它消息派发到线程池
线程池由自己创建
public class ConnectionOrderedChannelHandler extends WrappedChannelHandler {
protected final ThreadPoolExecutor connectionExecutor;
private final int queuewarninglimit ;
public ConnectionOrderedChannelHandler(ChannelHandler handler, URL url) {
super(handler, url);
connectionExecutor = new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(
url.getPositiveParameter(Constants.CONNECT_QUEUE_CAPACITY,
Integer.MAX_VALUE)),
new NamedThreadFactory(threadName, true),
new AbortPolicyWithReport(threadName, url)
); // FIXME 没有地方释放connectionExecutor!
queuewarninglimit =
url.getParameter(Constants.CONNECT_QUEUE_WARNING_SIZE,
Constants.DEFAULT_CONNECT_QUEUE_WARNING_SIZE);
}
public void connected(Channel channel) {
try{
checkQueueLength();
connectionExecutor.execute(new ChannelEventRunnable(
channel, handler ,ChannelState.CONNECTED));
}catch (Throwable t) {
throw new ExecutionException();
}
}
public void disconnected(Channel channel){
try{
checkQueueLength();
connectionExecutor.execute(new ChannelEventRunnable(
channel, handler ,ChannelState.DISCONNECTED));
}catch (Throwable t) {
throw new ExecutionException();
}
}
public void received(Channel channel, Object message){
ExecutorService cexecutor = executor;
if (cexecutor == null || cexecutor.isShutdown()) {
cexecutor = SHARED_EXECUTOR;
}
try {
cexecutor.execute(new ChannelEventRunnable(
channel, handler, ChannelState.RECEIVED, message));
} catch (Throwable t) {
throw new ExecutionException();
}
}
public void caught(Channel channel, Throwable exception) {
ExecutorService cexecutor = executor;
if (cexecutor == null || cexecutor.isShutdown()) {
cexecutor = SHARED_EXECUTOR;
}
try{
cexecutor.execute(new ChannelEventRunnable(channel,
handler ,ChannelState.CAUGHT, exception));
}catch (Throwable t) {
throw new ExecutionException();
}
}
private void checkQueueLength(){
if (connectionExecutor.getQueue().size() >
queuewarninglimit){
logger.warn(new IllegalThreadStateException());
}
}
}
@SPI("fixed")
public interface ThreadPool {
/**
* 线程池
*
* @param url 线程参数
* @return 线程池
*/
@Adaptive({
Constants.THREADPOOL_KEY})
Executor getExecutor(URL url);
}
FixedThreadPool
固定大小线程池,启动时建立线程,不关闭,一直持有。(缺省)
/**
* 此线程池启动时即创建固定大小的线程数,不做任何伸缩,来源于:Executors.newFixedThreadPool()
*
* @see java.util.concurrent.Executors#newFixedThreadPool(int)
* @author william.liangf
*/
public class FixedThreadPool implements ThreadPool {
public Executor getExecutor(URL url) {
String name = url.getParameter(Constants.THREAD_NAME_KEY,
Constants.DEFAULT_THREAD_NAME);
int threads = url.getParameter(Constants.THREADS_KEY,
Constants.DEFAULT_THREADS);
int queues = url.getParameter(Constants.QUEUES_KEY,
Constants.DEFAULT_QUEUES);
return new ThreadPoolExecutor(threads, threads, 0,
TimeUnit.MILLISECONDS,
queues == 0 ? new SynchronousQueue<Runnable>() :
(queues < 0 ? new LinkedBlockingQueue<Runnable>()
: new LinkedBlockingQueue<Runnable>(queues)),
new NamedThreadFactory(name, true),
new AbortPolicyWithReport(name, url));
}
}
**CachedThreadPool **
缓存线程池,空闲一分钟自动删除,需要时重建。
/**
* 此线程池可伸缩,线程空闲一分钟后回收,新请求重新创建线程,来源于:Executors.newCachedThreadPool()
*
* @see java.util.concurrent.Executors#newCachedThreadPool()
* @author william.liangf
*/
public class CachedThreadPool implements ThreadPool {
public Executor getExecutor(URL url) {
String name = url.getParameter(Constants.THREAD_NAME_KEY,
Constants.DEFAULT_THREAD_NAME);
int cores = url.getParameter(Constants.CORE_THREADS_KEY,
Constants.DEFAULT_CORE_THREADS);
int threads = url.getParameter(Constants.THREADS_KEY,
Integer.MAX_VALUE);
int queues = url.getParameter(Constants.QUEUES_KEY,
Constants.DEFAULT_QUEUES);
int alive = url.getParameter(Constants.ALIVE_KEY,
Constants.DEFAULT_ALIVE);
return new ThreadPoolExecutor(cores, threads, alive,
TimeUnit.MILLISECONDS,
queues == 0 ? new SynchronousQueue<Runnable>() :
(queues < 0 ? new LinkedBlockingQueue<Runnable>()
: new LinkedBlockingQueue<Runnable>(queues)),
new NamedThreadFactory(name, true),
new AbortPolicyWithReport(name, url));
}
}
LimitedThreadPool
可伸缩线程池,但池中的线程数只会增长不会收缩。
只增长不收缩的目的是为了避免收缩时突然来了大流量引起的性能问题。
/**
* 此线程池一直增长,直到上限,增长后不收缩。
*
* @author kimi
*/
public class LimitedThreadPool implements ThreadPool {
public Executor getExecutor(URL url) {
String name = url.getParameter(Constants.THREAD_NAME_KEY,
Constants.DEFAULT_THREAD_NAME);
int cores = url.getParameter(Constants.CORE_THREADS_KEY,
Constants.DEFAULT_CORE_THREADS);
int threads = url.getParameter(Constants.THREADS_KEY,
Constants.DEFAULT_THREADS);
int queues = url.getParameter(Constants.QUEUES_KEY,
Constants.DEFAULT_QUEUES);
return new ThreadPoolExecutor(cores, threads, Long.MAX_VALUE, TimeUnit.MILLISECONDS,
queues == 0 ? new SynchronousQueue<Runnable>() :
(queues < 0 ? new LinkedBlockingQueue<Runnable>()
: new LinkedBlockingQueue<Runnable>(queues)),
new NamedThreadFactory(name, true), new AbortPolicyWithReport(name, url));
}
}