4

I have a object named EXOB which has multiple routines. EXOB is a Spring bean and I want to execute the routine named routine1 which takes a String parameter. I have written this code till now:

import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;

JMXServiceURL url = new JMXServiceURL("URL");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
ObjectName mbeanName = new ObjectName("com.example.package:service=EXOB");
Object  Params[] = {"str"};
String  Sig[] = {String.class.getName()};
mbsc.invoke(mbeanName,"routine1",Params,Sig);
jmxc.close();

I am using intellij and it shows error at invoke() method as cannot resolve symbol routine1. Is the way in which I have written the code above wrong? How can I access a JMX Mbean and invoke a function inside it?

I also wanted to know if the above approach to invoke a function inside a MBean is appropriate or if there is some better way to do it?

UPDATE: When I execute the above code nothing happens and the execution seems to get hanged while I am running it

Also when I add following to the above code:

Set<ObjectName> names = new TreeSet<ObjectName>(mbsc.queryNames(null, null));
for (ObjectName name : names) {
  System.out.println("\tObjectName = " + name);
}

I get this as an output

ObjectName = com.example.package:service=EXOB

This is the object name that I am passing in my code above when I getting mbeanName. This EXOB has the routine called routine1 which I want to invoke

Jason Donnald
  • 2,256
  • 9
  • 36
  • 49

1 Answers1

1

javax.management package API defines two possible ways to access MBeans. The first one is like you do. The second one, if you have a Java interface that corresponds to the management interface for the MBean, is via MBean proxy:

EXOBMBean exob = JMX.newMBeanProxy(mbs, name, EXOBMBean.class);
exob.routine1();

The docs says: Using an MBean proxy is just a convenience. The second example ends up calling the same MBeanServer operations as the first one.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275