I have a Spring Integration + spring batch requirement where i have to establish a TCP client connection with remote server and send the request consisting of thousands of records processed from spring batch. I'm using MessageBuilder for the payload and message channel to send the data. We send all the records and then send the request for response message payload to start receiving the response from server. Server respond back with response data which is processed using the transformer and added to queuechannel to read later.
@Configuration
@EnableIntegration
public class GatewayConfig {
private static final Logger logger = LoggerFactory.getLogger(GatewayConfig.class);
@Autowired
private GatewayProperties properties;
/**Abstract class for client connection factories.
*
* @return AbstractClientConnectionFactory
* @throws Exception
*/
@Bean
public AbstractClientConnectionFactory clientCF() {
AbstractClientConnectionFactory factory = new TcpNetClientConnectionFactory( "138.25.189.110",5030);
factory.setSoKeepAlive(true);
factory.setDeserializer(new ByteArraySingleTerminatorSerializer((byte) 13));
return factory;
}
/**
* Receives messages over TCP.
* Specify the MessageChannel to which produced Messages should be sent.
* @param connectionFactory
* @return TcpReceivingChannelAdapter
*/
@Bean
public TcpReceivingChannelAdapter inboundAdapterClient(@Qualifier("clientCF")AbstractClientConnectionFactory connectionFactory) {
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
// adapter.setOutputChannelName(GatewayConstants.AGGREGATOR_PRE_INPUT_CHANNEL);
adapter.setOutputChannelName(GatewayConstants.PRETRANSFORM_CHANNEL);
adapter.setErrorChannelName(GatewayConstants.TCP_ERROR);
adapter.setClientMode(true);
adapter.setConnectionFactory(connectionFactory);
return adapter;
}
@Bean(GatewayConstants.RECEIVE_CHANNEL_NAME)
public QueueChannel receivingChannel() {
return new QueueChannel();
}
@Bean
@Transformer(inputChannel = GatewayConstants.PRETRANSFORM_CHANNEL, outputChannel = GatewayConstants.RECEIVE_CHANNEL_NAME)
public Pretransform pretransformMet() {
return new Pretransform();
}
/**
* Sends messages over TCP
* @param connectionFactory
* @return
*/
@Bean
@ServiceActivator(inputChannel = GatewayConstants.SEND_TO_VENDOR_CHANNEL_NAME)
public TcpSendingMessageHandler messageHandlerClient(@Qualifier("clientCF")AbstractClientConnectionFactory connectionFactory) {
TcpSendingMessageHandler sendingHandler = new TcpSendingMessageHandler();
sendingHandler.setConnectionFactory(connectionFactory);
sendingHandler.setClientMode(true);
return sendingHandler;
}
/**
*
* @return
*/
@Bean(GatewayConstants.TCP_ERROR)
public MessageChannel tcpErrrorChannel() {
return new DirectChannel();
}
@Bean(GatewayConstants.PRETRANSFORM_CHANNEL)
public MessageChannel preReceiveChannel() {
return new DirectChannel();
}
@Bean(GatewayConstants.BATCH_SEND_CHANNEL_NAME)
public MessageChannel sendchannel() {
return new DirectChannel();
}
/**
*
* @return
*/
@Bean(GatewayConstants.SEND_TO_VENDOR_CHANNEL_NAME)
public MessageChannel sendToVendorChannel() {
return new DirectChannel();
}
The batch job runs midnight everyday. After the application started/deployed the request is sent and response received successfully but the next day the server is not receiving any data. There are no tcp connection exceptions when sending the request but server is not receiving any data on it's end.
I even set singleuse to true but getting run time excpetion that client cannot set sing use to true. Is there a way i can log whenever the connection establish and status of tcp connection before sending the request and after receiving the response? Appreciate your help and thank you in advance.
You can add an
ApplicationListener
bean (or@EventListener
method) to receive all kinds ofTcpConnectionEvent
s (open, close etc).See the documentation.
EDIT
Simply add this to one of your beans...