Ramblings

December 14, 2009

Simpler Java asynch IO with JBoss Netty

Filed under: cool, education, groovy, java, mina, netty, tip — michaelangela @ 3:57 am

I don’t know what this will look like since I am using Scribfire which doesn’t like to deal with code very well… that should be a standard disclaimer on this site! I try to clean it up as best I can within a short timeframe though. :)

I recently wrote about some Java libraries/code that would simplify asynch socket communication. I hadn’t looked far enough! Looking for references on Groovy asynch programming I came across this:

ojug meeting tue oct 20th — grails and/or netty : Omaha Java Users Group

Netty has been getting some press as a potential successor to Apache’s Mina asynchronous I/O framework for building low-level custom protocols. E.g. previously I’ve used Mina to talk binary to a card processing system.

And checking in to MINA and Netty yielded some nice info. There is a great little tutorial on writing a simple server in MINA 2. But the simple Netty TelnetClient example was exactly what I was looking for. Specifically:

TelnetClient xref

TelnetClientHandler handler = new TelnetClientHandler();
bootstrap.setPipelineFactory(new TelnetPipelineFactory(handler));

For those more experienced with Java, I hadn’t seen a simpler way to assign what is essentially an event handler. The code I had seen before required using slots and keys or other bits that seemed a bit more verbose than needed. But then again that could be simply because of my inexperience in the language at this point. Seeing this immediately made sense coming from AS3. It’s not a closure like in AS3 where each event gets a method call of some sort, named or anonymous, but just a class that handles the events.

TelnetClientHandler xref

@Override
public void messageReceived(
ChannelHandlerContext ctx, MessageEvent e) {
// Print out the line received from the server.
System.err.println(e.getMessage());

}

Sweet and simple. Once that’s assigned you can go into your “do forever” loop and do the writing that needs to interact with the server.
TelnetClient xref

// Read commands from the stdin.
ChannelFuture lastWriteFuture = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for (;;) {
String line = in.readLine();
if (line == null) {
break;

}
// Sends the received line to the server.
lastWriteFuture = channel.write(line + ‘\n’);

// If user typed the ‘bye’ command, wait until the server closes
// the connection.
if (line.toLowerCase().equals(“bye”)) {

channel.getCloseFuture().awaitUninterruptibly();
break;

}

}

Even without Groovy this is really straightforward. I guess a Groovy implementation of Netty would reduce all of this to like 3 lines of code? Heh. :)

About these ads

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Silver is the New Black Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 42 other followers

%d bloggers like this: