What would be the Spring way of using TCP connections?
java, sockets, spring, tcp
Solution
Take a look at WebServiceTemplate. That is the main abstraction Spring provides for client-side web service access. Even though your server is not a typical web service, as long as it uses the same request-response pattern, you may still be able to use that as the basis for your solution. The class provides hooks for just about every part of the communication (marshalling, sending the request, receiving the response, unmarshalling, etc.). The JavaDoc lists all the steps it takes to perform web service calls and you can override pretty much anything there. So, for example, you can use the built-in marshalling support, but override `createConnection` to build your custom TCP connection.
Problem
I am reading and write XML over a TCP connection (not HTTP) as part of a web service I'm developing, and I was wondering whether there is a more "springified" way (or even other ideas) of achieving what I'm trying below: ``` InputStream is = null; OutputStream os = null; Socket s = null; try { s = new Socket(address, portNo); os = s.getOutputStream(); os.write(msg.getBytes()); os.flush(); is = s.getInputStream(); String xml = IOUtils.toString(is); return xml; } finally { IOUtils.closeQuietly(os); IOUtils.closeQuietly(is); if (s != null) s.close(); } ``` Note, I've got no control over the server, so I don't think I'll be able to use Spring remoting, but was wondering whether this can be improved akin to spring's JdbcTemplates. EDIT: Note, just to clarify IOUtils is Apache commons-io...