Here is the code for the interceptor approach - I added it to the tcp-client-server-sample and it worked fine. The only problem with it is it causes the gateway to emit an ERROR log when it detects the socket closed unexpectedly.
Code:
ERROR: org.springframework.integration.ip.tcp.connection.TcpNetConnection - Read exception localhost:46020:1579948023 SocketException:null:Socket is closed
Code:
package org.springframework.integration.samples.tcpclientserver;
import org.springframework.integration.Message;
import org.springframework.integration.ip.tcp.connection.AbstractTcpConnectionInterceptor;
import org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptor;
import org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactory;
/**
* @author Gary Russell
*
*/
public class SocketClosingInterceptorFactory implements
TcpConnectionInterceptorFactory {
public TcpConnectionInterceptor getInterceptor() {
return new AbstractTcpConnectionInterceptor() {
@Override
public void send(Message<?> message) throws Exception {
super.send(message);
this.close();
}
};
}
}
Code:
<ip:tcp-connection-factory id="crLfServer"
type="server"
single-use="true"
interceptor-factory-chain="socketClosingFactoryChain"
port="11111"/>
<beans:bean id="socketClosingFactoryChain" class="org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain">
<beans:property name="interceptors">
<beans:array>
<beans:bean class="org.springframework.integration.samples.tcpclientserver.SocketClosingInterceptorFactory"/>
</beans:array>
</beans:property>
</beans:bean>