Example usage for javax.management MBeanServerConnection invoke

List of usage examples for javax.management MBeanServerConnection invoke

Introduction

In this page you can find the example usage for javax.management MBeanServerConnection invoke.

Prototype

public Object invoke(ObjectName name, String operationName, Object params[], String signature[])
        throws InstanceNotFoundException, MBeanException, ReflectionException, IOException;

Source Link

Document

Invokes an operation on an MBean.

Because of the need for a signature to differentiate possibly-overloaded operations, it is much simpler to invoke operations through an JMX#newMBeanProxy(MBeanServerConnection,ObjectName,Class) MBean proxy where possible.

Usage

From source file:com.flipkart.poseidon.jmx.PoseidonJMXInvoker.java

public static void main(String[] args) {
    if (args.length < 3) {
        System.err.println(PoseidonJMXInvoker.class.getSimpleName() + " <host> <port> <operation>");
        System.exit(-1);//  w w w. ja v  a  2  s.  c  om
    }

    final String CONNECT_STRING = args[0] + ":" + args[1];
    final String OPERATION = args[2];
    try {
        System.out.println("Running " + OPERATION + " over JMX on " + CONNECT_STRING);

        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + CONNECT_STRING + "/jmxrmi");
        MBeanServerConnection connection = JMXConnectorFactory.connect(url).getMBeanServerConnection();
        connection.invoke(ObjectName.getInstance(MBEAN_NAME), OPERATION, null, null);
    } catch (Exception e) {
        if (!(ExceptionUtils.getRootCause(e) instanceof EOFException && "destroy".equals(OPERATION))) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
    System.out.println(OPERATION + " successful over JMX on " + CONNECT_STRING);
}

From source file:com.newlandframework.rpc.jmx.ThreadPoolMonitorProvider.java

public static void monitor(ThreadPoolStatus status) throws IOException, MalformedObjectNameException,
        ReflectionException, MBeanException, InstanceNotFoundException {
    MBeanServerConnectionFactoryBean mBeanServerConnectionFactoryBean = new MBeanServerConnectionFactoryBean();
    mBeanServerConnectionFactoryBean.setServiceUrl(url);
    mBeanServerConnectionFactoryBean.afterPropertiesSet();
    MBeanServerConnection connection = mBeanServerConnectionFactoryBean.getObject();
    ObjectName objectName = new ObjectName(
            "com.newlandframework.rpc.jmx:name=threadPoolStatus,type=ThreadPoolStatus");

    connection.invoke(objectName, jmxPoolSizeMethod, new Object[] { status.getPoolSize() },
            new String[] { int.class.getName() });
    connection.invoke(objectName, jmxActiveCountMethod, new Object[] { status.getActiveCount() },
            new String[] { int.class.getName() });
    connection.invoke(objectName, jmxCorePoolSizeMethod, new Object[] { status.getCorePoolSize() },
            new String[] { int.class.getName() });
    connection.invoke(objectName, jmxMaximumPoolSizeMethod, new Object[] { status.getMaximumPoolSize() },
            new String[] { int.class.getName() });
    connection.invoke(objectName, jmxLargestPoolSizeMethod, new Object[] { status.getLargestPoolSize() },
            new String[] { int.class.getName() });
    connection.invoke(objectName, jmxTaskCountMethod, new Object[] { status.getTaskCount() },
            new String[] { long.class.getName() });
    connection.invoke(objectName, jmxCompletedTaskCountMethod, new Object[] { status.getCompletedTaskCount() },
            new String[] { long.class.getName() });
}

From source file:org.jboss.test.cluster.testutil.SessionTestUtil.java

public static Object getSessionVersion(MBeanServerConnection adaptor, String sessionFqn) throws Exception {
    return adaptor.invoke(CacheHelper.OBJECT_NAME, "getSessionVersion", new Object[] { sessionFqn },
            STRING_ONLY_TYPES);//  w w  w.  j  a  v  a 2 s .  c om
}

From source file:org.jboss.test.cluster.testutil.SessionTestUtil.java

public static Object getBuddySessionVersion(MBeanServerConnection adaptor, String sessionFqn) throws Exception {

    return adaptor.invoke(CacheHelper.OBJECT_NAME, "getBuddySessionVersion", new Object[] { sessionFqn },
            STRING_ONLY_TYPES);/*  w w  w.j  a v  a  2  s . com*/
}

From source file:org.jboss.test.cluster.testutil.SessionTestUtil.java

@SuppressWarnings("unchecked")
public static Set<String> getSessionIds(MBeanServerConnection adaptor, String warFqn) throws Exception {
    return (Set<String>) adaptor.invoke(CacheHelper.OBJECT_NAME, "getSessionIds", new Object[] { warFqn },
            STRING_ONLY_TYPES);/*w w  w.  j  a  va  2 s.co m*/
}

