如何在带有弹簧集成的套接字上设置回复超时?

时间:2018-10-15 09:32:16

标签: java spring sockets spring-integration

我如下创建一个nio套接字服务器。

如果超时,我如何定义超时,该超时将自动将某种错误响应发送回客户端?

@MessageEndpoint
public class SocketEndpoint {
    @ServiceActivator(inputChannel = "serverChannel", sendTimeout = "5000")
    public String handleMessage(String message) {
        TimeUnit.SECONDS.sleep(60);
        //...TODO how to send some kind of "timeout exceeded" response?
    }
}

    @Bean
    public TcpConnectionFactoryFactoryBean factory() {
        TcpConnectionFactoryFactoryBean f = new TcpConnectionFactoryFactoryBean();
        f.setType("server");
        f.setPort(port);
        f.setUsingNio(true);
        f.setSingleUse(false);
        f.setDeserializer(deserializer);
        f.setSerializer(serializer);
        return f;
    }

    @Bean
    public TcpInboundGateway server(
            TcpConnectionFactoryFactoryBean factory,
            MessageChannel serverChannel) throws Exception {
        TcpInboundGateway g = new TcpInboundGateway();
        g.setConnectionFactory(factory.getObject());
        g.setRequestChannel(serverChannel);
        return g;
    }

1 个答案:

答案 0 :(得分:0)

没有办法做到。

在客户端,您可以设置套接字超时,这样,如果在该时间内未收到回复,客户端将获得异常。