JBoss 系列九十六:JBoss MSC - 简介及一个简单演示样例
什么是 JBoss MSC
JBoss MSC 即 JBoss Modular Service Container,是第三代 JBoss 产品 JBoss 7和WildFfly的内核,JBoss MSC 替换了之前的 JMX Kernel 和 MicroContainer,它主要特定能够总结例如以下三点:
- 高并发容器(A highly concurrent state machine)
- 无多相位,设计简单(No multiple phases, much simpler)
- 不依赖 JMX 和 Java EE
JBoss MSC 它管理一系列 Service,WildFly中不论什么东西都是一个Service,如 EJB,JCA等,接下来我们说明说明是Service。
什么是 Service
JBoss 7/WildFfly 中 Service 的定义:一个可相互依赖的 POJO 类。
Service 的状态例如以下图所看到的:
JBoss MSC Service 的状态比較简单,和之前的JMX Kernel 和 MicroContainer相比,没有多相位设计,如图:
- Service 在 DOWN 状态时能够从容器中移除,从而进入 REMOVED 状态
- Service 在 DOWN 状态时能够载入启动,启动成功进入 STARTING 状态,启动失败进入 START_FAILED 状态
- Service 在 START_FAILED 状态时能够尝试重新启动,尝试失败后能够退回 DOWN 状态
- Service 在 STARTING 状态后进入 UP 状态
- Service 在 UP 状态时能够停止进入 STOPPING 状态
一个简单演示样例
本演示样例简介怎样启动 JBoss MSC 并载入一个 Service。
Service 实现
一个 Service 必须实现 org.jboss.msc.service.Service<T> 接口,例如以下 MyService 的实现例如以下:
package org.jboss.msc.quickstart; import org.jboss.msc.service.Service; import org.jboss.msc.service.ServiceName; import org.jboss.msc.service.StartContext; import org.jboss.msc.service.StartException; import org.jboss.msc.service.StopContext; public class MyService implements Service<MyServiceManager> { final static ServiceName SERVICE = ServiceName.of("service"); private MyServiceManager manager; public MyService(MyServiceManager manager) { this.manager = manager; } public MyServiceManager getValue() throws IllegalStateException, IllegalArgumentException { return manager; } public void start(StartContext context) throws StartException { manager.initialize("init"); } public void stop(StopContext context) { manager.cleanup(); } }
相关 MyServiceManager 实现例如以下:
package org.jboss.msc.quickstart; public class MyServiceManager { private String description; public void initialize(String description) { this.description = description; } public void cleanup() { this.description = null; } public String getDescription() { return description; } }
启动 JBoss MSC 载入 MyService
我们能够在Main方法中启动载入MyService,例如以下所看到的:
ServiceContainer serviceContainer = ServiceContainer.Factory.create(); Service<MyServiceManager> service = new MyService(new MyServiceManager()); ServiceBuilder<MyServiceManager> builder = serviceContainer.addService(MyService.SERVICE, service); ServiceController<MyServiceManager> controller = builder.install();
思考
如上代码启动了JBoss MSC,JBoss 7启动时也启动了 JBoss MSC,然后逐一载入Service。执行如上代码后,我们收集线程 Dump 分析启动的线程,我们会发现例如以下线程:
"MSC service thread 1-1" prio=10 tid=0x6b904400 nid=0x14fa waiting on condition [0x6b55a000] java.lang.Thread.State: WAITING (parking) "MSC service thread 1-2" prio=10 tid=0x6b904400 nid=0x14fa waiting on condition [0x6b55a000] java.lang.Thread.State: WAITING (parking) "MSC service thread 1-3" prio=10 tid=0x6b904400 nid=0x14fa waiting on condition [0x6b55a000] java.lang.Thread.State: WAITING (parking) "MSC service thread 1-4" prio=10 tid=0x6b904400 nid=0x14fa waiting on condition [0x6b55a000] java.lang.Thread.State: WAITING (parking) "MSC service thread 1-5" prio=10 tid=0x6b904400 nid=0x14fa waiting on condition [0x6b55a000] java.lang.Thread.State: WAITING (parking) "MSC service thread 1-6" prio=10 tid=0x6b904400 nid=0x14fa waiting on condition [0x6b55a000] java.lang.Thread.State: WAITING (parking) "MSC service thread 1-7" prio=10 tid=0x6b904400 nid=0x14fa waiting on condition [0x6b55a000] java.lang.Thread.State: WAITING (parking) "MSC service thread 1-8" prio=10 tid=0x6b904400 nid=0x14fa waiting on condition [0x6b55a000] java.lang.Thread.State: WAITING (parking)
假设熟悉 JBoss 7或 WildFLy 线程,我们会发现这些线程和 JBoss 7或 WildFLy 同样,实际上它们都执行相似的代码。