Document Version 1.0
When you are familiar with JBoss, you should have known JMX-console. When you want to monitor you application deployed on JBoss, you might go with following step:
- wrapp the to be monitored property in a Mbean,
- register the MBean with JBoss' JMX sesrver
- check / manage the properties through JMX-Console
In such case, your will have to start your own JMX-server.
I will show how to use your own JMX-server, to monitor a standalone application which monitors the application's local time.
1. The MBean
The MBean interface exposes which methods are monitored by JMX.
package mbean;
/**
*
* @author swang
*
*/
public interface MySampleMBean {
String getServerTime();
}
/**
*
* @author swang
*
*/
public interface MySampleMBean {
String getServerTime();
}
This is the MBean implementation. The property "serverTime" will be updated every 5 seconds. We expect the upated serverTime will be shown to a JMX client when it connects to JXX-Server.
package mbean;
import java.util.Date;
/**
*
* @author swang
*
*/
public class MySample implements MySampleMBean, Runnable{
private static MySample instance = new MySample();
private String serverTime;
private MySample() {
}
public static MySample getInstance() {
return instance;
}
@Override
public String getServerTime() {
return this.serverTime;
}
public void setServerTime(String serverTime) {
this.serverTime = serverTime;
}
@Override
public void run() {
while(true) {
//Updating serverTime every 5 seconds
setServerTime(new Date().toLocaleString());
try {
Thread.currentThread().sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.util.Date;
/**
*
* @author swang
*
*/
public class MySample implements MySampleMBean, Runnable{
private static MySample instance = new MySample();
private String serverTime;
private MySample() {
}
public static MySample getInstance() {
return instance;
}
@Override
public String getServerTime() {
return this.serverTime;
}
public void setServerTime(String serverTime) {
this.serverTime = serverTime;
}
@Override
public void run() {
while(true) {
//Updating serverTime every 5 seconds
setServerTime(new Date().toLocaleString());
try {
Thread.currentThread().sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2. A JMX Agent
This is the main class of the program, we call it a JMX agent. It starts a JMX-Server, starts the MBean, and regist the MBean with the JMX-Server.
package mbean;
import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;
import java.util.HashMap;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
/**
*
* @author swang
*
*/
public class MyJMXAgent {
private static MBeanServer mbs = null;
public void init() throws Exception {
mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName objName = new ObjectName("TestJMXAgent:name=MySampleMBean");
mbs.registerMBean(MySample.getInstance(), objName);
String hostname = "localhost";
int port = 1717;
LocateRegistry.createRegistry(port);
System.setProperty("java.rmi.server.randomIDs", "true");
System.setProperty("java.rmi.server.hostname", hostname);
HashMap<String, Object> env = new HashMap<String, Object>(3);
env.put("jmx.remote.x.password.file", "jmxremote.password");
env.put("jmx.remote.x.access.file", "jmxremote.access");
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://" + hostname
+ ":" + port + "/jndi/rmi://" + hostname + ":" + port
+ "/jmxrmi");
JMXConnectorServer cs = JMXConnectorServerFactory
.newJMXConnectorServer(url, env, mbs);
cs.start();
System.out.println("Starting JMXConnectorServer on: " + url);
}
public static void main(String[] args) throws Exception {
System.out.println("jmx agent starting");
// Start updating MBean
new Thread(MySample.getInstance()).start();
MyJMXAgent agent = new MyJMXAgent();
agent.init();
}
}
import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;
import java.util.HashMap;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
/**
*
* @author swang
*
*/
public class MyJMXAgent {
private static MBeanServer mbs = null;
public void init() throws Exception {
mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName objName = new ObjectName("TestJMXAgent:name=MySampleMBean");
mbs.registerMBean(MySample.getInstance(), objName);
String hostname = "localhost";
int port = 1717;
LocateRegistry.createRegistry(port);
System.setProperty("java.rmi.server.randomIDs", "true");
System.setProperty("java.rmi.server.hostname", hostname);
HashMap<String, Object> env = new HashMap<String, Object>(3);
env.put("jmx.remote.x.password.file", "jmxremote.password");
env.put("jmx.remote.x.access.file", "jmxremote.access");
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://" + hostname
+ ":" + port + "/jndi/rmi://" + hostname + ":" + port
+ "/jmxrmi");
JMXConnectorServer cs = JMXConnectorServerFactory
.newJMXConnectorServer(url, env, mbs);
cs.start();
System.out.println("Starting JMXConnectorServer on: " + url);
}
public static void main(String[] args) throws Exception {
System.out.println("jmx agent starting");
// Start updating MBean
new Thread(MySample.getInstance()).start();
MyJMXAgent agent = new MyJMXAgent();
agent.init();
}
}
3. Configuration JMX access:
Create a "jmxremote.access" file:
monitor readonly
Create a "jmxremote.password" file:
monitor password
4. Run MyJMXAgent as Java application:
jmx agent starting
host name: localhost
Starting JMXConnectorServer on: service:jmx:rmi://localhost:1717/jndi/rmi://localhost:1717/jmxrmi
host name: localhost
Starting JMXConnectorServer on: service:jmx:rmi://localhost:1717/jndi/rmi://localhost:1717/jmxrmi
A JMX-Server is now running on localhost:1717, and you could use a JMX-client to connect to it.
5. Connect to JMX-Server with jconsole
Start jconsole an connect to MyMXAgent:
Check monitored property "serverTime":
Click "Refresh" button to get the updated "serverTime" :
No comments:
Post a Comment