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

public void messageReceived(

ChannelHandlerContext ctx, MessageEvent e) {
// Print out the line received from the server.


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) {


// 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”)) {




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. 🙂


December 11, 2009

Is it possible to add groovy code to a java project?

Filed under: groovy, java, tip — michaelangela @ 3:47 am

Yes and it’s not so crazy either. The fact that you can swap the extension of a Java class to .groovy and continue to build with it in groovy is pretty cool, too.

Wabi Sabi Software: A Tutorial for Adding Groovy to a Java Project

The steps are basically
a) download and install the Groovy GDK
b) add a how-to-compile-groovy task to your Ant build.xml (the task is included in the GDK)
c) add the groovy-all.jar file to your library path
d) add the groovy-plugin to your Eclipse project
e) enable groovy-nature in your Eclipse project
f) write a Groovy class and use it.

Overall this should take no more than twenty minutes or so. One of the really cool things is that once you have a Groovy class you can use it from your Java classes just like any other class…your Java code has no idea its using a class written in Groovy.
Now lets look at the steps listed above in cookbook manner.

December 10, 2009

Java async communication tools tips and tricks… well only 2

Filed under: education, java — michaelangela @ 1:34 pm

Java async communication is no simple matter and these posts highlight the issues as well as provide some solutions. The NIO tutorial is close to what I want but the callback API may bring in the simplicity I desire. I’ve been dusting off the Java portion of my memory and digging in a bit.

Rox Java NIO Tutorial

This tutorial is intended to collect together my own experiences using the Java NIO libraries and the dozens of hints, tips, suggestions and caveats that litter the Internet. When I wrote Rox all of the useful information existed as just that: hints, tips, suggestions and caveats on a handful of forums. This tutorial actually only covers using NIO for asynchronous networking (non-blocking sockets), and not the NIO libraries in all their glory. When I use the term NIO in this tutorial I’m taking liberties and only talking about the non-blocking IO part of the API.

tech: Java : A callback API for epoll(), building on top of Java NIO

I just finished the first draft of the Java callback API that allows you to do asynchronous socket communication using the scalable epoll() mechanism of Linux. This is built on top of Java NIO, so it will take advantage of the best underlying network mechanism the OS has to offer. In Linux 2.6 + /FreeBSD this will likely be epoll().


The Java NIO api can certainly be used for this purpose. But using that API is somewhat difficult and there are various caveats you have to guard against. You need to understand the Selector class, SelectionKey class, InterestOps class, SocketChannel class and their interplay. You need to know the logic for canceling SelectionKeys and setting InterestOps appropriately. And you need to understand the cryptic ByteBuffer class and its variants, possibly how the ByteArrayOutputStream works as well. You need to figure out how to use these classes to store the data separately for each connection, keeping in mind that the data will likely arrive mixed. You also should not call select if all sockets have been completely read, or select() will block – which means you will have to keep track of pending hosts.

December 8, 2009

sudo visudo and environment variables like JAVA_HOME

Filed under: bash, java, osx, tip — michaelangela @ 3:26 am

I was trying all sorts of incantations to get JAVA_HOME set. None worked. OS X. Bash. .bashrc, .profile, etc., etc. Turns out the following is the problem, and the solution.

sudo, JAVA_HOME and Mac OS X [Article] « elc technologies

The problem is that JAVA_HOME doesn’t get passed to sudo, so sudo cannot access it. I remembered two solutions to fix this problem, one by telling the env to keep the variable and the other by switching to root user and exporting the variable there.

So, the first solution is to make the JAVA_HOME variable available to sudo, first export it and then run sudo visudo and add the following line to it:

Defaults    env_keep +="JAVA_HOME"

In the end, it had to go in /etc/profile for it to take as noted below. Maven (mvn -v) just would not show the new version of java otherwise and builds done with maven would fail because they targeted the wrong version as well.

Compiling Java 1.6 projects using Maven on Mac OS X

You can declare this either as a one time export in your current shell above, in /etc/profile as I normally do or in any other startup file of your choice. Afterwards be sure to refresh your shell before you try again. This can be done either by closing your current terminal window and opening a new one or by sourcing whichever file you’ve put the above information into.

source /etc/profile

In order to make sure that it has taken effect one can output it in the shell as follows.


If it shows nothing then the shell has not picked up your change.

