[Info]Tags categorized posts and contents patterns..

[AJAX] Ajax Code E xamples.. [Book] About the book.. [CSS] CSS Code E xamples.. [DB] Sql Code E xamples.. [DEV] All development stor...

2017년 2월 24일 금요일

[Spring 레퍼런스] 23장 JMX #1..


출처 : Outsider's Dev Story https://blog.outsider.ne.kr/

이 문서는 개인적인 목적이나 배포하기 위해서 복사할 수 있다. 출력물이든 디지털 문서든 각 복사본에 어떤 비용도 청구할 수 없고 모든 복사본에는 이 카피라이트 문구가 있어야 한다.

23. JMX

23.1 소개

스프링은 스프링 어플리케이션을 JMX 인프라에 쉽고 투명하게 통합할 수 있도록 지원한다.
특히 스프링의 JMX 지원은 네 가지 핵심 기능을 제공한다.
  • 어떤 스프링 빈이라도 JMX MBean으로 자동 등록
  • 빈 관리 인터페이스를 제어하는 유연한 메커니즘
  • 선언적으로 원격(JSR-160 커넥터)에 MBean을 노출
  • 로컬/원격 MBean 리소스를 간단하게 프록시 함
이 기능들은 애플리케이션 컴포넌트가 스프링이나 JMX 인터페이스 혹은 클래스에 의존하지 않고 동작하도록 설계되었다. 사실 애플리케이션 클래스 대부분은 스프링의 JMX 좋은 기능을 위해서 스프링이나 JMX를 인지할 필요가 없다.


JMX?
이번 장은 JMX를 소개하는 장이 아니므로 왜 JMX가 필요한가에 대해서는 설명하지 않는다(또는 JMX가 무엇의 약자인지) JMX를 처음 본다면 이 장 마지막의 Section 23.8, “추가 자료”를 봐라.


23.2 빈을 JMX에 내보내기

MBeanExporter이 스프링 JMX 프레임워크의 핵심 클래스다. MBeanExporter이 스프링 빈을 가져와서 JMX MBeanServer에 등록한다. 예시로 다음 클래스를 생각해 보자.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Java

package org.springframework.jmx;

public class JmxTestBean implements IJmxTestBean {
  private String name;
  private int age;
  private boolean isSuperman;

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

  public int add(int x, int y) {
    return x + y;
  }

  public void dontExposeMe() {
    throw new RuntimeException();
  }
}

이 빈의 프로퍼티와 메서드를 MBean의 속성과 오퍼레이션으로 노출하려면 다음과 같이 설정파일에 MBeanExporter 클래스의 인스턴스를 설정하고 빈을 전달하면 된다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
Xml

<beans>
  
  <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
    <property name="beans">
      <map>
        <entry key="bean:name=testBean1" value-ref="testBean"/>
      </map>
    </property>
  </bean>

  <bean id="testBean" class="org.springframework.jmx.JmxTestBean">
    <property name="name" value="TEST"/>
    <property name="age" value="100"/>
  </bean>
</beans>

위 설정 예시에서 적절한 빈 정의는 exporter 빈이다. beans 프로퍼티가 빈 중에 정확히 어떤 빈을 JMX MBeanServer에 내보내야야 하는지 MBeanExporter에 알려준다. 기본 설정에서 beans Map의 각 요소의 키를 해당 값이 참조하는 빈의 ObjectName으로 사용한다. Section 23.4, “빈의 ObjectName 제어”에서 설명했듯이 이 동작은 변경할 수 있다.
이 설정에서 testBean 빈은 ObjectName bean:name=testBean1의 MBean으로 노출한다. 기본적으로 빈의 모든 public 프로퍼티는 속성(attribute)로 노출하고 public 메서드 (Object 클래스에서 상속받은 메서드 포함)는 오퍼레이션으로 노출한다.

23.2.1 MBeanServer 생성

