Java RMI - Client Timeout

Solution 1:

For socket read timeout, you can set your own factory like this,

           RMISocketFactory.setSocketFactory( new RMISocketFactory()
            {
                public Socket createSocket( String host, int port )
                    throws IOException
                {
                    Socket socket = new Socket();
                    socket.setSoTimeout( timeoutMillis );
                    socket.setSoLinger( false, 0 );
                    socket.connect( new InetSocketAddress( host, port ), timeoutMillis );
                    return socket;
                }

                public ServerSocket createServerSocket( int port )
                    throws IOException
                {
                    return new ServerSocket( port );
                }
            } );

Solution 2:

I recently encountered this problem as well and found that I needed to set the following Java property in order for an RMI call to timeout on the client side:

sun.rmi.transport.tcp.responseTimeout

Here is the full scoop on these params in newer versions of Java:

  • http://docs.oracle.com/javase/6/docs/technotes/guides/rmi/sunrmiproperties.html
  • http://docs.oracle.com/javase/7/docs/technotes/guides/rmi/sunrmiproperties.html

Solution 3:

There is a system property that you can set.
Something like sun.rmi.transport.connectionTimeout

They are detailed here:
https://docs.oracle.com/javase/7/docs/technotes/guides/rmi/sunrmiproperties.html