Spring集成头
问题描述:
我在Spring集成实现了一个简单的TCP连接工厂:Spring集成头
@Bean
@ServiceActivator(inputChannel = "toTcpChannel")
public TcpSendingMessageHandler tcpOutClient() throws Exception {
TcpSendingMessageHandler sender = new TcpSendingMessageHandler();
sender.setConnectionFactory(clientFactory());
sender.setClientMode(false);
sender.afterPropertiesSet();
return sender;
}
@Bean
public AbstractClientConnectionFactory clientFactory() {
final TcpNioClientConnectionFactory factory = new TcpNioClientConnectionFactory(tcpHost, tcpPort);
factory.setSingleUse(true);
return factory;
}
@EventListener
public void handleTcpConnectionOpenEvent(TcpConnectionOpenEvent event) throws Exception {
LOGGER.info("TCP connection OPEN event: {}", event.getConnectionId());
// HERE I would like to have "myCustomID" header here.
}
我找得到,我通过网关在生产TcpConnectionOpenEvent提供自定义ID(或类似的通过拦截器)
@Gateway(requestChannel="toTcpChannel")
public void sendToTcp(@Payload String message, @Header("myCustomID") Long myCustomID);
我知道这是一个事件没有消息,但我不知道怎么去,我会接受输入通道以任何其他方式连接ID。
我正在创建一个类型为my custom id – connection id
的哈希映射。 我无法通过聚合器使用自定义关联,因为响应消息不会包含有关以前发送的消息的任何信息。任何建议将受到欢迎。
答
哦!我懂了。不知道你要从你的自定义TcpSendingMessageHandler
做什么,但只要ApplicationEventPublisher
是单线程的,你可以将connectionId
存储在ThreadLocal
变量中,并在发送操作后从那里获取它。
对不起,什么是问题? 'TcpConnectionOpenEvent'无疑是开始连接,这是真的,没有任何消息要处理。然而,在这个事件中,你真的可以为你的未来目的存储'connectionId'。 –
我需要一个拦截器,我已经有一个连接ID,我仍然有发送通道标题。我的第一种方法是覆盖TcpSendingMessageHandler。 – crm86