Why am I getting the error "connection refused" with JMX
Asked Answered
S

4

10

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

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:

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.

Both server and client are on the same computer.

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

Solorzano answered 1/3, 2014 at 17:50 Comment(0)
W
11

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

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:

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

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

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

Wallasey answered 1/3, 2014 at 17:58 Comment(3)
Do I need to add this settings on client side or on server side?Solorzano
The first line reads "not running your server with the right JVM parameters" @IvanMushketyk.Wallasey
It works. Thank you. The correct port number is 1099.Solorzano
T
4

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

  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

Thrifty answered 4/3, 2015 at 22:8 Comment(1)
These parameters works also on Ubuntu 20.04 + Tomcat9Tangential
P
1

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.

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.

Paderna answered 27/6, 2014 at 14:11 Comment(0)
C
0

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.

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

you are done!

Coom answered 21/9, 2016 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.