ServiceContainer 接口类图如下所示:
如图:
public static ServiceContainer create() public static ServiceContainer create(String name) public static ServiceContainer create(int coreSize, long keepAliveTime, TimeUnit keepAliveTimeUnit) public static ServiceContainer create(String name, int coreSize, long keepAliveTime, TimeUnit keepAliveTimeUnit) public static ServiceContainer create(boolean autoShutdown) public static ServiceContainer create(String name, boolean autoShutdown) public static ServiceContainer create(int coreSize, long keepAliveTime, TimeUnit keepAliveTimeUnit, boolean autoShutdown) public static ServiceContainer create(String name, int coreSize, long keepAliveTime, TimeUnit keepAliveTimeUnit, boolean autoShutdown)
ServiceContainer 接口的实现类 ServiceContainerImpl 中有一个 ContainerExecutor 属性,其中在实例化ServiceContainerImpl被初始化,ContainerExecutor 实现了 ThreadPoolExecutor 且使用如下构造方法:
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
ContainerExecutor(final int corePoolSize, final int maximumPoolSize, final long keepAliveTime, final TimeUnit unit) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() { private final int id = executorSeq.getAndIncrement(); private final AtomicInteger threadSeq = new AtomicInteger(1); public Thread newThread(final Runnable r) { Thread thread = new ServiceThread(r, ServiceContainerImpl.this); thread.setName(String.format("MSC service thread %d-%d", Integer.valueOf(id), Integer.valueOf(threadSeq.getAndIncrement()))); thread.setUncaughtExceptionHandler(HANDLER); return thread; } }, POLICY); }
JBoss MSC ContainerExecutor 的启动是ServiceBuilder 的 install 方法,使的 ServiceContainerImpl 的 doExecute 方法中执行,此方法完成容器初始化:
void doExecute(final ArrayList<Runnable> tasks) { assert !holdsLock(this); if (tasks == null) return; final Executor executor = primaryRegistration.getContainer().getExecutor(); for (Runnable task : tasks) { try { executor.execute(task); } catch (RejectedExecutionException e) { task.run(); } } }
如 JBoss MSC - 简单介绍及一个简单示例 中的示例,我们只启动一个服务,但是示例执行完成后容器中有8个线程,这是因为我们初始化的 corePoolSize 为 8,当线程数小于 corePoolSize 就新建线程,并处理请求。
// coming soon
JBoss 系列九十八:JBoss MSC - 浅析 ServiceContainer,布布扣,bubuko.com
JBoss 系列九十八:JBoss MSC - 浅析 ServiceContainer
原文地址:http://blog.csdn.net/kylinsoong/article/details/25004575