Spring Integration TCP - 在发送数据之前用消息发起握手 [英] Spring Integration TCP - Initiate handshake with a message before sending data

查看:59
本文介绍了Spring Integration TCP - 在发送数据之前用消息发起握手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用@MessagingGateway 将数据发送到服务器.我为我的出站网关配置了一个 AbstractClientConnectionFactory 和一个 @ServiceActivator.

I am using @MessagingGateway to send data to to server. I configured a AbstractClientConnectionFactory and a @ServiceActivator for my outbound gateway.

为了向我的服务器发送数据,我需要在启动连接时发送握手消息.如果来自服务器的响应是我期望的握手响应,那么我发送有意义的数据.我最初的解决方案是

In order to send data to my server, I need to send a handshake message when the connection is initiated. If the response from server is the response that I expect for the handshake, then I send meaningful data. My initial solution is

if (gateway.handshake(HANDSHAKE).equals(HANDSHAKE_RESPONSE))gateway.sendData(data);

当我扩大规模时,这不太好,因为我通过 tcp 的呼叫增加了一倍,因为我只需要在连接启动时发送握手,而不是每次都发送.此外,我计划保持连接活跃.

This is not so good when i am scaling up because I have doubled my calls over tcp as I only need to send send handshake on connection initiation and not every time. Also, I plan on keeping the connection alive.

那么在连接启动时,我怎样才能实现这个自定义握手?

So on connection initiation, how can I have this custom handshake implemented?

@Bean
public AbstractClientConnectionFactory clientCF() {
    TcpNetClientConnectionFactory tcpNetClientConnectionFactory = new TcpNetClientConnectionFactory(host, port);

    tcpNetClientConnectionFactory.setSerializer(new serializerDeserializer());
    tcpNetClientConnectionFactory.setDeserializer(new serializerDeserializer());
    tcpNetClientConnectionFactory.setSoKeepAlive(true);
    tcpNetClientConnectionFactory.setSoTimeout(soTimeout);
    return tcpNetClientConnectionFactory;

}

@Bean
@ServiceActivator(inputChannel = "toTcp")
public MessageHandler tcpOutGate(AbstractClientConnectionFactory connectionFactory) {
    TcpOutboundGateway gate = new TcpOutboundGateway();
    gate.setConnectionFactory(connectionFactory);
    return gate;
}


@Bean
public MessageChannel fromTcp() {
    return new DirectChannel();
}

推荐答案

参见 连接拦截器.

可以使用对 TcpConnectionInterceptorFactoryChain 的引用来配置连接工厂.拦截器可用于向连接添加行为,例如协商、安全性和其他设置.框架当前未提供拦截器,但例如,请参阅源代码库中的 InterceptedSharedConnectionTests.

Connection factories can be configured with a reference to a TcpConnectionInterceptorFactoryChain. Interceptors can be used to add behavior to connections, such as negotiation, security, and other setup. No interceptors are currently provided by the framework but, for an example, see the InterceptedSharedConnectionTests in the source repository.

测试用例中使用的HelloWorldInterceptor的工作原理如下:

The HelloWorldInterceptor used in the test case works as follows:

...

虽然框架中没有标准拦截器,但有 测试用例展示了如何使用它们进行初始握手,例如这里这里.

While there are no standard interceptors in the framework, there are test cases that show how to use them for an initial handshake, for example here and here.

测试比您需要的更复杂,因为它们测试多个嵌套的拦截器.

The tests are more complex than you need because they test multiple nested interceptors.

hello world 测试拦截器 发送 Hello 并期待 World! 在套接字第一次打开时.他们实现了客户端和服务器端的握手.

The hello world test interceptors send Hello and expect World! when the socket is first opened. They implement the handshake for both the client and server side.

这篇关于Spring Integration TCP - 在发送数据之前用消息发起握手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