为什么我收到错误“连接被拒绝”与JMX

时间:2014-03-01 17:50:37

标签: java networking jmx

我无法连接到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防火墙已禁用。

4 个答案:

答案 0 :(得分:8)

  

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

很可能您没有使用正确的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客户端代码。

答案 1 :(得分:1)

我收到了同样的错误。在我的情况下,我使用@PostConstruct注释,该方法试图在Spring有机会配置连接之前使用RMI连接。

连接配置没有任何问题,只是我在它准备好之前不小心试图使用它。希望这可能有助于某人。

答案 2 :(得分:1)

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

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

  2. 以管理员权限运行

答案 3 :(得分:0)

检查tomcat的bin文件夹中的catalina.sh文件。打开它,找到java服务器参数。在任何你想要的行上添加以下$ JAVA_OPTS。

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

你完成了!