This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Socket Inbound Listener

Mar 2nd, 2010, 04:44 PM

I am in the process of implementing a basic socket listener service. This listener will accept connections on a server socket, read a message from the InputStream of the 'accept' socket, process the message through a series of channels, and eventually write a response to the OutputStream of the same socket. Content of the channels aside, this will behave more or less like a echo server.

I am considering several approaches and would appreciate any feedback.

Option 1) Implement listener as a standalone component (i.e. not associated with a channel). The listener would read the incoming message and then pass it through a gateway. The response would eventually be returned through the gateway. The integration layer would deal only with Strings and/or byte[]'s and have no direct relationship with the socket.

Option 2) Leverage the socket support which is planned for 2.0: http://forum.springsource.org/showthread.php?t=80814. I looked through the code on trunk for this and I see how it would be used to either send or receive messages. However, I don't see a clear way to return a response to any of the various implementations of AbstractInternetProtocolReceivingChannelAdapter. Am I missing something, or does this fall outside the intended usage?

Option 3) Provide a method in the listener to use as the MessageSource of SourcePollingChannel (i.e. <inbound-channel-adapter>), and provide a separate method which would be invoked by a <service-activator> to send the reply back to the original socket. Since the listener will handle multiple connections, a Map or something similar would be used to ensure that the response is sent to the correct socket. This seems rather messy, and might just indicate that I am missing something in option 2.

From here, option 1 seems like the most straightforward way to accomplish this.

Comment

Mark
i was looking for the same functionality of having some sort of gateway for handling asynchronous IO operations.
After reading threads and googling, i was redirected to JIRA INT-1008. I guess latest code is in trunk.
However, could you kindly explain me how will it work?
Let's say, i have implemented a ServerSocket using NIO classes that listens for requests, processes them and then reply back to the Client.
I read from JIRA that you guys have implemented a SimpleTcpNetInboundGateway and SimpleTcpNetOutboundGateway.
As i m littlebit lost on this..(and again i m confused on Outbound/Inbound things), could you explain me a simlpe usecase?

Will TCP clients send messages to OutboundGateway channel, and read messages from InboundGatewayChannel?