我正在创建一个连接到服务器套接字的客户端。服务器直接回复并关闭套接字本身。
以下代码通常有效。意思是:我可以看到打印到控制台的输出。
@Service
public class MyService {
@Autowired
@Qualifier("clientChannel")
private MessageChannel clientChannel;
public void send() {
Message<?> msg = MessageBuilder.withPayload("test\n").build();
Message<?> rsp = new MessagingTemplate(clientChannel).sendAndReceive(msg);
}
}
@Component
public class MyConfig {
@Bean
public AbstractClientConnectionFactory clientFactory() throws Exception {
TcpConnectionFactoryFactoryBean f = new TcpConnectionFactoryFactoryBean();
f.setType("client");
f.setHost(host);
f.setPort(port);
f.setUsingNio(true);
f.setSingleUse(true);
f.setSoTimeout(timeout);
f.setDeserializer(new ByteArrayRawSerializer());
return fact;
}
@Bean
@ServiceActivator(inputChannel = "clientChannel")
public TcpOutboundGateway outGateway(AbstractClientConnectionFactory factory,
@Qualifier("replayChannel") MessageChannel chan) throws Exception {
TcpOutboundGateway g = new TcpOutboundGateway();
g.setConnectionFactory(factory);
g.setReplyChannel(chan);
return g;
}
@ServiceActivator(inputChannel = "replyChannel")
public void replyHandler(byte[] in) {
System.out.println("replyHandler:"+new String(in));
}
@Bean
public MessageChannel clientChannel() {
return new DirectChannel();
}
@Bean
public MessageChannel replyChannel() {
return new DirectChannel();
}
}
问题:我想放弃replyChannel
并使用clientChannel
通过msg.sendAndReceive()
接收回复。
@Bean
@ServiceActivator(inputChannel = "clientChannel")
public TcpOutboundGateway outGateway(AbstractClientConnectionFactory factory,
@Qualifier("clientChannel") MessageChannel chan) throws Exception {
TcpOutboundGateway g = new TcpOutboundGateway();
g.setConnectionFactory(factory);
g.setReplyChannel(chan);
return g;
}
但这不起作用。执行在TcpOutboundGateway
内永远挂起,永远不会返回。但是为什么?
在调试模式下,我可以看到以下内容一遍又一遍地打印出来:
DEBUG o.s.i.i.t.TcpOutboundGateway: second chance
如果我使用我想要删除的replyChannel
,则不打印。
答案 0 :(得分:1)
回答我自己的问题:
完全省略g.setReplyChannel(chan);
。然后网关默认将输出发送回消息模板。