运行 JMXConnectorFactory.connect 时 JMX 连接不重播

JMX Connection does not replay when running JMXConnectorFactory.connect

需要您的帮助来理解为什么我无法使用 JMX 从客户端应用程序连接到服务器应用程序。 我有一个正在启动 JMX Bean 服务器的服务器应用程序和一个尝试使用 JMX 连接到此服务器的客户端。在 windows 中,这两个应用程序都在本地计算机上损坏。 Jconsole 工作正常,我可以用它连接到服务器。 但是当 运行 客户端在线时“卡住”:

  JMXConnector jmxConnector = JMXConnectorFactory.connect(url, null);

应用程序端 JMX 服务器初始化代码:

    MyClass mc = new MyClass ();
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName obj = new ObjectName("JMX:name=MyClassJmx");
    mbs.registerMBean(mc, obj);

应用程序端 JMX 服务器 JVM 参数:

-Dcom.sun.management.jmxremote.port=10090
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

我的客户端连接到应用程序代码:

  final JMXServiceURL url = new JMXServiceURL("service:jmx:jmxmp://127.0.0.1:10090");
  JMXConnector jmxConnector = JMXConnectorFactory.connect(url, null);
  jmxConnector.connect();

一个注释,使用

服务:jmx:rmi:///jndi/rmi://:10090/jmxrmi

正在工作。

Java reference

JMX 消息传递协议 (JMXMP) 连接器是通用连接器的配置,其中传输协议基于 TCP,对象包装是本机 Java 序列化。安全性比 RMI 连接器更高级。安全性基于 Java 安全套接字扩展 (JSSE)、Java 身份验证和授权服务 (JAAS) 以及简单身份验证和安全层 (SASL)。

通用连接器及其 JMXMP 配置是可选的,这意味着它们并不总是包含在 JMX Remote API 的实现中。 J2SE 平台不包括可选的通用连接器。

注意 – 如果您想使用 JMXMP 连接器,请从 http://java.sun.com/products/JavaManagement/download.html 下载 JSR 160 参考实现,并将 jmxremote_optional.jar 文件添加到您的类路径。

您是否在服务器和客户端类路径中包含 jmxremote_optional.jar 文件?

我之前没有使用 JMXMP 连接器方法的经验,我遵循了 JMX 文档并尝试通过编写以下示例来验证它是否有效。

JMX MBean interface

SystemConfigMBean.java

package jmx.learning;

public interface SystemConfigMBean {

    public void setThreadCount(int noOfThreads);
    public int getThreadCount();
    
    public void setSchemaName(String schemaName);
    public String getSchemaName();
    
    // any method starting with get and set are considered
    // as attributes getter and setter methods, so I am 
    // using do* for operation.
    public String doConfig();
}

A class to implement the JMX MBean interface

SystemConfig.java

package jmx.learning;

public class SystemConfig implements SystemConfigMBean {

    private int threadCount;
    private String schemaName;
    
    public SystemConfig(int numThreads, String schema){
        this.threadCount=numThreads;
        this.schemaName=schema;
    }
    
    @Override
    public void setThreadCount(int noOfThreads) {
        this.threadCount=noOfThreads;
    }


    @Override
    public int getThreadCount() {
        return this.threadCount;
    }


    @Override
    public void setSchemaName(String schemaName) {
        this.schemaName=schemaName;
    }


    @Override
    public String getSchemaName() {
        return this.schemaName;
    }
    
    @Override
    public String doConfig(){
        return "No of Threads="+this.threadCount+" and DB Schema Name="+this.schemaName;
    }

}

Creating and Registering the MBean in the MBean Server using JMXMP connector

SystemConfigManagement.java

package jmx.learning;

import java.io.IOException;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

public class SystemConfigManagement {
    private static final int DEFAULT_NO_THREADS = 10;
    private static final String DEFAULT_SCHEMA = "default";

    public static void main(String[] args) throws MalformedObjectNameException,
            InterruptedException, InstanceAlreadyExistsException,
            MBeanRegistrationException, NotCompliantMBeanException, IOException {
        // Instantiate the MBean server
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();

        // Create a JMXMP connector server
        JMXServiceURL url = new JMXServiceURL("jmxmp", null, 5555);
        JMXConnectorServer cs = JMXConnectorServerFactory
                .newJMXConnectorServer(url, null, mbs);
        cs.start();

        // register the MBean
        SystemConfig mBean = new SystemConfig(DEFAULT_NO_THREADS,
                DEFAULT_SCHEMA);
        ObjectName name = new ObjectName("jmx.learning:type=SystemConfig");
        mbs.registerMBean(mBean, name);
        do {
            Thread.sleep(3000);
            System.out.println("Thread Count=" + mBean.getThreadCount()
                    + " Schema Name=" + mBean.getSchemaName());
        } while (mBean.getThreadCount() != 0);

    }
}

JMXMP Connector Client

SystemConfigManagementClient.java

package jmx.learning;

import java.io.IOException;

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

public class SystemConfigManagementClient {
    public static void main(String[] args) {
        JMXConnector jmxc = null;
        try {
            // Create a JMXMP connector client
            System.out.println("\nCreate a JMXMP connector client");
            JMXServiceURL url = new JMXServiceURL(
                    "service:jmx:jmxmp://localhost:5555");
            jmxc = JMXConnectorFactory.connect(url, null);
            // get MBeanServerConnection
            MBeanServerConnection serverConn = jmxc.getMBeanServerConnection();

            ObjectName objectName = new ObjectName(
                    "jmx.learning:type=SystemConfig");

            // Executing doConfig() method
            String value = (String) serverConn.invoke(objectName, "doConfig",
                    new Object[] {}, new String[] {});
            System.out.println(value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jmxc != null) {
                try {
                    jmxc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

注意:我已经下载了 jmxremote_optional.jar 文件并保存在项目类路径中。

执行:

服务器输出:

d:\Java\jdk1.8.0_261\bin\java.exe -cp .;d:\External_Library\jmxremote_optional.jar jmx.learning.SystemConfigManagement
Thread Count=10 Schema Name=default
Thread Count=10 Schema Name=default

客户端输出:

d:\Java\jdk1.8.0_261\bin\java.exe -cp .;d:\External_Library\jmxremote_optional.jar jmx.learning.SystemConfigManagementClient

Create a JMXMP connector client
No of Threads=10 and DB Schema Name=default

我不确定您编写的服务器代码,看起来该代码需要 RMIConnector。因此,来自客户端的连接尝试使用 RMI 连接器 Java 远程方法协议 (JRMP) 正在工作。

连你都过了属性-Dcom.sun.management.jmxremote.port=portNum 属性 表示,portNum 是您要启用 JMX RMI 连接的端口号。

注意:出于演示目的,我没有展示安全处理内容,在实际代码中这些内容需要到位。