需要您的帮助来理解为什么我无法使用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参考
JMX消息传递协议(JMXMP(连接器是通用连接器的配置,其中传输协议基于TCP,对象包装是本机Java序列化。安全性比RMI连接器更高级。安全性基于Java安全套接字扩展(JSSE(、Java身份验证和授权服务(JAAS(以及简单身份验证和安全层(SASL(。
通用连接器及其JMXMP配置是可选的,这意味着它们并不总是包含在JMX远程API的实现中。J2SE平台不包括可选的通用连接器。
注意-如果您想使用JMXMP连接器,请从下载JSR160参考实现http://java.sun.com/products/JavaManagement/download.html,并将jmxremote_optial.jar文件添加到类路径中。
您是否在服务器和客户端类路径中包含了jmxremote_optional.jar
文件?
我以前没有JMXMP连接器方法的经验,我遵循了JMX文档,并试图通过编写以下示例进行验证,它确实有效。
JMX MBean接口
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();
}
实现JMXMBean接口的类
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;
}
}
使用JMXMP在MBean服务器中创建和注册MBean连接器
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连接器客户端
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:Javajdk1.8.0_261binjava.exe -cp .;d:External_Libraryjmxremote_optional.jar jmx.learning.SystemConfigManagement
Thread Count=10 Schema Name=default
Thread Count=10 Schema Name=default
客户端输出:
d:Javajdk1.8.0_261binjava.exe -cp .;d:External_Libraryjmxremote_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连接的端口号。
注意:为了演示的目的,我没有展示安全处理的东西,在真实的代码中,这些东西需要到位。