Agent Smith: scripting languages may not be the answer
Saturday, March 10th, 2007This is the first of a series of articles about Agent Smith. I will begin with an introduction, followed by a “try it, test it how-to”, ending with some examples together with Echo2, Struts and Spring.
As you may know, I am a bit involved with Echo2. Really, I’m a fan of it, I love the way I create ajax UIs coding the swing way. And forgetting the page thing.
But Echo2 has two problems: it consumes a bit of memory (but I don’t really care, since my apps are not intended to be delivered to the world “wild” web) and, if you don’t use EchoStudio, it needs you to reload the webapp to check even small UI changes (obliging you to a constant code-restart-code).
Well, the second problem has been solved. Its solution is called Agent Smith.
Why “agent” ? In java, there are many meanings of the word “agent”. One meaning is a class that has access to the Instrumentation interface, introduced since java 5. With Instrumentation you can call a wonderful method:
With such method you can change the definition of an entire class, while your program is running. If you have ever played with ClassLoader (creating your own implementations) you know what the method “defineClass” does and you know it is final.
Instrumentation can be used as a workaround to the problem of class redefinition, meaning you can actually use java as a scripting language.
So why not just use Groovy or some other scripting language ?
Because Groovy is slow. Because scripting languages are usually slow. Test Groovy. I found it 18 to 20 times slower that “native” java code.
Java has been criticized by every Microsoft developer for being slow. And they were right. Were. They are no more. Why the heck should I use Groovy to code? Would you shoot at your feet?
But don’t take my humble opinion as a “scripting languages are the wrong way” opinion. Groovy in particular has a lot of nice features (especially the syntax) but, since I’ve read articles about using groovy to speed up Struts actions building, you may find an agent a smarter and cleaner solution.
I think there are times when we really need to avoid app server restart. Think about your JEE application running into JBoss, about your JSF backing bean…
Scripting languages allow us to avoid server (or app) restart, but they also add weight to your code, making it slower.
An agent is a bit hard to set up and so it may not be a good solution. But if you are planning to work on the same project for the following two weeks, then it is.
In the next article I will explain how to compile and set up Smith.
See you soon.