第 8 章 JBoss EAP MBean Services

受管 Bean 有时只称为 MBean,是利用依赖注入创建的 JavaBean 类型。MBean 服务是 JBoss EAP 服务器的核心构建模块。

8.1. 编写 JBoss MBean 服务

编写依赖于 JBoss 服务的自定义 MBean 服务需要服务接口方法模式。JBoss MBean 服务接口方法模式由一组生命周期操作组成,在 可创建启动停止 和销毁 自身时通知 MBean 服务。

您可以使用以下任一方法管理依赖项状态:

  • 如果您想在 MBean 上调用特定方法,请在 MBean 界面中声明这些方法。这种方法允许您的 MBean 实施以避免对 JBoss 特定课程的依赖。
  • 如果您不担心对 JBoss 特定类的依赖,则您可以扩展您的 MBean 接口和 ServiceMBean Support 类。ServiceMBeanSupport 类提供创建、启动和停止等服务生命周期方法的实施。要处理 start() 事件等特定事件,您需要覆盖 ServiceMBeanSupport 类提供的 startService() 方法。

8.1.1. 标准 MBean 示例

本节开发两个在服务存档(.sar)中打包在一起的 MBean 服务示例。

ConfigServiceMBean 接口声明了具体的方法,如 startgetTimeoutstop 方法,以正确 启动保留 和停止 MBean,而无需使用任何 JBoss 特定类。ConfigService 类实施 ConfigServiceMBean 接口,从而实施该接口内使用的方法。

PlainThread 类扩展 ServiceMBeanSupport 类,并实施 PlainThreadMBean 接口。PlainThread 启动线程并使用 ConfigServiceMBean.getTimeout() 来确定线程应休眠的时长。

示例:MBean 服务类

package org.jboss.example.mbean.support;
public interface ConfigServiceMBean {
    int getTimeout();
    void start();
    void stop();
}
package org.jboss.example.mbean.support;
public class ConfigService implements ConfigServiceMBean {
    int timeout;
    @Override
    public int getTimeout() {
        return timeout;
    }
    @Override
    public void start() {
        //Create a random number between 3000 and 6000 milliseconds
        timeout = (int)Math.round(Math.random() * 3000) + 3000;
        System.out.println("Random timeout set to " + timeout + " seconds");
    }
    @Override
    public void stop() {
        timeout = 0;
    }
}

package org.jboss.example.mbean.support;
import org.jboss.system.ServiceMBean;
public interface PlainThreadMBean extends ServiceMBean {
    void setConfigService(ConfigServiceMBean configServiceMBean);
}

package org.jboss.example.mbean.support;
import org.jboss.system.ServiceMBeanSupport;
public class PlainThread extends ServiceMBeanSupport implements PlainThreadMBean {
    private ConfigServiceMBean configService;
    private Thread thread;
    private volatile boolean done;
    @Override
    public void setConfigService(ConfigServiceMBean configService) {
        this.configService = configService;
    }
    @Override
    protected void startService() throws Exception {
        System.out.println("Starting Plain Thread MBean");
        done = false;
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (!done) {
                        System.out.println("Sleeping....");
                        Thread.sleep(configService.getTimeout());
                        System.out.println("Slept!");
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });
        thread.start();
    }
    @Override
    protected void stopService() throws Exception {
        System.out.println("Stopping Plain Thread MBean");
        done = true;
    }
}

jboss-service.xml 描述符演示了 ConfigService 类如何使用注入标签 注入 PlainThread 类。inject tag 在 PlainThreadMBean 和 ConfigServiceMBean 之间建立一个依赖关系,因此 PlainThreadMBean 可以轻松地使用 ConfigServiceMBean

示例: jboss-service.xml 服务描述符

<server xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="urn:jboss:service:7.0 jboss-service_7_0.xsd"
        xmlns="urn:jboss:service:7.0">
 <mbean code="org.jboss.example.mbean.support.ConfigService" name="jboss.support:name=ConfigBean"/>
 <mbean code="org.jboss.example.mbean.support.PlainThread" name="jboss.support:name=ThreadBean">
  <attribute name="configService">
   <inject bean="jboss.support:name=ConfigBean"/>
  </attribute>
 </mbean>
</server>

编写 MBeans 示例后,您可以将类和 jboss-service.xml 描述符打包在服务存档(.sar)的 META-INF/ 文件夹中。