위의 설정은 하나의(유일한) MBeanServer가 동작하고 있는 환경에서 어플리케이션이 돌아가고 있다고 가정한다. 이러한 경우 스프링은 돌아가고 있는 MBeanServer을 찾아내서 (찾았다면)해당 서버에 빈을 등록하려고 할 것이다. Tomcat이나 IBM WebSphere처럼 MBeanServer를 가진 컨테이너에서 어플리케이션을 돌리는 경우 이 동작이 유용하다.
하지만 독립적인 환경이나 MBeanServer를 제공하지 않는 컨테이너를 사용하는 경우에는 이 접근을 사용하지 않는다. 설정에 org.springframework.jmx.support.MBeanServerFactoryBean 클래스의 인스턴스를 추가해서 선언적으로 MBeanServer 인스턴스를 생성해서 이 문제를 해결할 수 있다. MBeanExporter의 server 프로퍼티를 MBeanServerFactoryBean이 반환한 MBeanServer 값으로 설정해서 특정 MBeanServer를 사용한다는 것을 보장할 수도 있다. 예를 들면 다음과 같다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
Xml

<beans>
  <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean"/>

  
  <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="beans">
      <map>
        <entry key="bean:name=testBean1" value-ref="testBean"/>
      </map>
    </property>
    <property name="server" ref="mbeanServer"/>
  </bean>

  <bean id="testBean" class="org.springframework.jmx.JmxTestBean">
    <property name="name" value="TEST"/>
    <property name="age" value="100"/>
  </bean>
</beans>

여기서 MBeanServerFactoryBean이 MBeanServer의 인스턴스를 생성했고 server 프로퍼티로 MBeanExporter에 전달했다. 자신의 MBeanServer 인스턴스를 전달했을 때 MBeanExporter가 동작하고 있는 MBeanServer를 찾지 않고 전달받은 MBeanServer 인스턴스를 사용할 것이다. 이 부분이 제대로 동작하려면 클래스패스에 JMX 구현체가 당연히 있어야 한다.


23.2.2 이미 존재하는 MBeanServer의 재사용

서버를 지정하지 않으면 자동으로 MBeanExporter가 돌아가고 있는 MBeanServer를 탐지하려고 할 것이다. 딱 하나의 MBeanServer 인스턴스만 사용하는 대부분의 환경에서는 잘 동작할 것이지만 여러 인스턴스가 존재한다면 익스포터가 잘못된 서버를 선택할 수도 있다. 이러한 경우 사용할 인스턴스를 나타내는 MBeanServer agentId를 사용해야 한다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Xml

<beans>
   <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
     <!-- 제일 먼저 찾을 서버를 지정한다 -->
     <property name="locateExistingServerIfPossible" value="true"/>
     <!-- 주어진 agentId로 MBeanServer 인스턴스를 찾는다 -->
     <property name="agentId" value="<MBeanServer instance agentId>"/>
   </bean>

   <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
     <property name="server" ref="mbeanServer"/>
   ...
   </bean>
</beans>

기존의 MBeanServer가 검색 메서드로 얻은 동적(혹은 모르는) agentId를 가진 경우는 factory-method를 사용해야 한다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Xml

<beans>
   <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
     <property name="server">
       <!-- 커스텀 MBeanServerLocator -->
       <bean class="platform.package.MBeanServerLocator" factory-method="locateMBeanServer"/>
     </property>

     <!-- 다른  정의 -->
   </bean>
</beans>


23.2.3 지연 초기화된 MBean

지연 초기화로 설정된 MBeanExporter로 지연 초기화된 빈을 설정하는 경우 MBeanExporter이 해당 규약을 깨뜨리지 않을 것이고 빈을 인스턴스화하지 않을 것이다. 대신 MBeanExporter는 MBeanServer에 프록시를 등록하고 프록시에서 첫 호출이 발생할 때까지 컨테이너에서 빈을 가져오는 것을 미룰 것이다.

23.2.4 MBean의 자동 등록

MBeanExporter로 익스포트했고 유효한 MBean인 모든 빈은 스프링이 추가적으로 개입하지 않고 그대로 MBeanServer에 등록된다. autodetect 프로퍼티를 true로 설정해서 MBeanExporter가 자동으로 MBean을 탐지할 수 있다.

1
2
3
4
5
6
7
Xml

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
  <property name="autodetect" value="true"/>
</bean>

<bean name="spring:mbean=true" class="org.springframework.jmx.export.TestDynamicMBean"/>

이 예제에서 spring:mbean=true인 빈은 이미 유효한 JMX MBean이고 스프링이 자동으로 등록할 것이다. 기본적으로 JMX에 자동탐지되서 등록한 빈은 ObjectName을 빈 이름으로 사용한다. 이 동작은 Section 23.4, “빈의 ObjectName 제어”에서 설명한대로 변경할 수 있다.


23.2.5 등록과정의 제어

