How can an EJB call an MBean?
Calling an MBean from an EJB in the same JVM is just a matter of
knowing the ObjectName of the MBean you want to call
locating the local MBeansServer
get/set attributes or invoke operation on that MBean
Knowing the ObjectName of the target MBean is application specific.
For locating the MBeanServer look at the answer to this FAQ:
HowCanIGetAReferenceToTheMBeanServer.
The traditional way to get/set attributes and invoke operations is
to use the detyped interface of
in particular the methods:
interface MBeanServer {
...
Object getAttribute(ObjectName name, String attribute) throws ...
void setAttribute(ObjectName name, Attribute attribute) throws ...
Object invoke(ObjectName name,
String operationName,
Object[] params,
String[] signature)
throws ...
Starting with JMX v1.2 (JBoss v3.2.4+) you have an alternative
way of making type-safe invocations to your target MBean,
by obtaining a JDK1.3+ dynamic proxy. This is a lot more
conventient and it doesn't have an impact on performance:
import javax.management.MBeanServer;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
...
// target MBean
ObjectName objectName = new ObjectName("mydomain:name=mymbean");
// find the local MBeanServer
MBeanServer server = ...
// Get a type-safe dynamic proxy
MyClassMBean mbean =
(MyClassMBean)MBeanServerInvocationHandler.newProxyInstance(
server,
objectName,
MyClassMBean.class,
false);
// Use the proxy
String state = mbean.getStateString();
mbean.reset();
Before JMX v1.2 (starting from JBoss v.3.0.0+) you may use the JBoss-specific
class MBeanProxy to do the same:
import org.jboss.mx.util.MBeanProxy; ... MyClassMBean mbean = (MyClassMBean)MBeanProxy.get( MyClassMBean.class, objectName, server);
Then you'll probably cache the proxy reference for future use.
Referenced by:
Comments