Java Method Call invokeRemoteMBeanOperation(String remoteURL, String jmxName, Class klass, Function function)

Here you can find the source of invokeRemoteMBeanOperation(String remoteURL, String jmxName, Class klass, Function function)

Description

invoke Remote M Bean Operation

License

Open Source License

Declaration

public static <T, R> R invokeRemoteMBeanOperation(String remoteURL,
            String jmxName, Class<T> klass, Function<T, R> function)
            throws MalformedObjectNameException, IOException 

Method Source Code

//package com.java2s;
/*//from   w  ww. ja v  a  2  s  .co m
 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

import java.io.IOException;
import java.lang.management.ManagementFactory;

import java.util.function.Function;

import javax.management.JMX;

import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;

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

import javax.management.remote.JMXServiceURL;

public class Main {
    public static <T, R> R invokeRemoteMBeanOperation(String remoteURL,
            String jmxName, Class<T> klass, Function<T, R> function)
            throws MalformedObjectNameException, IOException {
        JMXServiceURL jmxURL = new JMXServiceURL(remoteURL);
        try (JMXConnector jmxc = JMXConnectorFactory.connect(jmxURL, null)) {
            MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
            T remoteMBean = getMBean(jmxName, klass, mbsc);
            return function.apply(remoteMBean);
        }
    }

    private static <T> T getMBean(String jmxName, Class<T> klass,
            MBeanServerConnection mbsc) throws MalformedObjectNameException {
        ObjectName objectName = new ObjectName(jmxName);
        if (JMX.isMXBeanInterface(klass)) {
            return JMX.newMXBeanProxy(mbsc, objectName, klass);
        } else {
            return JMX.newMBeanProxy(mbsc, objectName, klass);
        }
    }

    public static <T> T getMBean(String jmxName, Class<T> klass)
            throws MalformedObjectNameException {
        return getMBean(jmxName, klass,
                ManagementFactory.getPlatformMBeanServer());
    }
}

Related

  1. invokePublicMethod(Object o, String methodName, Class[] classes, Object[] objects)
  2. invokeQuietly(MethodHandle handle, Object instance)
  3. invokeQuietly(Object target, Method method, Object[] params)
  4. invokeReadResolveOn(Object object, Class clz)
  5. invokeReal(Object real, Method m, Object[] args)
  6. invokeRestrictedMethod(Object obj, Class theClass, String methodName)
  7. invokeServiceClass(JsonReader jsonReader, Object service, Method operation, Class[] paramClasses, int paramCount)
  8. invokeSet(Object o, String fieldName, Object value)
  9. invokeSetFieldValue(Object bean, Field field, Object value)