Java RMI - Client Timeout

◇◆丶佛笑我妖孽 提交于 2019-11-26 11:07:27

问题


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?


回答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 );
                }
            } );



回答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



回答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




回答4:


You can use custom socket factories. It works fine , It is not static and deprecated and against the system property, it does not apply to the whole JVM. But the point is that the code is on the server side.

Creating a Custom RMI Socket Factory




回答5:


tomcat config: go to vmoptions set -Dsun.rmi.transport.tcp.responseTimeout=30000 (milliseconds)

websphere config: go to Application servers > server1 > Process definition > Java Virtual Machine set -Dsun.rmi.transport.tcp.responseTimeout=30000 in Generic JVM arguments section



来源:https://stackoverflow.com/questions/1822695/java-rmi-client-timeout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!