Java RMI - 客户端超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1822695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:59:14  来源:igfitidea点击:

Java RMI - Client Timeout

javatimeoutrmi

提问by Joao Guilherme

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

我正在使用 Java RMI 构建分布式系统,它必须支持服务器丢失。

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.

如果我的客户端使用 RMI 连接到服务器,如果该服务器出现故障(例如电缆问题),我的客户端应该得到一个异常,以便它可以连接到其他服务器。

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?

但是当服务器出现故障时,我的客户没有任何反应,他一直在等待回复。如何为此设置超时?

回答by oxbow_lakes

There is a system property that you can set.
Something like sun.rmi.transport.connectionTimeout

您可以设置一个系统属性。
就像是sun.rmi.transport.connectionTimeout

They are detailed here:
https://docs.oracle.com/javase/7/docs/technotes/guides/rmi/sunrmiproperties.html

它们在此处详细说明:https:
//docs.oracle.com/javase/7/docs/technotes/guides/rmi/sunrmiproperties.html

回答by ZZ Coder

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

回答by Noky

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:

我最近也遇到了这个问题,发现我需要设置以下 Java 属性,以便 RMI 调用在客户端超时:

sun.rmi.transport.tcp.responseTimeout

Here is the full scoop on these params in newer versions of Java:

以下是在较新版本的 Java 中对这些参数的完整介绍:

回答by user3399000

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.

您可以使用自定义套接字工厂。它工作正常,它不是静态的和不推荐使用的,并且不符合系统属性,它不适用于整个 JVM。但关键是代码在服务器端。

Creating a Custom RMI Socket Factory

创建自定义 RMI 套接字工厂

回答by shirin

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

tomcat 配置:转到 vmoptions 设置 -Dsun.rmi.transport.tcp.responseTimeout=30000(毫秒)

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

websphere 配置:转到应用程序服务器 > server1 > 进程定义 > Java 虚拟机 set -Dsun.rmi.transport.tcp.responseTimeout=30000 在通用 JVM 参数部分