tags:

views:

484

answers:

2

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?

A: 

There is a system property that you can set; something like sun.rmi.transport.connectionTimeout. I'll try and find the link:

edit; they are detailed here: http://java.sun.com/j2se/1.4.2/docs/guide/rmi/sunrmiproperties.html

oxbow_lakes
Thx, it would helpa lot!
Joao Guilherme
Apols for brevity and inaccuracy; no Internet and iPhone being slow...
oxbow_lakes
I was thinking that it could be the sun.rmi.transport.connectionTimeout.But the defaulf value is 15 seconds. My client keeps waiting for the reply for minutes.. =/
Joao Guilherme
+2  A: 

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 );
                }
            } );
ZZ Coder
Thanks for this! Exactly what I was looking for...
Fortega