| 고친 과정 | |
|---|---|
| 고침 $Revision: 1.1 $ | $Date: 2002/04/10 14:11:51 $ |
MX4J는 소스코드내의 고유의 javadoc 주석을 이용하여 MBean 인터페이스와 description 소스를 생성하는 방법을 제공한다. 소스 생성은 XDoclet을 사용하여 이루어 진다. 이 기능의 설명에 대해서는 배포본의 예제를 보는 것이 좋다.
소스코드를 생성하기 위해서는 XDoclet이 모든 라이브러리 파일들이 있는 디렉토리에 복사되어 있어야 한다. 대개 프로젝트의 하위디렉토리로 lib 디렉토리이다. 이제부터 Ant 변수 ${lib}로 참고될 것이다. Ant 작업에 path가 정의되어 있어야 한다. 예제는 아래와 같다 :
예 5.6. xdoclet 클래스패스와 Ant task를 정의하기
<path id="xdoclet">
<pathelement location="${lib}/xdoclet.jar"/>
<pathelement location="${lib}/log4j.jar"/>
<pathelement location="${ANT_HOME}/lib/ant.jar"/>
<pathelement location="${build}"/>
</path>
<taskdef name="jmxdoclet" classname="xdoclet.jmx.JMXDocletTask">
<classpath refid="xdoclet"/>
</taskdef>
ant 컴파일 타겟을 compilation라고 부르기로 하자. MBean 인터페이스와 설명에 대한 소스코드를 생성하는 가장 쉬운 방법은 아래와 같이 정의된 generateJMX 타겟에 의존하는(depends) compilation 타겟을 만드는 것이다.
예 5.7.
<target name="generate-jmx" depends="init">
<!-- Generate the MBean interfaces and descriptions -->
<jmxdoclet sourcepath="${src}" destdir="${src}" classpathref="xdoclet" force="yes">
<fileset dir="${src}">
<include name="com/xtremejava/webos/**" />
</fileset>
<!-- Create the {0}MBean interface for the MBean -->
<mbeaninterface/>
<!-- Create the MX4J specific description adaptor class for the MBean -->
<mx4jdescription />
<!-- Generate the mlet file -->
<mlet destinationFile="mbeans.mlet" destdir="conf"/>
</jmxdoclet>
</target>
몇개안되는 javadoc 태그가 MBean의 인터페이스와 설명을 어떻케 생성할 것인지를 명시하기 위해 사용될 수 있다.
표 5.1. Javadoc tag의 목록
| Javadoc 태그 이름 | 의미 |
|---|---|
| @jmx:mbean | 이 태그는 MBean의 이름과 그 설명을 정의하기 위해서 사용한다. 클래스 레벨에서 정의되어야 한다. |
| @jmx:managed-constructor | 생성자와 그 설명을 정의하기 위해서 사용된다. 생성자 레벨에서 정의되어야 한다. |
| @jmx:managed-constructor-parameter | 생성자 속성의 이름과 인덱스와 설명을 정의하기 위해서 사용한다. 생성자 레벨에서 정의되어야한다. |
| @jmx:managed-operation | 오퍼레이션의 이름과 그 설명을 정의하기 위해서 사용된다. 메소드 레벨에서 정의되어야 한다. |
| @jmx:managed-operation-parameter | 오퍼레이션의 속성, 인덱스, 설명을 정의하기 위해서 사용된다. 메소드 레벨에서 정의되어야 한다. |
| @jmx:managed-attribute | 속성의 설명을 정의하기 위해서 사용된다. 메소드 레벨에서 정의되어야 한다. getter 설명이 setter보다 우선순위가 높다. |
| @jmx:mlet-entry | mlet파일을 생성하기 위해서 사용한다. 모든 클래스가 이 태그를 가질수 있으며, 하나의 통합된 파일을 출력한다. 클래스 레벨에서 정의되어야 한다. |
| 고친 과정 | |
|---|---|
| 고침 $Revision: 1.1 $ | $Date: 2002/04/10 14:11:51 $ |
이 예제는 FilePersister를 MLet과 함께 사용하는 방법에 대해서 다룬다. 또한 MLet을 사용하는 방법에 대한 훌륭한 개요가 될 것이다. 우리가 하려고 하는 것은 FilePersister를 상속한 두개의 MBean을 만들어, 이를 가지고 객체를 읽고 저장할 수 있게 하는 것이다. 다른 하나는 FilePersisterMBean에게 저장을 요구하는 standard MBean이다. 이 예제에서 재미있는 점은 두개의 다른 jar파일로 투어 두개의 다른 MLet을 사용해서 mbean을 읽어들인다는 것이다. 주의해야 할 중요한 것은 저장할 모든 객체는 Serializable 인터페이스를 구현 하여야 한다는 것이다. 이제 시작해 보자.
FilePersister를 상속한 MBean과 상위(부모) 클래스에 어떤 호출을 전달해야 하는 MBean을 작성하려고 한다.
예 5.8. MBeanOne 구현
public class MBeanOne extends FilePersister implements Serializable
{
public MBeanOne(String location, String name) throws MBeanException
{
super(location, name);
}
public void store(Object mbean)throws MBeanException, InstanceNotFoundException
{
store(mbean);
}
public Object load()throws MBeanException, RuntimeOperationsException, InstanceNotFoundException
{
return load();
}
}
이제 MBeanOne 저장하는 요청을 하는 mbean을 작성할 것이다.
예 5.9. MBeanTwo 구현
public class MBeanTwo implements MBeanTwoMBean, Serializable
{
// constructor... see example
//we are now going to invoke MBeanOne through the MBeanServer
public void storeIt(MBeanServer server, ObjectName name)
{
server.invoke(name, "store", new Object[]{this}, new String[]{"java.lang.Object"});
}
public Object loadIt(MBeanServer server, ObjectName name)
{
Object me = null;
try
{
me = (MBeanTwo)server.invoke(name, "load", new Object[0], new String[0]);
}
catch (Exception ex)
{
ex.printStackTrace();
}
return me;
}
"main" 클래스는 두개의 MLet을 생성하고 등록하게 될 것이다. 각각의 MLet은 독립적인 jar파일로 부터 자신의 MBean을 읽어들이게 될 것이다.
예 5.10. The FilePersisterAgent
// create the mbeanServer
// build the obejctNames for the MLets
// register the MLets
MLet mlet1 = new MLet();
m_server.registerMBean(mlet1, mName1);
mlet1.addURL(jarOneUrl);
MLet mlet2 = new MLet();
m_server.registerMBean(mlet2, mName2);
mlet2.addURL(jarTwoUrl);
// we now have access to the MBeans, so instantiate them
m_server.createMBean(mbeanClassName, mbeanObjectName, mLetObjectName, params, signature);
// as above but the other mbean is now registered using the othe MLet object name as the third parameter
m_server.createMBean(.....);
// now invoke the storage of one MBean by the other
m_server.invoke(mbeanName2, "storeIt", new Object[] {m_server, mbeanName1},
new String[]{"javax.management.MBeanServer", "javax.management.ObjectName"});
// now load it
Object a = m_server.invoke(mbeanName2, "loadIt", new Object[] {m_server, mbeanName1},
new String[]{"javax.management.MBeanServer", "javax.management.ObjectName"});
// and finally a test to see that the objects are equal
if(a.getClass().getName() == mbeanClass2) System.out.println("Objects are equal and the same");
파일이 있고, 컴파일하고 mbean을 포함한 jar 파일을 만들어야 한다. command prompt를 시작하고 examples/classes 디렉토리로 이동하여 다음과 같은 명령을 입력한다 :
예 5.11.
jar cvf one.jar examples/tools/persister/MBeanOne.class // and then jar cvf two.jar examples/tools/persister/MBeanTwo.class examples/tools/persister/MBeanTwoMBean.class
이제 MBeanOne.class 파일과 MBeanTwo.class 파일을 지운다(클래스 패스에 있어서는 안된다). 이제, 4가지 어플리케이션 파라미터를 추가한다.
이제 에이전트를 시작할 준비가 되었다.