0

The code works when the server and client are on the same machine. When I run the server code on a different machine, I get the exception below.

[WARNING] 
java.rmi.ConnectException: Connection refused to host: 127.0.1.1; nested exception is: 
    java.net.ConnectException: Operation timed out (Connection timed out)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:129)
    at javax.management.remote.rmi.RMIServerImpl_Stub.newClient(Unknown Source)
    at javax.management.remote.rmi.RMIConnector.getConnection(RMIConnector.java:2430)
    at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:308)
    at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:270)
    at Client.main(Client.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:282)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Operation timed out (Connection timed out)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:211)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
    ... 14 more

It's suspicious that the exception says 127.0.1.1 refused the connection when the client really used the JMXServiceURL below:

service:jmx:rmi:///jndi/rmi://10.10.173.196:8474/jmxrmi

If I start the application using the -Dcom.sun.management.jmxremote.* settings, the client doesn't not have an issue with remote connections.

Code is pasted below:

import javax.management.MBeanServer;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;
import java.util.*;

public class Server {

    public static void main(String[] args) throws Exception {
        if(args.length != 2) {
            throw new IllegalArgumentException("Server <port> <path suffix>"
                            + "\nservice:jmx:rmi://0.0.0.0:<port>/jndi/rmi://0.0.0.0:<port>/<pathsuffix>"
                            );
        }

        int port = Integer.parseInt(args[0]);
        String pathSuffix = args[1];

        LocateRegistry.createRegistry(port);

        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://0.0.0.0:"
                        + port + "/jndi/rmi://0.0.0.0:" + port + "/" + pathSuffix);

        Map<String, Object> envConf = new HashMap<>();
        envConf.put("jmx.remote.x.password.file", "password.properties");
        envConf.put("jmx.remote.x.access.file", "access.properties");

        JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, envConf, mbs);
        cs.start();

        while(true) {
            Thread.sleep(1000);
        }
    }

}

I also used the JMXServiceURL on the server:

service:jmx:rmi:///jndi/rmi://0.0.0.0:<port>/jmxrmi
joseph
  • 2,429
  • 1
  • 22
  • 43

1 Answers1

0

https://stackoverflow.com/a/11654322/1810962 mentions that /etc/hosts must be configured properly. On the machine that JMX works I have:

hostname -i
10.10.172.72 10.10.172.72

On the machine that doesn't work:

hostname -i
127.0.1.1
joseph
  • 2,429
  • 1
  • 22
  • 43