Spring 3.0 RmiProxyFactoryBean:如何设置连接超时?

时间:2012-05-09 18:09:28

标签: spring rmi connection-timeout

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

 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秒后出现“连接超时”,但这种情况不会发生。知道怎么做才有可能吗?

1 个答案:

答案 0 :(得分:1)

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