什么是 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,WildFfly中任何东西都是一个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 相同,实际上它们都运行类似的代码。
时间: 2024-10-25 18:32:48