스프링 MBeanExporter가 'bean:name=testBean1'이라는 ObjectName으로 MBeanServer에 MBean을 등록하려고 하는 시나리오를 생각해 보자. MBean 인스턴스가 이미 같은 ObjectName으로 등록되어 있다면 기본 동작은 실패한다. (InstanceAlreadyExistsException를 던진다.)
MBean을 MBeanServer에 등록했을 때 일어나는 동작을 제어할 수 있다. 스프링의 JMX 지원으로 등록과정에서 MBean이 이미 같은 ObjectName으로 등록되었다는 점을 발견했을 때 등록 동작을 제어할 수 있는 세가지 등록 동작을 이용할 수 있다. 이 등록 동작은 다음 표에 정리되어 있다.

Table 23.1. 등록 동작
등록 동작설명
REGISTRATION_FAIL_ON_EXISTING기본 등록 동작이다. MBean 인스턴스가 같은 ObjectName으로 이미 등록되어 있다면 등록하려던 MBean을 등록하지 않고 InstanceAlreadyExistsException를 던질 것이다. 존재하는 MBean에는 영향을 주지 않는다.
REGISTRATION_IGNORE_EXISTINGMBean 인스턴스가 같은 ObjectName으로 이미 등록되어 있다면 등록하려던 MBean을 등록하지 않는다. 기존의 MBean에는 영향을 주지 않고 Exception도 던지지 않는다.
이 동작은 공유된 MBeanServer에서 여러 어플리케이션이 공통적인 MBean을 공유할 때 유용하다.
REGISTRATION_REPLACE_EXISTINGMBean 인스턴스가 같은 ObjectName으로 이미 등록되어 있다면 미리 등록되어 있던 MBean이 등록 해지되고 새로운 MBean이 해당 위치에 등록될 것이다. (이전의 인스턴스를 새로운 MBean이 효율적으로 바꿔치기 한다.)












위의 값은 MBeanRegistrationSupport 클래스(MBeanExporter 클래스는 이 수퍼클래스에서 파생된 클래스다.)에 상수로 정의되어 있다. 기존 등록동작을 바꾸고자 한다면 MBeanExporter 정의에서 registrationBehaviorName 프로퍼티의 값을 변경하면 된다.
다음 예제는 기본 등록 동작을 REGISTRATION_REPLACE_EXISTING로 변경이 어떻게 영향을 주는지 보여준다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
Xml

<beans>
  <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="beans">
      <map>
        <entry key="bean:name=testBean1" value-ref="testBean"/>
      </map>
    </property>
    <property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING"/>
  </bean>

  <bean id="testBean" class="org.springframework.jmx.JmxTestBean">
    <property name="name" value="TEST"/>
    <property name="age" value="100"/>
  </bean>
</beans>


23.3 빈의 관리 인터페이스 제어하기

이전 예제에서 빈의 관리(management) 인터페이스로 약간의 제어를 했다. 익스포트한 모든 public 프로퍼티와 메서드는 JMX 속성과 동작으로 각각 노출된다. 익스포트한 빈의 어떤 프로퍼티와 메서드가 JMX 속성과 동작으로 노출되는지 세밀하게 제어하기 위해서 스프링 JMX는 빈의 관리 인터페이스를 제어하는 광범위하고 확장가능한 메카니즘을 제공한다.

23.3.1 MBeanInfoAssembler 인터페이스

내부적으로 MBeanExporter는 노출될 각 빈의 관리 인터페이스를 정의하는 역할을 하는 org.springframework.jmx.export.assembler.MBeanInfoAssembler 인터페이스의 구현체에 위임한다. 기본 구현체인 org.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssembler는 모든 퍼블릭 프로퍼티와 메서드를 노출하는(이전 예제에서 본 것처럼) 관리 인터페이스를 정의한다. 스프링은 소스수준의 메타데이터나 다른 임의의 인터페이스를 사용해서 생성한 관리 인터페이스를 제어할 수 있는 MBeanInfoAssembler 인터페이스의 두 구현체를 추가적으로 제공한다.

23.3.2 소스 수준의 메타데이터 사용하기 (JDK 5.0 어노테이션)

