Tuesday, March 16, 2010

JMX Sample

public class MessageHolder implements MessageHolderMBean{
public static void main(String[] args) {

}
private String message;
@Override
public void setMessage(String message) {
this.message = message;
}

@Override
public String getMessage() {
return this.message; //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void sayHello() {
System.out.println("Messsage:"+message);
}
}


public class JmxAgent {

private MBeanServer mbs = null;

public JmxAgent() {

// Get the platform MBeanServer
mbs = ManagementFactory.getPlatformMBeanServer();

// Unique identification of MBeans
MessageHolder helloBean = new MessageHolder();
ObjectName helloName = null;

try {
// Uniquely identify the MBeans and register them with the platform MBeanServer
helloName = new ObjectName("SimpleAgent:name=ManageYourProgram");
mbs.registerMBean(helloBean, helloName);
} catch(Exception e) {
e.printStackTrace();
}
}

// Utility method: so that the application continues to run
private static void waitForEnterPressed() {
try {
System.out.println("Press to continue...");
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String argv[]) {
JmxAgent agent = new JmxAgent();
System.out.println("SimpleAgent is running...");
JmxAgent.waitForEnterPressed();
}
}


public interface MessageHolderMBean
{

public void setMessage(String message);

public String getMessage();

public void sayHello();
}

No comments:

Post a Comment