【问题标题】:How to set reply-timeout on a socket with spring-integration?如何在带有弹簧集成的套接字上设置回复超时?
【发布时间】:2019-03-19 16:50:22
【问题描述】:

我如下创建一个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;
    }

【问题讨论】:

    标签: java spring sockets spring-integration


    【解决方案1】:

    没有办法做到这一点。

    在客户端,您可以设置套接字超时,这样如果在该时间内没有收到回复,客户端就会收到异常。

    【讨论】:

    • 这正是客户端系统所做的(遗留系统)。但我想预计超时,我自己会生成一个正确的错误消息,我可以在超时发生之前将其发送回客户端。 (因为旧系统的超时程序无法正常工作)。我只能想到将我的方法执行包装到CompletableFuture 并调用future.get(...timeout) 以在客户端超时之前终止。
    猜你喜欢
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多