问题
I have simple class about JMX: Interface HelloMBean:
public interface HelloMBean {
public void sayHello();
public String getName();
public void setName(String name);
public String conCat(String s1, String s2);
}
Class Hello implements from interface HelloMBean:
import javax.management.AttributeChangeNotification;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
public class Hello extends NotificationBroadcasterSupport implements HelloMBean {
private String m_name;
@Override
public void sayHello() {
System.out.print("I say hello");
}
@Override
public String getName() {
return null;
}
@Override
public void setName(String name) {
Notification n = new AttributeChangeNotification(
this, 0,
System.currentTimeMillis(),
"My name is changed",
"setName", "String", m_name, name);
sendNotification(n);
m_name = name;
}
@Override
public String conCat(String s1, String s2) {
return null;
}
}
Then, I create a class client:
import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
public class HelloClient {
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:7271/Hello");
JMXConnector cn = JMXConnectorFactory.connect(url);
MBeanServerConnection msc = cn.getMBeanServerConnection();
ObjectName helloObjName = new ObjectName("com:service=Hello1");
HelloMBean hello = JMX.newMBeanProxy(msc, helloObjName, HelloMBean.class);
hello.sayHello();
System.out.println(hello.conCat("I am", " a hello mbean"));
Thread.sleep(Long.MAX_VALUE);
}
}
And class Server:
import java.lang.management.ManagementFactory;
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 HelloMain {
public static void main(String[] args) throws Exception {
MBeanServer ms = ManagementFactory.getPlatformMBeanServer();
ObjectName helloMBName = new ObjectName("com:service=Hello1");
Hello hello = new Hello();
ms.registerMBean(hello, helloMBName);
JMXServiceURL url = new JMXServiceURL(
"service:jmx:rmi:///jndi/rmi://localhost:7271/Hello");
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, ms);
cs.start();//
Thread.sleep(Long.MAX_VALUE);
}
}
Affter using rmiregistry 7271. I have create simple client and server using JMX. But I can't integrate JMX with Spring (JMX handled with Spring). I have try but unsuccessful. Have any solution for it??
回答1:
It's much easier to just let Spring manage JMX for you.
来源:https://stackoverflow.com/questions/17010651/how-to-integrate-jmx-with-spring