Answer the question
In order to leave comments, you need to log in
How to write an MBean agent for JMX?
Hello. Please help me solve the following problem.
Making a simple MBean
public interface TestI {
public void testSetter();
public String testGetter();
}
public class Test implements TestI {
private String message = null;
public Test() {
message = "constructor";
}
public Test(String message) {
this.message = message;
}
@Override
public void testSetter() {
message = "setter";
}
@Override
public String testGetter() {
return message;
}
}
Test test = new Test();
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("Admin:type=Test");
mBeanServer.registerMBean(test, objectName);
Exception in thread "main" javax.management.NotCompliantMBeanException: MBean class test.Test does not implement DynamicMBean
public interface HelloMBean {
public void setMessage(String message);
public String getMessage();
public void sayHello();
}
public class Hello implements HelloMBean
{
private String message = null;
public Hello() {
message = "Hello there";
}
public Hello(String message) {
this.message = message;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void sayHello() {
System.out.println(message);
}
}
private MBeanServer mbs = null;
public SimpleAgent()
{
// Получить экземпляр MBeanServer
mbs = ManagementFactory.getPlatformMBeanServer();
// Создаем наш MBean
Hello helloBean = new Hello();
ObjectName helloName = null;
try {
// И регистрируем его на платформе MBeanServer
helloName = new ObjectName("SimpleAgent:name=hellothere");
mbs.registerMBean(helloBean, helloName);
} catch(Exception e) {
e.printStackTrace();
}
}
public class Test implements TestIMBean {
private String message = null;
public Test() {
message = "constructor";
}
public Test(String message) {
this.message = message;
}
@Override
public void setMessage(String str) {
this.message=message;
}
@Override
public String getMessage() {
return this.message;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question