Java RMI – client timeout

I am using Java RMI to build a distributed system, which must support server loss

If my client uses RMI to connect to the server, if the server fails (such as cable problem), my client should receive an exception to connect to other servers

But when the server was down, my client didn't respond. He was waiting for a reply How to set the timeout?

Solution

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

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 );
                }
            } );
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>