【发布时间】:2019-05-02 20:44:50
【问题描述】:
我被困在一个看似简单的任务上,但我没有想法。我有一个 TcpInboundGateway 附加到一个 TcpNetServerConnectionFactory ,它将请求传递给一个服务激活器。服务激活器只是将消息放回网关的回复通道。我希望网关通过连接返回该消息。
当我运行测试时,消息会成功发送到服务激活器。我知道这一点是因为服务激活器会在返回消息负载之前打印它。我也知道服务激活器将消息放在正确的通道上,因为我在该通道上有一个拦截器,它也打印消息。
问题似乎是网关没有读取该通道,即使我将其设置为setReplyChannel()。我也可以在日志中看到这一点:
Adding {bridge:null} as a subscriber to the 'testResponseChannel' channel
这让我怀疑消息只是被发送到空通道而不是被我的网关接收。
这是配置:
@Bean
public TcpNetServerConnectionFactory testServerFactory() {
TcpNetServerConnectionFactory testServerFactory = new TcpNetServerConnectionFactory(0);
testServerFactory.setSerializer(TcpCodecs.lengthHeader2());
testServerFactory.setDeserializer(TcpCodecs.lengthHeader2());
return testServerFactory;
}
@Bean
public DirectChannel testRequestChannel() {
return new DirectChannel();
}
@Bean
public DirectChannel testResponseChannel() {
DirectChannel testResponseChannel = new DirectChannel();
testResponseChannel.addInterceptor(channelInterceptor());
return testResponseChannel;
}
@Bean
public TcpInboundGateway gateway() {
TcpInboundGateway gateway = new TcpInboundGateway();
gateway.setConnectionFactory(testServerFactory());
gateway.setRequestChannel(testRequestChannel());
gateway.setReplyChannel(testResponseChannel());
return gateway;
}
@Bean
@ServiceActivator(inputChannel = "testRequestChannel", outputChannel = "testResponseChannel")
public EchoHandler echoHandler() {
return new EchoHandler();
}
这是我的 POJO 服务激活器:
public class EchoHandler {
public Message<String> echoMessage(Message<String> request) {
System.out.println(request.getPayload());
return request;
}
}
这是错误,它发生在消息通过拦截器之后:
Unexpected message - no endpoint registered with connection interceptor: localhost:6060:59848:3c6c3cff-c697-4fc9-b4e3-9ea14508cec7 - GenericMessage [payload=byte[3], headers={ip_tcp_remotePort=6060, ip_connectionId=localhost:6060:59848:3c6c3cff-c697-4fc9-b4e3-9ea14508cec7, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=93c75664-54db-c93e-ab3a-3e06b1e4b626, ip_hostname=localhost, timestamp=1556828832645}]
【问题讨论】:
-
这不是TCP客户端的问题吗?所以,你在那里使用
TcpSendingMessageHandler,而不是TcpOutboundGateway。我们有没有机会从你那里得到一个简单的项目来玩和复制? -
啊哈,你是对的。我以为问题出在服务器上,但实际上是在客户端上。服务端网关成功发回消息,但客户端没有 TcpListener 来接收。