I want to achieve the following using spring-integration
: having a singleton open socket that constantly receives and writes data, asyncrhon!
This means I have to open a socket that constantly reads from the single socket, dispatches each message for async processing, and return the responses over the socket also async.
How can I achieve that asynchron pattern?
Especially: how can I use Serializer/Deserializer
? As far as I understood, a serializer is only invoked on a new socket connection, so in my case only once at start of the first message?
@Configuration
public class SocketConfig {
@Bean
public TcpConnectionFactoryFactoryBean tcpFactory(MyConverter converter) {
TcpConnectionFactoryFactoryBean fact = new TcpConnectionFactoryFactoryBean();
fact.setType("server");
fact.setPort(PORT);
fact.setUsingNio(true); //should I use true or false?
fact.setSingleUse(false); //keep socket constantly open
fact.setSerializer(converter);
fact.setDeserializer(converter);
return fact;
}
@Bean
public TcpInboundGateway serverGateway(
@Qualifier("tcpFactory") TcpConnectionFactoryFactoryBean factory,
@Qualifier("serverChannel") MessageChannel serverChannel) throws Exception {
TcpInboundGateway g = new TcpInboundGateway();
g.setConnectionFactory(factory.getObject());
g.setRequestChannel(serverChannel);
return g;
}
}
@MessageEndpoint
public class SocketEndpoint {
@ServiceActivator(inputChannel = "serverChannel")
public Object run(Object obj) {
}
}
@Service
public class MyConverter implements Serializer<Object>, Deserializer<Object> {
//read from socket
@Override
public Object deserialize(InputStream inputStream) {
}
//send back to socket
@Override
public void serialize(Object message, OutputStream outputStream) {
}
}