Execute a method with JMX without JConsole

10,335

You have not put the code to invoke an mbean method. Here is a sample code to help you with that:

import javax.management.*;
import javax.management.remote.*;
import com.sun.messaging.AdminConnectionFactory;
import com.sun.messaging.jms.management.server.*;


public class  InvokeOp
  { 
    public static void  main (String[]  args)
      { 
        try
          { //  Create administration connection factory
                AdminConnectionFactory  acf = new AdminConnectionFactory();

            //  Get JMX connector, supplying user name and password
                JMXConnector  jmxc = acf.createConnection("AliBaba", "sesame");

            //  Get MBean server connection
                MBeanServerConnection  mbsc = jmxc.getMBeanServerConnection();

            //  Create object name
                ObjectName  serviceConfigName = MQObjectName.createServiceConfig("jms");

            //  Invoke operation
                mbsc.invoke(serviceConfigName, ServiceOperations.PAUSE, null, null);

            //  Close JMX connector
                jmxc.close();
          }

        catch (Exception  e)
          { System.out.println( "Exception occurred: " + e.toString() );
            e.printStackTrace();
          }
      }
  }
Share:
10,335
Sanyifejű
Author by

Sanyifejű

I am here to learn

Updated on June 04, 2022

Comments

  • Sanyifejű
    Sanyifejű about 2 years

    How to execute a method with JMX without JConsole?

    I want to invoke methods through JMX from Java code. With the code below I can get the name of all methods of the MBean interface but I am not yet able to actually execute them. Something is still missing, could anybody please help me?

    private static String connectJmx() {
        StringBuilder sb = new StringBuilder();
    
        JMXServiceURL serviceUrl;
        JMXConnector jmxConnector = null;
        try {
            serviceUrl = new JMXServiceURL(URL);
            jmxConnector = JMXConnectorFactory.connect(serviceUrl, null);
            MBeanServerConnection mbeanConn = jmxConnector.getMBeanServerConnection();
    
            ObjectName objectName = ObjectNameProvider.getObjectName();
            MBeanInfo info = mbeanConn.getMBeanInfo(objectName);
    
            MBeanOperationInfo[] operations = info.getOperations();         
            for (int i = 0; i < operations.length; i++) {
             sb.append(operations[i].getName()).append("\n");
            }
    
        }
        catch (Exception e) {
            LogManager.doLog(LOG, LOGLEVEL.INFO, "exception connection jmx",  e);
    
        } finally {
            try {
                if(jmxConnector != null){
                    jmxConnector.close();
                }
            } catch (IOException e) {
            //
            }
        }       
        return sb.toString();
    }
    

    The for cycle above collects the names of the methods. I want something similar, that instead of collecting the names, actually executes them. Please do not recommend JConsole, it does not work for other reasons.