Java RMI - 客户端超时 [英] Java RMI - Client Timeout

查看:346
本文介绍了Java RMI - 客户端超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如果我的客户端使用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.

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

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

这篇关于Java RMI - 客户端超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