JMX의 간단한 회고
JMX 스키마:
package com.blackbeans.example.mbeans;
/**
* Mbean
* @author blackbeans
*
*/
public interface GunMBean {
public void aimAndShoot(String[] targetName);
}
package com.blackbeans.example.mbeans;
import javax.management.AttributeChangeNotification;
import javax.management.NotificationBroadcasterSupport;
/**
* Mbean, JMX Resources
*
* @author blackbeans
*
*/
public class Gun extends NotificationBroadcasterSupport implements GunMBean {
private String gunModel;
public Gun(String gunModel) {
this.gunModel = gunModel;
}
@Override
public void aimAndShoot(String[] targetNames){
for(String targetName : targetNames ){
System.out.println(gunModel + "aim at "+ targetName +" !");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(gunModel + " fire !");
}
AttributeChangeNotification notification = new AttributeChangeNotification(this, 0, 0L, "Gun shot!", null, null, null, null);
sendNotification(notification);
}
}
Mbean의 값 변화를 감시하기 위해 JMX의 에이전트 Level은 자바 이벤트를 기반으로 하는 Notification 메커니즘을 제공하여 다른 JMX의 현재 자원이 변경되었음을 알립니다.JMX의 Agent Level은 모니터 서비스를 제공하고 모든 Listener에게 알립니다.
package com.blackbeans.example.mbeans;
import javax.management.Notification;
import javax.management.NotificationListener;
public class GunFiredNotificationListener implements NotificationListener {
@Override
public void handleNotification(Notification notification, Object handback) {
System.out.println("notifyInfo:\t"+notification+"
handback:\t"+handback);
}
}
MBServer
package com.blackbeans.example.mbeans;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.MalformedURLException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
public class MyMBeanServer {
private MBeanServer mbServer;
public MyMBeanServer()
{
this.mbServer = ManagementFactory.getPlatformMBeanServer();
try {
Registry reg = LocateRegistry.createRegistry(9999);
for(String bind : reg.list()){
System.out.println(bind);
}
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://192.168.1.101:9999/MBServer");
JMXConnectorServer connector = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbServer);
connector.start();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void registerMBean(String objectName,Object obj)
{
ObjectName objName = null;
try {
objName = new ObjectName(objectName);
this.mbServer.registerMBean(obj, objName);
this.mbServer.addNotificationListener(objName, new GunFiredNotificationListener(), null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
Gun gun = new Gun("AK-47");
MyMBeanServer myServer = new MyMBeanServer();
myServer.registerMBean(gun.getClass().getPackage().getName() + ":name="+gun.getClass().getSimpleName(), gun);
Thread.sleep(100000);
}
}
MBeanClient
package com.blackbeans.example.mbeans;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
public class MyMBeanClient{
private MBeanServerConnection connection;
public MyMBeanClient(String jmxUrl)
{
JMXServiceURL url = null;
try {
url = new JMXServiceURL(jmxUrl);
JMXConnector connector = JMXConnectorFactory.connect(url);
connector.connect();
this.connection = connector.getMBeanServerConnection();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void invokeMethod(ObjectName objName,String opName ,Object[] paras,String[] signature)
{
try {
this.connection.invoke(objName, opName, paras, signature);
} catch (InstanceNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MBeanException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ReflectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
String[] gunTypes = new String[]{"AK-47","M16"};
MyMBeanClient client = new MyMBeanClient("service:jmx:rmi:///jndi/rmi://192.168.1.101:9999/MBServer");
try {
ObjectName objName = new ObjectName("com.blackbeans.example.mbeans:name=Gun");
Object[] parameters = new Object[]{gunTypes};
String[] signatures = new String[]{"[Ljava.lang.String;"};
client.invokeMethod(objName, "aimAndShoot", parameters, signatures);
} catch (MalformedObjectNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
나의 예에서 사용한 JMX의 RMI 호출은 물론 로컬 방식을 사용하거나 Sun의 HtmlJmxAdapter를 사용하여 JMX 호출을 완성하거나 클라이언트가 직접 jconsol을 사용하지 않고 연결하여 호출하면 된다.
reference:
ht tp://java.sun.com/developer/technicalArticles/J2SE/jmx.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Exception in thread main java.lang. NoClassDefFoundError 오류 해결 방법즉,/home/hadoop/jarfile) 시스템은 Hello World 패키지 아래의class라는 클래스 파일을 실행하고 있다고 오인하여 시스템의 CLASSPATH 아래 (일반적으로 현재 디렉터리를 포함) Hell...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.