P
P
parkito2016-07-11 13:22:14
Java
parkito, 2016-07-11 13:22:14

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;
    }
}

I register it
Test test = new Test();
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        ObjectName objectName = new ObjectName("Admin:type=Test");
        mBeanServer.registerMBean(test, objectName);

As a result, exceptions are thrown
Exception in thread "main" javax.management.NotCompliantMBeanException: MBean class test.Test does not implement DynamicMBean

I got on the Internet to see what was wrong with me. Found an example.
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();
        }
    }

What is most interesting - this example works for me. Conclusion: somewhere I mow. Where can this happen?
UPD:
Correct version
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

1 answer(s)
S
sirs, 2016-07-11
@parkito

try naming your interface TestIMBean

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question