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:
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.
@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.






November 19th, 2007 at 10:46 am
Eric, I am trying to use Cairngen 2.1 in FB 3 beta 2 (stand-alone) and getting an error:
BUILD FAILED
C:\…\build.xml:373: Unable to load a script engine manager (org.apache.bsf.BSFManager or javax.script.ScriptEngineManager)
Have you seen this before? Is there a software update I need to install? If so, which one?
Thanks,
Leif
November 19th, 2007 at 11:59 am
Hey Lief,
You need to upgrade to JRE 6. First download and install at: http://www.java.com/en/download/manual.jsp
Once you have upgraded the Java Runtime, restart Eclipse and from
the toolbar select Windows > Preferences > Java (expand) >
Installed JREs > Add > Browse (browse to Java Installation and
select latest JRE). Click OK.
Restart Eclipse and you should be good to go. Let me know if you have any problems.
- Eric