Writing Async(Or async like) Client Socket Code in Java?

Apart from Mina and NIO there are a couple of other libraries (all generally built on top of NIO).

Apart from Mina and NIO there are a couple of other libraries (all generally built on top of NIO) xSocket Netty Grizzly Also JDK 7 is adding support for Async IO.

The technique I used for asynchronous communication was to use an ExecutorService. In my example below, the service would execute a FutureTask that would wait for the next line. In the meantime, the server could continue executing other tasks and periodically poll the reader to see if a result was received yet.

For writing you could implement a similar FutureTask. Note: You need not extend the functionality of an existing Stream/Reader/Writer like I did. This was just the source code that I had handy public class SocketStringReader extends BufferedReader { /** * Internal buffer used for asynchronous reads */ private HashMap buf = null; /** * Waiting status used for asynchronous reads */ private boolean waiting = false; /** * Shared ExecutorService for asynchronous reads */ private static ExecutorService executor = Executors.

NewCachedThreadPool(); /** * Constructor here to pacify Java * @param in The InputStream to be used as the underlying stream */ public SocketStringReader(InputStream in) { super(new InputStreamReader(in)); } @Override public HashMap readHashMap() throws IOException, ClassNotFoundException { if (buf! = null) { HashMap resp = new HashMap(buf); buf = null; return resp; } return stringToHashMap(this.readLine()); } /** * Parses a string and converts it to a HashMap * @param map A String object of the format "{key=value, key=value, ...}" * that is parsed into a HashMap * @return The parsed HashMap */ public static HashMap stringToHashMap(String map) { // take the string apart String split = map. Split("={},"); HashMap result = new HashMap(); for (int I = 1; I future = new FutureTask( new Callable() { @Override public HashMap call() throws Exception { buf = stringToHashMap(readLine()); logger.

Debug("Read object with sessionid: " + buf. Get("sessionid")); waiting = false; return null; } }); executor. Execute(future); } } return null; } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions