Java 为什么我在使用 JMX 时收到错误“连接被拒绝”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22118004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:43:50  来源:igfitidea点击:

Why am I getting the error "connection refused" with JMX

javanetworkingjmx

提问by Ivan Mushketyk

I am unable to connect to JMX object. Here is how I create a JMX object:

我无法连接到 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
    }
}

This is a code for connecting to the remote JMX object:

这是连接远程 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;

Unfortunatelly I receive the following error:

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

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

If I try to connect to my JMX server (as to local process) using "jconsole" utility, I first get "ConnectionFailedSSL1" error, but when I click "Insecure" button in the error form, I connect successfully.

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

Both server and client are on the same computer.

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

I am using Windows 7 x64. Windows firewall is disabled.

我正在使用 Windows 7 x64。Windows 防火墙已禁用。

采纳答案by Gray

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

引起:java.rmi.ConnectException:连接拒绝主机:127.0.0.1;嵌套异常是:

Most likely you are not running your server with the right JVM parameters. Jconsole uses a different mechanism to find and to connect to local processes. Your client code is trying to use TCP/IP to connect to your server. To turn this on you'll need to add something like the following to your Java command line on your server:

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

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

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

请参阅:如何在我的 JVM 上激活 JMX 以使用 jconsole 进行访问?

As an aside, you might want to consider using my SimpleJMXlibrarywhich does all this code for you. It includes a JMX client code as well.

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

回答by Ryan D

I received the same error. In my case I was using a @PostConstruct annotation and that method was attempting to use the RMI connection before Spring had a chance to configure the connection.

我收到了同样的错误。就我而言,我使用的是 @PostConstruct 注释,并且该方法试图在 Spring 有机会配置连接之前使用 RMI 连接。

It didn't end up being anything wrong with the configuration of the connection, just that I accidentally tried to use it before it was ready. Hope this might help someone.

连接的配置最终没有出现任何问题,只是我在它准备好之前不小心尝试使用它。希望这可以帮助某人。

回答by Grigory Kislin

I've manage run Tomcat with JMX (Tomcat 8, Windows) by:

我通过以下方式使用 JMX(Tomcat 8,Windows)管理运行 Tomcat:

  1. add -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

  2. run with admin permissions

  1. 添加 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

  2. 以管理员权限运行

回答by K?v?lc?m

check your tomcat's bin folder for catalina.sh file. open it up and find java server parameters. add thoose followings to $JAVA_OPTS on any line you want.

检查您的 tomcat 的 bin 文件夹中是否有 catalina.sh 文件。打开它并找到java服务器参数。在您想要的任何行上将以下内容添加到 $JAVA_OPTS 中。

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

you are done!

你完成了!