2

我需要为 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 秒后出现“连接超时”,但这不会发生。知道如何使它成为可能吗?

4

1 回答 1

1

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

于 2012-05-09T23:21:51.333 回答