gpt4 book ai didi

java - 为什么我使用 JMX 时会收到错误 "connection refused"

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:58:56 25 4
gpt4 key购买 nike

我无法连接到 JMX 对象。以下是我创建 JMX 对象的方法:

public static void main(String... args) {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
try {
ObjectName name = new ObjectName("org.javasimon.jmx.example:type=Simon");
if (mbs.isRegistered(name)) {
mbs.unregisterMBean(name);
}
SimonManagerMXBean simonManagerMXBean = new SimonManagerMXBeanImpl(SimonManager.manager());
mbs.registerMBean(simonManagerMXBean, name);
System.out.println("SimonManagerMXBean registerd under name: "+name);

} catch (JMException e) {
System.out.println("SimonManagerMXBean registration failed!\n"+e);
}

while (true) {
// waiting for connections
}
}

这是连接到远程 JMX 对象的代码:

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://127.0.0.1:9999/jndi/rmi://127.0.0.1:1099/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
SimonManagerMXBean simonManagerMXBean = JMX.newMXBeanProxy(mbsc, new ObjectName("org.javasimon.jmx.example:type=Simon"), SimonManagerMXBean.class);
return simonManagerMXBean;

不幸的是,我收到以下错误:

java.io.IOException: Failed to retrieve RMIServer stub: javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: 
java.net.ConnectException: Connection refused: connect]
at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:338)
at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:248)
at my.code.RemoteSimonManagerFactoryImpl.createSimonManager(RemoteSimonManagerFactoryImpl.java:24)
at my.code.Demo.main(DemoAggregation.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused: connect]
at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:101)
at com.sun.jndi.toolkit.url.GenericURLContext.lookup(GenericURLContext.java:185)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at javax.management.remote.rmi.RMIConnector.findRMIServerJNDI(RMIConnector.java:1886)
at javax.management.remote.rmi.RMIConnector.findRMIServer(RMIConnector.java:1856)
at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:255)
... 9 more
Caused by: java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:97)
... 14 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
... 19 more

如果我尝试使用“jconsole”实用程序连接到我的 JMX 服务器(对于本地进程),我首先收到“ConnectionFailedSSL1”错误,但是当我单击错误表单中的“不安全”按钮时,我连接成功。

服务器和客户端都在同一台计算机上。

我使用的是 Windows 7 x64。 Windows 防火墙已禁用。

最佳答案

Caused by: java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:

很可能您没有使用正确的 JVM 参数运行您的服务器。 Jconsole 使用不同的机制来查找和连接到本地进程。您的客户端代码正在尝试使用 TCP/IP 连接到您的服务器。要打开它,您需要在服务器上的 Java 命令行中添加如下内容:

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=1099

参见:How to activate JMX on my JVM for access with jconsole?

顺便说一句,您可能需要考虑使用我的 SimpleJMX library它为您完成所有这些代码。它还包括一个 JMX 客户端代码。

关于java - 为什么我使用 JMX 时会收到错误 "connection refused",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22118004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com