EK.
EK.

Reputation: 2996

Jmx connection client

I want to create a JMX client, which can work with my server. I can connect to my server with jconsole, but how can I do it with java code.

This is my server code:

            MBeanServer mbeanServer = ManagementFactory
                .getPlatformMBeanServer();

        //giving name for service
        ObjectName objectName = new ObjectName("bean:name=logService");
        RequiredModelMBean mbean = new RequiredModelMBean();

        mbean.setManagedResource(logService, "objectReference");

        Descriptor logStringDescriptor = new DescriptorSupport(
                new String[] { "name=logString",
                        "descriptorType=attribute",
                        "getMethod=getLogString", "setMethod=setLogString" });
        
        ModelMBeanAttributeInfo logStringAttribute = new ModelMBeanAttributeInfo(
                "logString", "java.lang.String", "String to be logged",
                true, true, false, logStringDescriptor);

        ModelMBeanOperationInfo getLogStringOperation = new ModelMBeanOperationInfo(
                "Get the log string",
                LoggerService.class.getMethod("getLogString"));
        
        ModelMBeanOperationInfo setLogStringOperation = new ModelMBeanOperationInfo(
                "Set the log string", LoggerService.class.getMethod(
                        "setLogString", String.class));

        ModelMBeanInfo mbeanInfo = new ModelMBeanInfoSupport(
                "LoggerService", "Logger Service",
                new ModelMBeanAttributeInfo[] { logStringAttribute }, null,
                new ModelMBeanOperationInfo[] { getLogStringOperation,
                        setLogStringOperation }, null);

        mbean.setModelMBeanInfo(mbeanInfo);
        mbeanServer.registerMBean(mbean, objectName);

Upvotes: 1

Views: 408

Answers (3)

Mykhaylo Adamovych
Mykhaylo Adamovych

Reputation: 20956

public static <T> T createJmxClient(Class<T> clazz, String objectName, String serviceUrl) {
    return createJmxClient(clazz, objectName, serviceUrl, null, null);
}

public static <T> T createJmxClient(Class<T> clazz, String objectName, String serviceUrl, final String user, final String pass) {
    try {
        JMXServiceURL jmxServiceUrl = new JMXServiceURL(serviceUrl);
        Map<String, ?> env = user == null ? null : new HashMap<String, Object>() {{
            put(JMXConnector.CREDENTIALS, new String[] {user, pass});
        }};
        JMXConnector jmxc = JMXConnectorFactory.connect(jmxServiceUrl, env);
        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
        ObjectName mbeanName = new ObjectName(objectName);
        return JMX.newMBeanProxy(mbsc, mbeanName, clazz, true);
    } catch (IOException | MalformedObjectNameException e) {
        throw new RuntimeException("Can not create client for remote JMX " + serviceUrl, e);
    }
}

Upvotes: 0

EK.
EK.

Reputation: 2996

It is very simple :)

            MBeanServer mbeanServer = ManagementFactory
                .getPlatformMBeanServer();


        //giving name for service
        ObjectName objectName = new ObjectName("bean:name=logService");

        String str = (String) mbeanServer.getAttribute (objectName, "logString");
        System.out.println(str);

Upvotes: 1

Adam Sznajder
Adam Sznajder

Reputation: 9206

You have to create a JMX agent which will be a wrapper of your MBeans server and allow connecting to your application by external processes with the usage of special adapter. This adapter will allow your clients to connect to MBean server using certain protocol (SNMP, HTTP etc.) or technology (RMI). You can read about adapters here: http://marxsoftware.blogspot.com/2008/08/remote-jmx-connectors-and-adapters.html

Upvotes: 0

Related Questions