MetadataMBeanInfoAssembler를 사용해서 소스 수준의 메타데이터를 사용하는 빈에 대한 관리 인터페이스를 정의할 수 있다. 메타데이터는 org.springframework.jmx.export.metadata.JmxAttributeSource 인터페이스로 캡슐화해서 읽는다. 스프링 JMX는 JDK 5.0 어노테이션 (org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource)을 사용하는 기본 구현체를 제공한다. MetadataMBeanInfoAssembler가 제대로 동작하기 위해서는 JmxAttributeSource 인터페이스의 구현체 인스턴스로 설정해야 한다.(기본값이 존재하지 않는다.)
JMX로 내보내도록 빈을 표시하려면 빈 클래스에 ManagedResource 어노테이션을 붙혀야 한다. 동작(operation)으로 노출하려는 각 메서드는 ManagedOperation 어노테이션을 붙혀야 하고 노출하려는 각 프로퍼티는 ManagedAttribute 어노테이션을 붙혀야 한다. 프로퍼티에 어노테이션을 붙힐 때는 읽기 전용이나 쓰기 전용 속성을 만들기 위해서 getter나 setter의 어노테이션을 생략할 수 있다. 아래 예제는 앞에서 본 JmxTestBean 클래스의 어노테이션을 사용한 버전이다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Java

package org.springframework.jmx;

import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedAttribute;

@ManagedResource(objectName="bean:name=testBean4", description="My Managed Bean", log=true,
    logFile="jmx.log", currencyTimeLimit=15, persistPolicy="OnUpdate", persistPeriod=200,
    persistLocation="foo", persistName="bar")
public class AnnotationTestBean implements IJmxTestBean {

  private String name;
  private int age;

  @ManagedAttribute(description="The Age Attribute", currencyTimeLimit=15)
  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  @ManagedAttribute(description="The Name Attribute",
      currencyTimeLimit=20,
      defaultValue="bar",
      persistPolicy="OnUpdate")
  public void setName(String name) {
    this.name = name;
  }

  @ManagedAttribute(defaultValue="foo", persistPeriod=300)
  public String getName() {
    return name;
  }

  @ManagedOperation(description="Add two numbers")
  @ManagedOperationParameters({
    @ManagedOperationParameter(name = "x", description = "The first number"),
    @ManagedOperationParameter(name = "y", description = "The second number")})
  public int add(int x, int y) {
    return x + y;
  }

  public void dontExposeMe() {
    throw new RuntimeException();
  }
}

이 예제에서 JmxTestBean 클래스에 ManagedResource 어노테이션이 붙어있는 것을 볼 수 있고 이 ManagedResource 어노테이션은 여러 가지 프로터티들로 설정되어 있다. MBeanExporter가 생성하는 MBean의 다양한 관점을 설정하는데 이러한 프로퍼티를 설정할 수 있고 더 자세한 내용은 나중에 Section 23.3.3, “소스 수준의 메타데이터 타입”부분에서 설명한다.
age와 name 프로퍼티도 ManagedAttribute 어노테이션이 붙을 것을 볼 수 있는데 여기서 age에는 getter에만 어노테이션이 붙어 있다. 이 어노테이션을 붙힘으로써 두 프로퍼티 모두 관리 인터페이스의 속성으로 포함되지만 age는 읽기 전용이다. 마지막으로 add(int, int) 메서드에는 ManagedOperation 어노테이션이 붙어있지만 dontExposeMe() 메서드에는 붙어있지 않다. 그래서 관리 인터페이스는 MetadataMBeanInfoAssembler를 사용할 때 add(int, int) 오퍼레이션만 가진다.
MetadataMBeanInfoAssembler를 사용하는 MBeanExporter를 설정하는 방법이 아래 설정에 나와 있다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Xml

<beans>
  <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="assembler" ref="assembler"/>
    <property name="namingStrategy" ref="namingStrategy"/>
    <property name="autodetect" value="true"/>
  </bean>

  <bean id="jmxAttributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>

  
  <bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
    <property name="attributeSource" ref="jmxAttributeSource"/>
  </bean>

  
  
  <bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
    <property name="attributeSource" ref="jmxAttributeSource"/>
  </bean>

  <bean id="testBean" class="org.springframework.jmx.AnnotationTestBean">
    <property name="name" value="TEST"/>
    <property name="age" value="100"/>
  </bean>
</beans>

여기서 MetadataMBeanInfoAssembler 빈이 AnnotationJmxAttributeSource 클래스의 인스턴스로 설정되었고 assembler 프로퍼티로 MBeanExporter에 전달되는 것을 볼 수 있다. 이것이 스프링이 노출한 MBean의 메타데이터 주도 관리 인터페이스의 장점을 취하기 위해서 필요한 전부이다.


23.3.3 소스 수준의 메타데이터 타입

다음 소스수준의 메타데이터 타입을 스프링 JMX에서 사용할 수 있다.

Table 23.2. 소스 수준의 메타데이터 타입
목적어노테이션어노테이션 타입
Class의 모든 인스턴스를 JMX가 관리하는 리소스로 표시한다.@ManagedResourceClass
메서드를 JMX 동작으로 표시한다.@ManagedOperationMethod
getter나 setter를 JMX 속성의 절반으로 표시한다.@ManagedAttributeMethod (getter와 setter 전용)
동작(operation) 파라미터에 대한 디스크립션을 정의한다.@ManagedOperationParameter와 @ManagedOperationParametersMethod





소스 수준의 메타데이터 타입에서 다음 설정 파라미터를 사용할 수 있다.

Table 23.3. 소스 수준의 메타데이터 파라미터
파라미터설명적용 대상
ObjectName관리하는 리소스의 ObjectName을 결정하기 위해서 MetadataNamingStrategy가 사용한다.ManagedResource
description리소스, 속성, 동작의 디스크립션을 설정한다.ManagedResource, ManagedAttribute, ManagedOperation, ManagedOperationParameter
currencyTimeLimitcurrencyTimeLimit 디스크립터 필드의 값을 설정한다ManagedResource, ManagedAttribute
defaultValuedefaultValue 디스크립터 필드의 값을 설정한다ManagedAttribute
loglog 디스크립터 필드의 값을 설정한다ManagedResource
logFilelogFile 디스크립터 필드의 값을 설정한다ManagedResource
persistPolicypersistPolicy 디스크립터 필드의 값을 설정한다ManagedResource
persistPeriodpersistPeriod 디스크립터 필드의 값을 설정한다ManagedResource
persistLocationpersistLocation 디스크립터 필드의 값을 설정한다ManagedResource
persistNamepersistName 디스크립터 필드의 값을 설정한다ManagedResource
name동작(operation) 파라미터의 표시 이름을 설정한다ManagedOperationParameter
index동작 파라미터의 인덱스를 설정한다ManagedOperationParameter


















23.3.4 AutodetectCapableMBeanInfoAssembler 인터페이스

더 간단한 설정을 위해서 스프링은 MBeanInfoAssembler 인터페이스를 확장해서 MBean 리소스를 자동탐지를 지원하려고 AutodetectCapableMBeanInfoAssembler 인터페이스를 도입했다. AutodetectCapableMBeanInfoAssembler 인스턴스로 MBeanExporter를 설정하면 JMX에 노출하기 위해 빈에 "투표(vote)"할 수 있다.
AutodetectCapableMBeanInfo 인터페이스의 유일한 구현체인 MetadataMBeanInfoAssembler는 ManagedResource 속성으로 표시된 모든 빈을 포함하도록 투표한다. 이 경우 기본 접근은 다음과 같은 설정의 빈 이름을 ObjectName으로 사용하는 것이다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
Xml

<beans>
  <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
    <!-- 어떤 'beans'도 명시적으로 선언하지 않았다. -->
    <property name="autodetect" value="true"/>
    <property name="assembler" ref="assembler"/>
  </bean>

  <bean id="testBean" class="org.springframework.jmx.JmxTestBean">
    <property name="name" value="TEST"/>
    <property name="age" value="100"/>
  </bean>

  <bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
    <property name="attributeSource">
        <bean class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
    </property>
  </bean>
</beans>

이 설정에서 어떤 빈도 MBeanExporter에 전달하지 않았다. 하지만 ManagedResource 속성으로 표시되었고 MetadataMBeanInfoAssembler가 ManagedResource를 탐지해서 포함시키도록 투표하므로 JmxTestBean가 등록될 것이다. 이 접근방법의 유일한 문제점은 JmxTestBean의 이름이 비즈니스적인 의미를 갖게 되었다는 점이다. Section 23.4, “빈의 ObjectName 제어”에서 정의된 것처럼 ObjectName를 생성하는 기본 동작을 변경해서 이 문제를 해결할 수 있다.


23.3.5 자바 인터페이스를 사용한 관리 인터페이스 정의

