【问题标题】:Spring 3.0 RmiProxyFactoryBean: how to set connection timeout?Spring 3.0 RmiProxyFactoryBean:如何设置连接超时?
【发布时间】:2012-05-09 18:09:27
【问题描述】:

我需要为 RMI 连接添加“测试”功能(检查另一端的服务器是否可用/存在)。我已经创建了这个类/bean:

 public class MyRmiClientSocketFactory implements RMIClientSocketFactory {

private int timeout;

public void setTimeout(int timeout) {
    this.timeout = timeout;
}

@Override
public Socket createSocket(String host, int port) throws IOException {
    final Socket socket = new Socket();
    socket.setSoTimeout(timeout);
            socket.setSoLinger(false, 0);
            socket.connect(new InetSocketAddress(host, port), timeout);
    return socket;
}

 }

 <bean id="myRmiClientSocketFactory" class="org.myapp.MyRmiClientSocketFactory">
    <property name="timeout" value="2000"/>
</bean>

 <bean id="myExecutor" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
    <property name="serviceInterface" value="org.myapp.MyExecutor"/>
    <property name="serviceUrl" value="rmi://localhost:1099/myExecutor"/>
<!--        <property name="refreshStubOnConnectFailure" value="true"/> -->
<!--        <property name="lookupStubOnStartup" value="false"/> -->
    <property name="registryClientSocketFactory" ref="myRmiClientSocketFactory"/>
</bean>

当我在“serviceUrl”中设置“错误”网址时,我预计 2 秒后会出现“连接超时”,但这不会发生。知道如何使它成为可能吗?

【问题讨论】:

    标签: spring rmi connection-timeout


    【解决方案1】:

    您设置了读取超时,而不是连接超时。调用 connect() 时会发生连接超时。

    【讨论】:

    • 是的,就是这样。我更改了 createSocket(...) 方法(上图),现在可以了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 2017-09-18
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多