Spring내 JMX지원은 당신에게 쉽게 기능을 제공하고 당신의 Spring애플리케이션을 JMS내부구조와 투명하게 통합한다. 특별히 Spring JMS는 4가지의 핵심적인 기능을 제공한다.
JMS MBean처럼 Spring bean의 자동 등록
당신 bean의 관리 인터페이스를 제어하기 위한 유연한 기법
원격, JSR-160 연결자를 넘어선 MBean의 명시적인 제시(exposure)
local과 remote MBean자원 모두의 간단한 프록시화
이러한 기능들은 Spring이나 JMS인터페이스 그리고 클래스를 위한 애플리케이션 컴포넌트를 커플링하지 않는 작업을 위해 다지인되었다. 물론 당신 애플리케이션 클래스의 대부분을 위해 Spring JMX기능의 장점을 가져가기 위한 Spring이나 JMS를 인식할 필요는 없다.
Spring JMX 프레임워크내 핵심(core) 클래스는 MBeanExporter이다. 이 클래스는 당신의 Spring bean을 가져오고 그것들을 JMX MBeanServer에 등록하는 책임이 있다. 예를 들어, 밑에서 보여지는 간단한 bean클래스를 보자.
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(); } }
JMX MBean의 속성과 작동(operation)처럼 이 bean의 프라퍼티와 메소드를 드러내기 위해 당신은 당신의 설정파일내 MBeanExporter 클래스의 인스턴스를 간단하게 설정하고 밑에서 보여지는것처럼 bean으로 전달한다.
<beans> <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="beans"> <map> <entry key="bean:name=testBean1"> <ref local="testBean"/> </entry> </map> </property> </bean> <bean id="testBean" class="org.springframework.jmx.JmxTestBean"> <property name="name"> <value>TEST</value> </property> <property name="age"> <value>100</value> </property> </bean> </beans>
여기서 중요한 정의는 exporter bean이다. beans 프라퍼티는 JMX MBeanServer에 내보내져야하는 당신의 bean이 어떠한 것인지 MBeanExporter에 알리기 위해 사용된다. beans 프라퍼티는 Map의 타입이고 게다가 당신은 내보낼 bean을 설정하기 위한 <map> 과 <entry> 태그들을 사용한다. 디폴트 설정에서 Map의 항목의 key는 항목의 값인 bean을 위한 ObjectName처럼 사용된다. 이 행위는 XXX부분에서 언급된것처럼 변경될수 있다.
testBean bean를 설정하는것은 ObjectNamebean:name=testBean1 하위의 JMX MBean처럼 드러난다. bean의 모든 public성격의 프라퍼티는 속성과 모든 public성격의 메소드(Object내 정의된)가 작동(operation)처럼 드러나는 만큼 드러난다.
위에서 보여지는 설정은 애플리케이션이 이미 실행중인 하나의 MBeanServer를 가진 환경에서 실행중이라는것을 가정한다. 이 경우 Spring은 실행중인 MBeanServer를 할당하고 그것과 함께 당신의 bean을 등록할것이다. 이것은 당신의 애플리케이션이 자신만의 MBeanServer를 가진 톰캣이나 IBM웹스피어와 같은 컨테이너내부에서 실행중일때 유용하다.
어쨌든, 이 접근법은 독립형(standalone) 환경이나 MBeanServer를 제공하지 않는 컨테이너내부에서 실행될때는 필요없다. 이것을 극복하기 위해 당신은 당신의 설정을 위한 org.springframework.jmx.support.MBeanServerFactoryBean의 인스턴스를 추가하여 선언적으로 MBeanServer인스턴스를 생성할수 있다. 당신은 또한 MBeanServer가 MBeanExporter의 서버 프라퍼티를 셋팅하기 위해서 MBeanServerFactoryBean를 사용하는지 확인할수 있다. 이것은 밑에서 보여준다.
<beans> <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="beans"> <map> <entry key="bean:name=testBean1"> <ref local="testBean"/> </entry> </map> </property> <property name="server"> <ref local="mbeanServer"/> </property> </bean> <bean id="testBean" class="org.springframework.jmx.JmxTestBean"> <property name="name"> <value>TEST</value> </property> <property name="age"> <value>100</value> </property> </bean> <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean"/> </beans>
여기, MBeanServer의 인스턴스는 MBeanServerFactoryBean에 의해 생성되고 서버 프라퍼티를 통해 MBeanExporter에 제공된다. 당신이 당신 자신의 MBeanServer를 제공할때 MBeanServer는 수행을 할당하기 위해 시도하지 않을것이다. 이 작업을 정확하게 하기 위해 당신은 클래스패스에 JMX구현물의 위치시켜야만 한다.
만약 당신이 MBeanExporter을 가진 bean을 설정한다면 그것은 늦은(lazy) 초기화를 위해 설정되고 그 다음 MBeanExporter는 이 규칙을 깨지 않을것이고 bean을 인스턴스화하는것을 피할것이다. 대신 이것은 MBeanServer을 가지고 프록시를 등록할것이고 발생한 프록시의 첫번째 호출까지 BeanFactory로 부터 bean을 얻는것을 미룰것이다.
MBeanExporter와 이미 유효한 MBean을 통해 내보내어진 bean은 Spring으로 부터 더이상의 조정(intervention) 없는 MBeanServer처럼 등록된 이미 유요한 MBean이다. MBean은 autodetect 프라퍼티를 true로 셋팅하여 MBeanExporter에 의해 자동적으로 감지될수 있다.
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="autodetect"> <value>true</value> </property> </bean> <bean name="spring:mbean=true" class="org.springframework.jmx.export.TestDynamicMBean"/>
여기에, spring:mbean=true 라고 불리는 bean은 벌써 유효한 JMX MBean이고 Spring에 의해 자동적으로 등록된것이다. 디폴트에 의하면 JMX등록을 위해 자동감지된 bean은 ObjectName처럼 사용된 bean이름을 가진다. 이 행위는 XXX부분에서 상세화된것처럼 오버라이드될수 있다.
이전의 예제에서 당신은 모든 public성격의 프라퍼티와 메소드를 가진 bean의 관리 인터페이스를 조금 제어했다. 이 문제를 해결하기 위해 Spring JMX는 당신 bean의 관리 인터페이스를 제어하기 위한 포괄적이고 확장가능한 기법을 제공한다.
이 장면뒤에 MBeanExporter는 드러난 각각의 bean의 관리 인터페이스를 명시하는 책임을 지닌 org.springframework.jmx.export.assembler.MBeanInfoAssembler 인터페이스의 구현물을 위임한다. 디폴트 구현물인 org.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssembler는 당신이 이전 예제에서 본것처럼 모든 public성격의 프라퍼티와 메소드를 드러내는 인터페이스를 간단하게 정의한다. Spring은 소스레벨 메타데이타(metadata)나 어느 임의의 인터페이스를 사용하여 관리 인터페이스를 제어하도록 허용하는 MBeanInfoAssembler 인터페이스의 두개의 추가적인 구현물을 제공한다.
MetadataMBeanInfoAssembler를 사용하여 당신은 소스레벨 메타데이타를 사용하는 당신의 bean을 위한 관리 인터페이스를 정의할수 있다. 메타데이타 읽기는 org.springframework.jmx.export.metadata.JmxAttributeSource인터페이스에 의해 캡슐화된다. 특히 Spring JMX는 Commons Attributes를 위한 AttributesJmxAttributeSource인터페이스와 JDK 5.0 annotations을 위한 AnnotationsAttributeSource인터페이스의 두가지 구현물을 위한 지원을 제공한다. MetadataMBeanInfoAssembler는 정확하게 기능을 위한 JmxAttributeSource의 구현물이 설정되어야만 한다. 이 예제를 위해 우리는 Commons Attributes 메타데이타 접근법을 사용할것이다.
JMX로 내보내기위한 bean을 표시하기 위해 당신은 ManagedResource 속성으로 bean의 클래스에 주석(annotate)을 달아야 한다. Commons Attributes 메타데이타 접근법의 경우 이 클래스는 org.springframework.jmx.metadata 패키지에서 찾을수 있다. 당신이 작동(operation)처럼 드러내기를 바라는 각각의 메소드는 ManagedOperation속성으로 표시되어야만 하고 당신이 드러내길 바라는 각각의 프라퍼티는 ManagedAttribute속성으로 표시되어야 한다. 프라퍼티를 표시할때 당신은 쓰기전용(write-only)이나 읽기전용(read-only) 속성을 위한 getter이나 setter를 생략할수 있다.
밑의 예제는 당신이 좀더 먼저 본 JmxTestBean 클래스가 Commons Attributes 메타데이타로 표시된다는것을 보여준다.
package org.springframework.jmx; /** * @@org.springframework.jmx.export.metadata.ManagedResource * (description="My Managed Bean", objectName="spring:bean=test", * log=true, logFile="jmx.log", currencyTimeLimit=15, persistPolicy="OnUpdate", * persistPeriod=200, persistLocation="foo", persistName="bar") * */ public class JmxTestBean implements IJmxTestBean { private String name; private int age; /** * @@org.springframework.jmx.export.metadata.ManagedAttribute * (description="The Age Attribute", currencyTimeLimit=15) */ public int getAge() { return age; } public void setAge(int age) { this.age = age; } /** * @@org.springframework.jmx.export.metadata.ManagedAttribute * (description="The Name Attribute", currencyTimeLimit=20, * defaultValue="bar", persistPolicy="OnUpdate") */ public void setName(String name) { this.name = name; } /** * @@org.springframework.jmx.export.metadata.ManagedAttribute * (defaultValue="foo", persistPeriod=300) */ public String getName() { return name; } /** * @@org.springframework.jmx.export.metadata.ManagedOperation * (description="Add Two Numbers Together") */ public int add(int x, int y) { return x + y; } public void dontExposeMe() { throw new RuntimeException(); } }
여기서 당신은 JmxTestBean 클래스가 ManagedResource 속성으로 표시되고 이 ManagedResource 속성이 프라퍼티의 세트로 설정된다. 이러한 프라퍼티는 MBeanExporter에 의해 생성되는 MBean의 다양한 양상(aspect)로 설정하기 위해 사용될수 있고 Section 20.3.4, “소스레벨 메타데이타 타입들”에서 나중에 좀더 상세하게 설명된다.
당신은 ManagedAttribute속성으로 표시되는 age와 name속성들 모두 알릴것이지만 age 프라퍼티의 경우 오직 getter만이 표시된다. 이것은 속성처럼 관리 인터페이스내 포함되기 위한 이헌 프라퍼티 모두를 야기할것이다. 그리고 age속성은 읽기전용이다.
마지막으로 당신은 dontExposeMe() 메소드가 표시되지 않기 때문에 add(int, int)메소드는 ManagedOperation속성으로 표시된다는것을 알릴것이다. 이것은 MetadataMBeanInfoAssembler를 사용할때 오직 하나의 작업(operation)인 add(int, int)을 포함하기 위한 관리 인터페이스를 야기할것이다.
아래의 코드는 당신이 MetadataMBeanInfoAssembler를 사용하기 위한 MBeanExporter를 설정하는 방법을 보여준다.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="beans"> <map> <entry key="bean:name=testBean1"> <ref local="testBean"/> </entry> </map> </property> <property name="assembler"> <ref local="assembler"/> </property> </bean> <bean id="testBean" class="org.springframework.jmx.JmxTestBean"> <property name="name"> <value>TEST</value> </property> <property name="age"> <value>100</value> </property> </bean> <bean id="attributeSource" class="org.springframework.jmx.export.metadata.AttributesJmxAttributeSource"/> <bean id="assembler" class="org.springframework.jmx.assembler.MetadataMBeanInfoAssembler"> <property name="attributeSource"> <ref local="attributeSource"/> </property> </bean> </beans>
여기서 당신은 MetadataMBeanInfoAssembler가 AttributesJmxAttributeSource의 인스턴스로 설정되고 assembler프라퍼티를 통해 MBeanExporter로 전달하는것을 볼수 있다. 이것은 당신의 Spring-노출 MBean을 위한 데타데이타-기반 관리 인터페이스의 장점을 가져오기 위해 요구되는 모든것이다.
관리 인터페이스 정의를 위한 JDK 5.0 annotation의 사용을 가능하게 하기 위해 Spring은 Commons Attribute속성 클래스와 그것들을 읽기 위해 MBeanInfoAssembler을 허용하는 JmxAttributeSource, AnnotationsJmxAttributeSource의 구현물을 반영하는 annotation의 세트를 제공한다.
밑의 예제는 관리 인터페이스가 정의된 JDK 5.0 annotation을 가진 bean을 보여준다.
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 Together") public int add(int x, int y) { return x + y; } public void dontExposeMe() { throw new RuntimeException(); } }
당신이 볼수 있는것처럼 메타데이타 정의의 기본적인 문법보다 조금 변경된것을 볼수 있다. 그 장면뒤에서 이 접근법은 JDK 5.0 annotation이 Commons Attributes에 의해 사용되는 클래스로 형변환되기 때문에 수행시 조금더 느리다. 어쨌든 이것은 한번에 한한(one-off) 비용이고 JDK 5.0 annotations은 당신에게 컴파일시각 체크(compile-time checking)의 이득을 부여한다.
다음의 소스레벨 메타데이타 타입들은 Spring JMX내에서 사용되기 위해 사용가능하다.
Table 20.1. 소스-레벨 메타데이타 타입들
목적 | Commons Attributes 속성 | JDK 5.0 Annotation | Annotation/Attribute 타입 |
---|---|---|---|
자원을 관리하는 JMX처럼 클래스의 모든 인사턴스를 표시하기(Mark) | ManagedResource | @ManagedResource | Class |
JMX작동(operation)처럼 메소드 표시하기 | ManagedOperation | @ManagedOperation | Method |
JMX속성의 절반처럼 getter이나 setter 표시하기 | ManagedAttribute | @ManagedAttribute | Method (only getters and setters) |
작동 파라미터를 위한 설명 정의하기 | ManagedOperationParameter | @ManagedOperationParameter 와 @ManagedOperationParameters | Method |
다음의 설정 파라미터는 소스레벨 메타데이타 타입들의 사용을 위해 사용가능하다.
Table 20.2. 소스레벨 메타데이타 파라미터
파라미터 | 설명 | 적용 |
---|---|---|
objectName | 관리되는 자원의 ObjectName를 결정하기 위한 MetadataNamingStrategy에 의해 사용 | ManagedResource |
description | 자원, 속성 또는 작동(operation)의 친숙한 설명 셋팅하기 | ManagedResource, ManagedAttribute, ManagedOperation, ManagedOperationParameter |
currencyTimeLimit | currencyTimeLimit 서술자 필드의 값을 셋팅하기 | ManagedResource, ManagedAttribute |
defaultValue | defaultValue 서술자 필드의 값을 셋팅하기 | ManagedAttribute |
log | log 서술자 필드의 값을 셋팅하기 | ManagedResource |
logFile | logFile 서술자 필드의 값을 셋팅하기 | ManagedResource |
persistPolicy | persistPolicy 서술자 필드의 값을 셋팅하기 | ManagedResource |
persistPeriod | persistPeriod 서술자 필드의 값을 셋팅하기 | ManagedResource |
persistLocation | persistLocation 서술자 필드의 값을 셋팅하기 | ManagedResource |
persistName | persistName 서술자 필드의 값을 셋팅하기 | ManagedResource |
name | 작동(operation) 파라미터의 표시명(display name)을 셋팅하기 | ManagedOperationParameter |
index | 작동(operation) 파라미터의 인덱스 셋팅하기 | ManagedOperationParameter |
좀더 간단한 설정을 위해, Spring은 MBean자원의 자동감지를 위한 지원을 추가하는 MBeanInfoAssembler 인터페이스를 확장하는 AutodetectCapableMBeanInfoAssembler인터페이스를 소개한다. 만약 당신이 AutodetectCapableMBeanInfoAssembler의 인스턴스를 사용해서 MBeanExporter을 설정한다면 이것은 JMX에 드러내기 위한 bean의 포함(inclusion)에 '결정(vote)'하는것을 허용한다.
특별히, AutodetectCapableMBeanInfo 의 구현물만이 ManagedResource속성으로 표시되는 bean을 포함하기 위해 결정(vote)할 MetadataMBeanInfoAssembler이다. 이 경우 디폴트 접근법은 이것과 같은 설정의 결과를 보이는 ObjectName처럼 bean이름을 사용하는 것이다.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="assembler"> <ref local="assembler"/> </property> </bean> <bean id="bean:name=testBean1" class="org.springframework.jmx.JmxTestBean"> <property name="name"> <value>TEST</value> </property> <property name="age"> <value>100</value> </property> </bean> <bean id="attributeSource" class="org.springframework.jmx.export.metadata.AttributesJmxAttributeSource"/> <bean id="assembler" class="org.springframework.jmx.assembler.MetadataMBeanInfoAssembler"> <property name="attributeSource"> <ref local="attributeSource"/> </property> </bean> </beans>
이 설정내에서 MBeanExporter에 전달되는 bean은 없다. 어쨌든 JmxTestBean은 이것이 ManagedResource 속성으로 표시된 이후 등록될것이고 MetadataMBeanInfoAssembler는 이것을 감지하고 이것을 포함하기 위해 결정한다. 이 접근법이 가진 문제점은 JmxTestBean의 이름이 현재 비지니스 의미(meaning)를 가진다는것이다. 당신은 Section 20.4, “당신의 bean을 위한 ObjectName 제어하기”에서 정의되는것처럼 ObjectName 생성을 위한 디폴트 행위를 변경하여 이 문제를 해결할수 있다.
MetadataMBeanInfoAssembler에 추가적으로, Spring은 당신에게 인터페이스의 집합(collection)내 정의된 메소드의 세트에 기반하여 드러나는 메소드와 프라퍼티를 강요하는것을 허용하는 InterfaceBasedMBeanInfoAssembler을 포함한다.
비록 MBean을 드러내기 위한 표준적인 기법이 인터페이스와 간단한 명명 개요(scheme)를 사용하는 것이라고 하더라도 InterfaceBasedMBeanInfoAssembler는 당신에게 하나의 인터페이스보다 많은 수를 사용하는것을 허용하고 MBean인터페이스를 구현하는 당신의 bean을 위한 필요성을 제거하는 명명규칙을 위한 필요성을 제거하여 이 기능을 확장한다.
당신이 좀더 일찍 본 JmxTestBean클래스를 위한 관리 인터페이스를 정의하기 위해 사용되는 이 인터페이스를 보라.
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의 작동(operation)과 속성처럼 드러날 메소드와 프라퍼티를 정의한다. 밑의 코드는 관리 인터페이스를 위한 정의처럼 이 인터페이스를 사용하기 위해 Spring JMX를 설정하는 방법을 보여준다.
<beans> <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="beans"> <map> <entry key="bean:name=testBean5"> <ref local="testBean"/> </entry> </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</value> </property> <property name="age"> <value>100</value> </property> </bean> </beans>
여기서 당신은 어느 bean을 위한 관리 인터페이스를 생성할때 InterfaceBasedMBeanInfoAssembler가 IJmxTestBean 인터페이스를 사용하기 위해 설정되는것을 볼수 있다. InterfaceBasedMBeanInfoAssembler에 의해 처리되는 bean이 JMX 관리 인터페이스를 생성하기 위해 사용되는 인터페이스를 구현하는것을 요구하지 않는 것을 이해하는것은 중요하다.
위 경우에, IJmxTestBean 인터페이스는 모든 bean을 위한 모든 관리 인터페이스를 생성하기 위해 사용된다. 많은 경우 이것은 바람직한 행위가 아니며 당신은 다른 bean을 위해 다른 인터페이스를 사용하길 원할것이다. 이 경우 당신은 각각의 항목의 key는 bean이름이고 각각의 항목의 값은 bean사용을 위한 인터페이스 이름의 콤마로 분리된 리스트인 interfaceMappings 프라퍼티를 통해 InterfaceBasedMBeanInfoAssembler에 Properties를 전달할수 있다.
만약 managedInterfaces 나 interfaceMappings 프라퍼티를 통해 명시된 관리 인터페이스가 없다면 InterfaceBasedMBeanInfoAssembler는 bean에 반영할것이고 관리 인터페이스를 생성하기 위한 bean에 의해 구현되는 모든 인터페이스를 사용할것이다.
MethodNameBasedMBeanInfoAssembler는 당신에게 속성과 작동(operation)처럼 JMX에 드러날 메소드명의 리스트를 명시하는것을 허용한다. 밑의 코드는 이것을 위한 샘플 설정을 보여준다.
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="beans"> <map> <entry key="bean:name=testBean5"> <ref local="testBean"/> </entry> </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작동(operation)처럼 드러나고 getName, setName 그리고 getAge 메소드가 선호되는 JMX속성의 절반처럼 드러날것을 볼수 있다. 위 코드에서 메소드 맵핑은 JMX에 드러나는 bean에 적용한다. bean기초에 의해 bean에서 드러나는 메소드를 제어하는 것은 메소드명의 리스트를 위한 bean이름을 맵핑하기 위한 MethodNameMBeanInfoAssembler의 methodMappings프라퍼티를 사용한다.
MBeanExporter 는 이것이 등록하는 각각의 bean을 위한 ObjectName를 얻기 위해 ObjectNamingStrategy의 구현물로 위임한다. 디폴트 구현물인 KeyNamingStrategy는 디폴트에 의해 ObjectName처럼 beans 의 key Map을 사용할것이다. 추가적으로 KeyNamingStrategy는 ObjectName을 분석하기 위한 Properties 파일내 항목을 위한 beans의 key Map을 맵핑할수 있다. KeyNamingStrategy에 추가적으로 Spring은 두가지(bean의 동일성에 기반하는 ObjectName을 빌드하는 IdentityNamingStrategy와 ObjectName을 얻기 위한 소스레벨 메타데이타를 사용하는 MetadataNamingStrategy)의 추가적인 ObjectNamingStrategy 구현물을 제공한다.
당신은 당신 자신의 KeyNamingStrategy 인스턴스를 설정할수 있고 bean key를 사용하는것보다 Properties 인스턴스로부터 ObjectName를 읽어서 이것을 설정한다. KeyNamingStrategy는 bean key에 대응하는 key를 가진 Properties내 항목을 위치시키는 시도를 할것이다. 만약 어떠한 항목이 발견되지 않거나 Properties 인스턴스가 null이라면 그 bean key는 자체적으로 사용된다.
밑의 코드는 KeyNamingStrategy를 위한 샘플 설정을 보여준다.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="beans"> <map> <entry key="testBean"> <ref local="testBean"/> </entry> </map> </property> <property name="namingStrategy"> <ref local="namingStrategy"/> </property> </bean> <bean id="testBean" class="org.springframework.jmx.JmxTestBean"> <property name="name"> <value>TEST</value> </property> <property name="age"> <value>100</value> </property> </bean> <bean id="namingStrategy" class="org.springframework.jmx.export.naming.KeyNamingStrategy"> <property name="mappings"> <props> <prop key="testBean">bean:name=testBean1</prop> </props> </property> <property name="mappingLocations"> <value>names1.properties,names2.properties</value> </property> </bean </beans>
여기서 KeyNamingStrategy의 인스턴스는 mapping프라퍼티에 의해 정의된 Properties 인스턴스로 부터 통합된 Properties 인스턴스와 mapping 프라퍼티에 의해 정의된 경로내 위치한 프라퍼티 파일들로 설정된다. 이 설정에서 testBean bean은 이 항목이 bean key에 대응되는 key를 가진 Properties 인스턴스내 항목이 된 이후 ObjectName bean:name=testBean1가 주어질것이다.
Properties 인스턴스내 발견될수 있는 항목이 없다면 bean key는 ObjectName처럼 사용된다.
MetadataNamingStrategy 는 ObjectName을 생성하기 위한 각각의 bean의 ManagedResource속성의 objectName프라퍼티를 사용한다. 밑의 코드는 MetadataNamingStrategy를 위한 설정을 보여준다.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="beans"> <map> <entry key="testBean"> <ref local="testBean"/> </entry> </map> </property> <property name="namingStrategy"> <ref local="namingStrategy"/> </property> </bean> <bean id="testBean" class="org.springframework.jmx.JmxTestBean"> <property name="name"> <value>TEST</value> </property> <property name="age"> <value>100</value> </property> </bean> <bean id="attributeSource" class="org.springframework.jmx.export.metadata.AttributesJmxAttributeSource"/> <bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy"> <property name="attributeSource"> <ref local="attributeSource"/> </property> </bean> </beans>
원격 접속을 위해 Spring JMX 모듈은 서버측과 클라이언트측 연결자를 생성하기 위한 org.springframework.jmx.support패키지내 두가지의 FactoryBean 구현물을 제공한다.
Spring JMX가 JSR-160 JMXConnectorServer를 생성, 시작 그리고 드러내기 위해 다음의 설정을 사용한다.
<bean id="serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean"/>
디폴트에 의해 ConnectorServerFactoryBean은 "service:jmx:jmxmp://localhost:9875"를 바운드하는 JMXConnectorServer를 생성한다. serverConnector bean은 로컬호스트, 9875포트의 jmxmp 프로토콜을 통해 클라이언트로 로컬 MBeanServer를 드러낸다. JMXMP 프로토콜은 JSR 160에 의해 선택적으로 표시된다는것을 알라. 현재 인기있는 오픈소스 구현물인 MX4J와 자바 5.0에 의해 제공되는 것은 JMXMP를 지원하지 않는다.
다른 URL을 명시하고 MBeanServer를 가진 JMXConnectorServer를 등록하는 것은 serviceUrl 과 objectName 프라퍼티를 사용한다.
<bean id="serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean"> <property name="objectName"> <value>connector:name=rmi</value> </property> <property name="serviceUrl"> <value>service:jmx:rmi://localhost:9875</value> </property> </bean>
만약 objectName 프라퍼티가 셋팅된다면 Spring은 ObjectName하위의 MBeanServer으로 당신의 연결자를 자동적으로 등록할것이다. 밑의 예제는 당신이 JMXConnector를 생성할때 ConnectorServerFactoryBean로 전달할수 있는 파라미터의 전체 세트를 보여준다.
<bean id="serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean"> <property name="objectName"> <value>connector:name=iiop</value> </property> <property name="serviceUrl"> <value>service:jmx:iiop://localhost:9875</value> </property> <property name="threaded"> <value>true</value> </property> <property name="daemon"> <value>true</value> </property> <property name="environment"> <map> <entry key="someKey"> <value>someValue</value> </entry> </map> </property> </bean>
이러한 프라퍼티에 대한 좀더 상세한 정보를 JavaDoc을 보라.
MBeanServer을 가능하게 하는 원격 JSR-160을 위해 MBeanServerConnection을 생성하는 것은 밑에서 보여지는것처럼 MBeanServerConnectionFactoryBean을 사용한다.
<bean id="clientConnector" class="org.springframework.jmx.support.MBeanServerConnectionFactoryBean"> <property name="serviceUrl"> <value>service:jmx:rmi://localhost:9875</value> </property> </bean>
JSR-160 은 클라이언트와 서버간에 이루어지는 통신의 방법을 위한 확장을 허락한다. 위 예제는 JSR-160(IIOP 와 JRMP) 와 선택적인 JMXMP에 의해 요구되는 필수 RMI-기반의 구현물을 사용하는것이다. 다른 제공자(provider)와 MX4J와 같은 구현물을 사용하여 당신은 SOAP, Hessian, 간단한 HTTP나 SSL곳곳의 Burlap, 그리고 다른것들같은 프로토콜의 장점을 가질수 있다.
<bean id="serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean"> <property name="objectName"> <value>connector:name=burlap</value> </property> <property name="serviceUrl"> <value>service:jmx:burlap://localhost:9874</value> </property> </bean>
MX4J가 사용되는 이 예제를 위한 좀더 다양한 정보를 위해서는 MX4J문서를 보라.
Spring JMX는 당신에게 로컬또는 원격 MBeanServer내 등록된 MBean에 대한 호출 경로를 재정의하는 프록시를 생성하는것을 허용한다. 이러한 프록시는 당신에게 당신의 MBean과 상호작용할수 있는 것을 통해 표준적인 자바 인터페이스를 제공한다. 밑의 코드는 로컬 MBeanServer내에서 실행중인 MBean을 위한 프록시를 설정하는 방법을 보여준다.
<bean id="proxy" class="org.springframework.jmx.access.MBeanProxyFactoryBean"> <property name="objectName"> <value>bean:name=testBean</value> </property> <property name="proxyInterface"> <value>org.springframework.jmx.IJmxTestBean</value> </property> </bean>
여기서 당신은 프록시가 ObjectName(bean:name=testBean)하위에 등록된 MBean을 위해 생성되는것을 볼수 있다. 프록시가 구현할 인터페이스의 세트는 proxyInterfaces 프라퍼티에 의해 제어되고 MBean의 작동(operation)과 속성을 위한 인터페이스의 메소드와 프라퍼티를 맵핑하기 위한 규칙은 InterfaceBasedMBeanInfoAssembler에 의해 사용되는 규칙과 같다.
MBeanProxyFactoryBean은 MBeanServerConnection을 통해 접근가능한 MBean을 위한 프록시를 생성할수 있다. 디폴트에 의해 로컬 MBeanServer는 위치되고 사용되지만 당신은 이것을 오버라이드 할수 있고 원격 MBean을 위한 프록시 지점(pointing)을 위해 허용하는 원격 MBeanServer를 위한 MBeanServerConnection 지점(pointing)을 제공할수 있다.
<bean id="clientConnector" class="org.springframework.jmx.support.MBeanServerConnectionFactoryBean"> <property name="serviceUrl"> <value>service:jmx:rmi://remotehost:9875</value> </property> </bean> <bean id="proxy" class="org.springframework.jmx.access.MBeanProxyFactoryBean"> <property name="objectName"> <value>bean:name=testBean</value> </property> <property name="proxyInterface"> <value>org.springframework.jmx.IJmxTestBean</value> </property> </bean>
여기서 당신은 우리가 MBeanServerConnectionFactoryBean을 사용하여 원격 머신을 위한 MBeanServerConnection 지점을 생성하는것을 볼수 있다. 이 MBeanServerConnection은 server 프라퍼티를 통해 MBeanProxyFactoryBean으로 전달된다. 생성된 프록시는 MBeanServerConnection를 통해 MBeanServer로 모든 호출을 전달할것이다.