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)