December 7, 2009

The source of my early Java knowledge

Filed under: education, java — michaelangela @ 12:14 pm

Once upon a time I worked for Lucent. I worked with a lot of great people there and had a good time there actually except for the fact that I wanted to telecommute… except I don’t think that term was popular yet. But among the other good things that happened there, I got a chance to learn Java. And somewhere in that course, Bruce Eckel’s name came up and the resources for the class I took might have been based on his materials as well.

At any rate it is now time for a refresher. 🙂 Java has come a long way since then. Not to mention the computers. I think at  that time my work laptop was around 130MHz beast! But I’d have to ask to confirm that. 🙂

Note: all links to mindview.net are not working as it seems to be down. mindviewinc.com links work fine though.

Bruce Eckel’s Free Electronic Books

These are electronic books in HTML on C++ and Java, along with the source code. The HTML books are fully indexed, use Frames for easy navigation through the chapters, and have color syntax highlighting on all the source-code listings. Each HTML download contains an entire book and source code in a single zipped file.

March 31, 2008

Starting Eclipse with an alternate VM

Filed under: cairngen, cairngorm, eclipse, java, tool — michaelangela @ 10:29 pm

So I was trying out the Soylatte Java VM to get it to work with Eclipse so I could use Cairngen on a 10.4 Mac. I got an error after working with some Eclipse configurations so that Eclipse started with errors and didn’t allow much work. However starting from the command line (/Applications/eclipse/eclipse) got me back. I can also set the VM I want to use with the -vm argument. (Side note, I can use Scribefire’s “Custom HTML” option to insert <code> tags where needed it seems!)

Java Runtime Environment (JRE) – How to change the default JRE when there are several version of JRE in Sys

I recently installed Oracle 9i client on my new box
and now Eclipse will not start. I get an error

You can set the path to your VM in the eclipse start command:
eclipse.exe -vm <path-to-your-javaw.exe>

eclipse.exe -vm c:\program files\java\jdk1.5.09_09\bin\javaw.exe

Another way is to locate the VM with your path environment. Set the variable JAVA_HOME to the root path of your VM installation. Update your path settings and remove all occurences of java. At the end add the following: %JAVA_HOME%\bin
Now delete javaw.exe and java.exe from your windows\system32 directory. They’re not needed, because they are in your path.

So all java programs are in the path. To change the VM you only need to change the JAVA_HOME variable.

Java 6 Port for Mac OS X 10.4 and 10.5 (Intel)

Filed under: cairngen, dev, java, tool — michaelangela @ 9:20 pm

What ho! Hey I just complained about no Java 6 for the Mac and then this pops up! Now will it work for what I am trying to do with Cairngen?!

SoyLatte – Port of BSD Java

SoyLatte is a functional, X11-based port of the FreeBSD Java 1.6 patchset to Mac OS X Intel machines. SoyLatte is initially focused on supporting Java 6 development; however, the long-term view far more captivating: open development of Java 7 for Mac OS X, with a release available in concert with the official Sun release, supported on all recent versions of Mac OS X.

The name “SoyLatte” was chosen to clearly differentiate from Sun’s trademarks (Red Hat already claimed IcedTea, and I drank a lot of double soy lattes while working on this. (I think it’s funny)

The work is being done as a part of the FreeBSD Java project, with changes being pushed upstream to the FreeBSD code base as the port progresses. Coordination with OpenJDK is also progressing. SoyLatte is a staging ground for the Mac OS X port — it is not intended to be a fork. For more information on licensing, and the project’s relationship with FreeBSD and OpenJDK, please see the licensing section below.

Cairngen, Eclipse and “Unable to load a script engine manager”

Filed under: cairngen, cairngorm, dev, java, tool — michaelangela @ 9:16 pm

I need to add a bunch of functionality and I really want to automate the file creation so I just started trying out Cairngen. I got this “unable to load a script engine manager” error. Fortunately I had just checked my Java VM version (default Mac OS 1.5) and according to this post I need at least JRE 6. Let’s see now… hrmm… Java on a Mac is a little trickier than I thought… distressing

Eric Feminella: Solutions Architect, Rich Internet Applications » Blog Archive » Cairngen 2.1

  1. Leif Wells UNITED STATES Says:

    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:


    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?



  2. eric UNITED STATES Says:

    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

Blog at WordPress.com.