Tracing JMX
https://blogs.oracle.com/jmxetc/entry/tracing_jmx_what_s_going な感じらしい。ちょっとデモっぽいのやってみた
MBean.java
package sample;
public interface MBean {
public static final String SERVICE = "service:jmx:rmi:///jndi/rmi://localhost/sample";
public static final String OBJECT_NAME = "sample.jmx:Type=JmxServer";
public void test();
}
MBeanImpl.java
package sample;
import javax.management.StandardMBean;
public class MBeanImpl extends StandardMBean implements MBean {
public MBeanImpl() {
super(MBean.class, false);
}
public void test() {
System.out.println("hoge fuga foobar");
}
}
Server.java
package sample;
import java.rmi.registry.LocateRegistry;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.remote.JMXServiceURL;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import java.lang.management.ManagementFactory;
public class Server {
public static void main(String[] args) throws Exception {
LocateRegistry.createRegistry(1099);
MBeanServer mbean = ManagementFactory.getPlatformMBeanServer();
mbean.registerMBean(new MBeanImpl(), new ObjectName(MBean.OBJECT_NAME));
JMXServiceURL url = new JMXServiceURL(MBean.SERVICE);
JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbean);
server.start();
}
}
な感じで作って、logging.propertiesを作る
handlers= java.util.logging.ConsoleHandler
.level=INFO
java.util.logging.ConsoleHandler.level = FINEST
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
javax.management.level=FINEST
javax.management.remote.level=FINER
あとは普通にServerクラスを実行して、jconsoleとかで接続してぐだぐだしてみると今回はConsoleHandlerなのでサーバー実行コンソール上に出る。FileHandlerとか使えば(ry
Client.java
jconsoleじゃなくて、クライアント作って試しても良い
package sample;
import javax.management.remote.JMXServiceURL;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
public class Client {
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL(MBean.SERVICE);
JMXConnector connector = JMXConnectorFactory.connect(url);
MBeanServerConnection conn = connector.getMBeanServerConnection();
ObjectName oname = new ObjectName(MBean.OBJECT_NAME);
conn.invoke(oname, "test", null, null);
}
}