8장. JBoss EAP MBean 서비스

관리형 빈(즉, 간단히 MBean이라고도 함)은 종속성 주입을 사용하여 생성되는 JavaBean의 유형입니다. MBean 서비스는 JBoss EAP 서버의 핵심 구성 요소입니다.

8.1. JBoss MBean 서비스 작성

JBoss 서비스를 사용하는 사용자 지정 MBean 서비스를 작성하려면 서비스 인터페이스 방법 패턴이 필요합니다. JBoss MBean 서비스 인터페이스 방법 패턴은 자체적으로 생성,시작,중지 및 제거할 수 있는 MBean 서비스에 알리는 라이프사이클 작업 집합으로 구성됩니다.

다음 방법을 사용하여 종속성 상태를 관리할 수 있습니다.

  • MBean에서 특정 메서드를 호출하려면 MBean 인터페이스에서 해당 메서드를 선언합니다. 이 접근 방식을 통해 MBean 구현을 통해 JBoss 특정 클래스에 대한 종속성을 방지할 수 있습니다.
  • JBoss 특정 클래스에 대한 종속성에 대해 걱정할 필요가 없는 경우 MBean 인터페이스에서 ServiceMBean 인터페이스와 ServiceMBean Support 클래스를 확장할 수 있습니다. ServiceMBeanSupport 클래스는 생성, 시작 및 중지와 같은 서비스 라이프사이클 방법을 구현합니다. start() 이벤트와 같은 특정 이벤트를 처리하려면 ServiceMBeanSupport 클래스에서 제공하는 startService() 메서드를 재정의해야 합니다.

8.1.1. 표준 MBean 예

이 섹션에서는 서비스 아카이브(. sar)에 함께 패키지로 제공되는 MBean 서비스 예제를 개발합니다.

ConfigServiceMBean 인터페이스는 JBoss 특정 클래스를 사용하지 않고 MBean을 올바르게 시작,보유중지하는 start,getTimeoutstop 메서드와 같은 특정 메서드를 선언합니다. 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 설명자는 inject 태그를 사용하여 ConfigService 클래스가 PlainThread 클래스에 삽입되는 방법을 보여줍니다. inject 태그는 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 예제를 작성한 후에는 서비스 아카이브( . sar)의 META-INF/ 폴더에 클래스 및 jboss- service.xml설명자를 패키징할 수 있습니다.