'TCP Dynamic Client
I need to send request to two tcp external servers, Do i need to use router or need to have a separate AbstractClientConnectionFactory, tcpOutBound?
Updated code - Gary, As Suggested, I am using two connection factories, I will be communicating with two TCP servers, Is this the right way? Cant I just use one single AbstractClientConnectionFactory for two channels?
@Bean(name = "visaConnectionFactory")
public AbstractClientConnectionFactory clientConnectionFactoryVisa() {
ISOConnectionProperties visa = applicationProperties.getIsoServers().getVisa();
TcpNioClientConnectionFactory tcpNioClientConnectionFactory =
new TcpNioClientConnectionFactory(visa.getHost(), visa.getPort());
tcpNioClientConnectionFactory.setUsingDirectBuffers(true);
tcpNioClientConnectionFactory.setApplicationEventPublisher(applicationEventPublisher);
return new CachingClientConnectionFactory(tcpNioClientConnectionFactory, visa.getPoolSize());
}
@Bean(name = "mastercardConnectionFactory")
public AbstractClientConnectionFactory clientConnectionFactoryMastercard() {
ISOConnectionProperties mastercard = applicationProperties.getIsoServers().getMastercard();
TcpNioClientConnectionFactory tcpNioClientConnectionFactory =
new TcpNioClientConnectionFactory(mastercard.getHost(), mastercard.getPort());
tcpNioClientConnectionFactory.setUsingDirectBuffers(true);
tcpNioClientConnectionFactory.setApplicationEventPublisher(applicationEventPublisher);
return new CachingClientConnectionFactory(tcpNioClientConnectionFactory, mastercard.getPoolSize());
}
@Bean
public MessageChannel outboundChannel() {
return new DirectChannel();
}
@Bean
@ServiceActivator(inputChannel = "router.channel")
public HeaderValueRouter consumeRouterMessage() {
HeaderValueRouter router = new HeaderValueRouter("cardType");
router.setChannelMapping("V", "outboundChannelVisa");
router.setChannelMapping("M", "outboundChannelMastercard");
return router;
}
@Bean
@ServiceActivator(inputChannel = "outboundChannelVisa")
public MessageHandler outboundGatewayVisa(
@Qualifier("visaConnectionFactory") AbstractClientConnectionFactory clientConnectionFactory) {
TcpOutboundGateway tcpOutboundGateway = new TcpOutboundGateway();
tcpOutboundGateway.setConnectionFactory(clientConnectionFactory);
tcpOutboundGateway.setRequestTimeout(5_000);
return tcpOutboundGateway;
}
@Bean
@ServiceActivator(inputChannel = "outboundChannelMastercard")
public MessageHandler outboundGatewayMc(
@Qualifier("mastercardConnectionFactory") AbstractClientConnectionFactory clientConnectionFactory) {
TcpOutboundGateway tcpOutboundGateway = new TcpOutboundGateway();
tcpOutboundGateway.setConnectionFactory(clientConnectionFactory);
tcpOutboundGateway.setRequestTimeout(5_000);
tcpOutboundGateway.setUnsolicitedMessageChannelName("unsolicited");
return tcpOutboundGateway;
}
@ServiceActivator(inputChannel = "unsolicited")
public void handleUnsolicited(String in) {
log.debug("Unsolicited response:{}", in);
}
Solution 1:[1]
You need a separate factory and adapter; also see this sample
https://github.com/spring-projects/spring-integration-samples/tree/main/advanced/dynamic-tcp-client
Demonstrates a technique to dynamically add TCP clients on-demand, with caching and LRU removal.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gary Russell |
