Monday, March 9, 2009

Groovy javaloader goodness

In my usual mode of trying a bunch of things at once I was rewriting some slow Coldfusion 8 code in Groovy to see if I can improve performance. And I wanted to play with Groovy.

So I set up Groovy Eclipse plug-in and dove into Groovy documentation. I needed a way to plug my Groovy .class files into my Coldfusion code. Mark Mandel has a cool javaloader thing that I haven't used yet.

The advantage of using Javaloader is you don't have to reload Coldfusion every time you recompile your Java code. There might be other advantages but that one was my main interest. It didn't work right the way because I was using Groovy not Java, but Groovy is built on top of Java so what I needed to do is to add Groovy .jar to the loader.

Groovy has multiple .jar files in it's directory. I only had to add two, depending on the code you might need to add more or all I suppose. So if you get an error at first look closely at the Java stack trace and see if there are clues as to what other .jar you might need. For example at first I only added groovy-1.6-RC-2.jar file and after running code got an error "Object Instantiation Exception. "
In the stack trace there was a line:
Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Opcodes
That was a clue to add asm-2.2.3.jar to the fold. (Thanks Brian ;-) )

So in the end it was great. I just worked in Eclipse, switch to Java perspective change my Groovy code, click run to refresh the class file. My Coldfusion files are in CFeclipse and are side by side with Groovy files. Integration dream.

Groovy code did end up being faster by the way then Coldfusion counterpart but I did optimize some things along the way so the comparison is not exact. I am thinking of converting it to Java for the ultimate gain, but Groovy is fast enough for now.

No comments: