JBoss MSC 即 JBoss Modular Service Container,是第三代 JBoss 产品 JBoss 7和WildFfly的内核,JBoss MSC 替换了之前的 JMX Kernel 和 MicroContainer,它主要特定可以总结如下三点:
JBoss MSC 它管理一系列 Service,WildFly中任何东西都是一个Service,如 EJB,JCA等,接下来我们说明说明是Service。
JBoss 7/WildFfly 中 Service 的定义:一个可相互依赖的 POJO 类。
Service 的状态如下图所示:
JBoss MSC Service 的状态比较简单,和之前的JMX Kernel 和 MicroContainer相比,没有多相位设计,如图:
本示例简单介绍如何启动 JBoss MSC 并加载一个 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(); } }
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; } }
我们可以在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)