Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java RMI - Client Timeout

Tags:

java

timeout

rmi

I'm building a Distributed System using Java RMI and it must support a server loss.

If my client is connected to a Server using RMI, if this server goes down (cable problems for example), my client should get an exception so it can connect to other server.

But when the server goes down, nothing happens to my client, he keeps waiting for the reply. How can I set a timeout for that?

like image 844
Joao Guilherme Avatar asked Nov 30 '09 22:11

Joao Guilherme


2 Answers

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 );
                }
            } );
like image 61
ZZ Coder Avatar answered Sep 18 '22 15:09

ZZ Coder


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
like image 42
Noky Avatar answered Sep 18 '22 15:09

Noky