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?
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 );
}
} );
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With