I've a TCP client which was built using spring integration TCP and the server supports a keep alive message (ping/pong style). The connections were configured using a CachingClientConnectionFactory and I'd like to take advantage on this server feature. Here's my bean configuration:
private static final int SERIALIZER_HEADER_SIZE = 2;
/**
* Serializer used by connection factory to send and receive messages
*/
@Bean
public ByteArrayLengthHeaderSerializer byteArrayLengthHeaderSerializer() {
return new ByteArrayLengthHeaderSerializer(SERIALIZER_HEADER_SIZE);
}
@Bean
public AbstractClientConnectionFactory tcpClientConnectionFactory() {
TcpNetClientConnectionFactory connFactory =
new TcpNetClientConnectionFactory(props.getUrl(), props.getPort());
connFactory.setSerializer(byteArrayLengthHeaderSerializer());
connFactory.setDeserializer(byteArrayLengthHeaderSerializer());
connFactory.setSoTimeout(props.getSoTimeout());
if (props.isUseSSL()) {
connFactory.setTcpSocketFactorySupport(new DefaultTcpNetSSLSocketFactorySupport(() -> {
return SSLContext.getDefault();
}));
}
return connFactory;
}
/**
* Connection factory used to create TCP client socket connections
*/
@Bean
public AbstractClientConnectionFactory tcpCachedClientConnectionFactory() {
CachingClientConnectionFactory cachingConnFactory =
new CachingClientConnectionFactory(tcpClientConnectionFactory(), props.getMaxPoolSize());
cachingConnFactory.setConnectionWaitTimeout(props.getMaxPoolWait());
return cachingConnFactory;
}
Using the solution posted here Configure keep alive to keep connection alive all the time I can keep the connection opened but I also wanted to take leverage on those server keep alive messages and send those messages from time to time to check if the connection is still alive. This can improve the performance on the client side since it won't need to re-connect/create a new connection if the socket was closed.
Based on that, does anyone has a suggestion on how to implement this using spring integration?
soKeepAliveto true so the operating system will keep the sockets open by sending pings. Then if you don't setsoTimeoutthe socket will remain open indefinitely.KEEP_ALIVE_REQUESTand will send back aKEEP_ALIVE_RESPONSE. My question was about using that to keep the connection open, but based on your responsesoKeepAliveandsoTimeouttogether can do the trick.