MetadataMBeanInfoAssembler에 추가적으로 스프링도 인터페이스 컬렉션에 정의된 메서드 세트에 기반해서 노출되는 메서드와 프로퍼티에 제약을 가할 수 있는 InterfaceBasedMBeanInfoAssembler를 포함하고 있다.
MBean을 노출하는 표준 메카니즘이 인터페이스와 간단한 작명 계획을 사용하기는 하지만 InterfaceBasedMBeanInfoAssembler는 작명 관례에 대한 요구사항을 제거해서 이 기능을 확장함으로써 하나 이상의 인터페이스를 사용하고 빈이 MBean 인터페이스를 구현하지 않다도 되게 한다.
앞에서 본 JmxTestBean 클래스에 대한 관리 인터페이스를 정의할 때 이 인터페이스의 사용을 고려해 봐라.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
Java

public interface IJmxTestBean {

  public int add(int x, int y);

  public long myOperation();

  public int getAge();

  public void setAge(int age);

  public void setName(String name);

  public String getName();
}

이 인터페이스는 JMX MBean의 동작과 속성으로 노출될 메서드와 프로퍼티를 정의한다. 아래 코드는 관리 인터페이스의 정의로 이 인터페이스를 사용하도록 스프릥 JMX를 설정하는 방법을 보여준다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Xml

<beans>
  <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="beans">
      <map>
        <entry key="bean:name=testBean5" value-ref="testBean"/>
      </map>
    </property>
    <property name="assembler">
      <bean class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
        <property name="managedInterfaces">
          <value>org.springframework.jmx.IJmxTestBean</value>
        </property>
      </bean>
    </property>
  </bean>

  <bean id="testBean" class="org.springframework.jmx.JmxTestBean">
    <property name="name" value="TEST"/>
    <property name="age" value="100"/>
  </bean>
</beans>

모든 빈에 대한 관리 인터페이스를 생성할 때 IJmxTestBean 인터페이스를 사용하도록 설정한 InterfaceBasedMBeanInfoAssembler를 볼 수 있다. InterfaceBasedMBeanInfoAssembler가 처리한 빈은 JMX 관리 인터페이스를 생성하는데 사용하는 인터페이스를 구현할 필요가 없다는 점을 이해하는 것이 중요하다.
위의 경우 모든 빈에 대한 모든 관리 인터페이스를 생성하는데 IJmxTestBean 인터페이스를 사용한다. 많은 경우 이는 원하는 동작이 아니고 빈마다 다른 인터페이스를 사용하길 원할 것이다. 이러한 경우에는 interfaceMappings 프로퍼티로(각 엔트리는 빈의 이름이고 엔트리의 값은 해당 빈에 사용할 인터페이스 이름을 콤마로 구분한 목록이다.) InterfaceBasedMBeanInfoAssembler Properties 인스턴스를 전달할 수 있다.
managedInterfaces와 interfaceMappings 프로퍼티로 지정한 관리 인터페이스가 없으면 InterfaceBasedMBeanInfoAssembler가 빈에 반영되고 관리인터페이스를 생성하는 해당 빈이 구현한 모든 인터페이스를 사용할 것이다.

23.3.6 MethodNameBasedMBeanInfoAssembler 사용하기

MethodNameBasedMBeanInfoAssembler로 JMX가 속성과 동작으로 노출할 메서드명의 목록을 지정할 수 있다. 아래 코드는 이에 대한 설정 예시를 보여 준다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
Xml

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
  <property name="beans">
    <map>
      <entry key="bean:name=testBean5" value-ref="testBean"/>
    </map>
  </property>
  <property name="assembler">
    <bean class="org.springframework.jmx.export.assembler.MethodNameBasedMBeanInfoAssembler">
      <property name="managedMethods">
        <value>add,myOperation,getName,setName,getAge</value>
      </property>
    </bean>
  </property>
</bean>

이 코드에서 add와 myOperation 메서드가 JMX 동작으로 노출되고 getName(), setName(String), getAge()는 JMX 속성으로 노출되는 것을 볼 수 있다. 위 코드에서 메서드는 JMX에 노출되는 빈에 매핑된다. 빈과 빈(bean-by-bean)의 메서드 노출을 제어하려면 빈 이름을 메서드 이름의 목록에 매핑하기 위해 MethodNameMBeanInfoAssembler의 methodMappings 프로퍼티를 사용해라.

[UFC] UFC 마감뉴스..


출처 : SPOTV NEWS