From source file:com.redhat.poc.jdg.bankofchina.function.TestCase411RemoteMultiThreadsCustomMarshal.java

public static void registerProtofile(String jmxHost, int jmxPort, String cacheContainerName) throws Exception {
    JMXConnector jmxConnector = JMXConnectorFactory
            .connect(new JMXServiceURL("service:jmx:remoting-jmx://" + jmxHost + ":" + jmxPort));
    MBeanServerConnection jmxConnection = jmxConnector.getMBeanServerConnection();

    ObjectName protobufMetadataManagerObjName = new ObjectName("jboss.infinispan:type=RemoteQuery,name="
            + ObjectName.quote(cacheContainerName) + ",component=ProtobufMetadataManager");

    // initialize client-side serialization context via JMX
    byte[] descriptor = readClasspathResource("/model/userbaseinfo.protobin");
    jmxConnection.invoke(protobufMetadataManagerObjName, "registerProtofile", new Object[] { descriptor },
            new String[] { byte[].class.getName() });
    jmxConnector.close();//from w w w.j a  v  a 2 s.co m
}

From source file:org.jboss.test.cluster.testutil.SessionTestUtil.java

public static void setCacheConfigName(MBeanServerConnection adaptor, String cacheConfigName,
        boolean usePojoCache) throws Exception {
    adaptor.invoke(CacheHelper.OBJECT_NAME, "setCacheConfigName",
            new Object[] { cacheConfigName, Boolean.valueOf(usePojoCache) },
            new String[] { String.class.getName(), boolean.class.getName() });
}

From source file:org.jboss.test.cluster.testutil.SessionTestUtil.java

@SuppressWarnings("unchecked")
public static Set<String> getSessionIds(MBeanServerConnection adaptor, String warFqn, boolean includeBuddies)
        throws Exception {
    return (Set<String>) adaptor.invoke(CacheHelper.OBJECT_NAME, "getSessionIds",
            new Object[] { warFqn, Boolean.valueOf(includeBuddies) }, STRING_BOOLEAN_TYPES);
}

From source file:cc.kune.kunecli.JMXUtils.java

public static Object doOperation(final String objectName, final String operation) {

    final List<VirtualMachineDescriptor> vms = VirtualMachine.list();

    try {// w  w w  . j ava2 s .co  m
        for (final VirtualMachineDescriptor vmd : vms) {
            final String id = vmd.id();
            LOG.debug("VM id: " + id);
            final JMXServiceURL url = getURLForPid(id.trim());

            final JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
            final MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

            final ObjectName mbeanName = new ObjectName(objectName);

            MBeanInfo beanInfo;
            try {
                beanInfo = mbsc.getMBeanInfo(mbeanName);
                for (final MBeanOperationInfo currentOp : beanInfo.getOperations()) {
                    if (currentOp.getName().equals(operation)) {
                        LOG.debug("Doing operation '" + operation + "' over mbean: '" + objectName
                                + "' with id: '" + id + "'.");
                        final Object invoke = mbsc.invoke(mbeanName, currentOp.getName(), new Object[] {},
                                new String[] {});
                        return invoke;
                    }
                }
            } catch (final InstanceNotFoundException e) {
                // Ok, operation not found in this VM or domain
            }
        }
        throw new RuntimeException("JMX operation not found");
    } catch (final Exception e) {
        LOG.error("Error in jmx connection", e);
    }
    throw new RuntimeException("JMX operation failed");
}

From source file:org.hyperic.hq.plugin.jboss.JBossUtil.java

public static Object invoke(Metric metric, String method, Object[] args, String[] sig)
        throws MetricUnreachableException, MetricNotFoundException, PluginException {
    try {//  w  w w.  j av  a2 s.co  m
        MBeanServerConnection mServer = getMBeanServerConnection(metric);
        ObjectName obj = new ObjectName(metric.getObjectName());
        MBeanInfo info = mServer.getMBeanInfo(obj);

        if (sig.length == 0) {
            MBeanUtil.OperationParams params = MBeanUtil.getOperationParams(info, method, args);
            if (params.isAttribute) {
                return setAttribute(mServer, obj, method, params.arguments[0]);
            }
            sig = params.signature;
            args = params.arguments;
        }

        return mServer.invoke(obj, method, args, sig);
    } catch (NamingException e) {
        throw unreachable(metric, e);
    } catch (RemoteException e) {
        throw unreachable(metric, e);
    } catch (MalformedObjectNameException e) {
        throw invalid(metric, e);
    } catch (InstanceNotFoundException e) {
        throw notfound(metric, e);
    } catch (ReflectionException e) {
        throw error(metric, e, method);
    } catch (IntrospectionException e) {
        throw error(metric, e);
    } catch (MBeanException e) {
        throw error(metric, e, method);
    } catch (IOException e) {
        throw error(metric, e);
    }
}