If a management client only wants to invoke a single operation or access a single attribute on an MBean, it typically accesses the MBeanServer directly:
import javax.management.MBeanServerConnection; import javax.management.ObjectName; ... MBeanServerConnection mbsc; ObjectName name; ... mbsc.setAttribute(name, new Attribute("Test", "test value")); |
If the client does more interaction, the code will become difficult to understand. If the interface of the MBean is available to the class loader of the client, it could create an MBean proxy, which significantly simplifies the interaction with the MBean.
// the management interface of the MBean interface MyMBean { public String anOperation(String, Integer); public String setTest(String test); ... }
import javax.management.MBeanServerConnection; import javax.management.ObjectName; import javax.management.MBeanServerInvocationHandler; ... MBeanServerConnection mbsc; ObjectName name; ... // create the MBean proxy MyMBean myMBean= (MyMBean) MBeanServerInvocationHandler.newProxyInstance(mbsc, name, MyMBean.class, false); // invoke operations, access attributes mbeanProxy.setTest(aValue); mbeanProxy.anOperation(aValue, bValue); |
For more information, see the API documentation of javax.management.MBeanServerInvocationHandler.