Dubbo线程分析

Dubbo 线程分析

  • Dubbo provider应用初始化及启动加载
  • Dubbo provider的线程模型分析

    Dubbo provider应用初始化及启动加载

  • Dubbo spring context 启动加载入口:
    1
    2
    3
    // 类:com.alibaba.dubbo.config.spring.ServiceBean<T>
    public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean, DisposableBean,
    ApplicationContextAware, ApplicationListener, BeanNameAware {......}

Dubbo 线程模型

  • 接口:com.alibaba.dubbo.common.threadpool
  • 类1:com.alibaba.dubbo.common.threadpool.support.fixed.FixedThreadPool
  • 类2:com.alibaba.dubbo.common.threadpool.support.limited.LimitedThreadPool
  • 类3:com.alibaba.dubbo.common.threadpool.support.cached.CachedThreadPool

FixedThreadPool 线程改造,增加定时线程池状态记录方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.common.threadpool.support.fixed;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.threadpool.ThreadPool;
import com.alibaba.dubbo.common.threadpool.support.AbortPolicyWithReport;
import com.alibaba.dubbo.common.utils.NamedThreadFactory;
/**
* 此线程池启动时即创建固定大小的线程数,不做任何伸缩,来源于:<code>Executors.newFixedThreadPool()</code>
*
* @see java.util.concurrent.Executors#newFixedThreadPool(int)
* @author william.liangf
*/
public class FixedThreadPool implements ThreadPool {
private static ThreadPoolExecutor POOL_EXECUTOR;
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));
POOL_EXECUTOR = 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));
return POOL_EXECUTOR;
}
/**
* 获取 poolExecutor
* @return 返回 poolExecutor
*/
public final static ThreadPoolExecutor getPoolExecutor() {
return POOL_EXECUTOR;
}
}