Agent Smith

AGENTSMITH 1.0 RELEASED
LICENSE: ASL2
PROJECT SITE: https://agentsmith.dev.java.net/
---------------------------------------------------------------
Hi all,

I want to introduce you to Agent Smith, a tiny jar that, plugged into your tomcat's (or whatever) jvm will avoid you to reload your webapps each time you modify your UI.

Here is a brief explanation.

With java 5 (or 6) you have a new package: java.lang.instrument. With its classes and interfaces you can build an agent, a class that redefines your classes while the jvm (and your application is running)

I just hate the need to reload the webapp each time I modify a label to check "how it looks". I would like to use java like a scripting language, when developing, while having its speed and performance when deploying my application on production.

An agent is something that allows you to do that: Smith will poll your webapp classes folder each second, looking for modified classes. If some class has been modified, it will redefine it.
You just need to re-execute the method you have changed to see your modifications applied. You don't need to reload the whole app. It's just like closing and re-opening the modified window pane to see the new title you've just set.

Read the project page for more details and for instructions on how to use it.

Smith is licenses under the LGPL. If you have issues about the license, mind that Smith is intended to be a development thing and that your application code WON'T be affected by it in any way.

Hope that helps and that you will feed me back with comments and suggestions

Bye all

New version of Smith. That's 0.3 and allows you plug the agent after the jvm started up, without the need to edit the launch parameters of your servlet container.

Unfortunately, such features requires the PID of the virtual machine, something that is OS dependent: I was able to code the Linux version only. Feel free to contribute!

More details available at the project page at http://www.fridrik.it/blog/agent-smith/

Hope that helps

New version 0.4 is out

* "Linux only" problem when using java6 should be solved.
* Defaults to java5 version when building
* Revised project page

Smith 0.5 released

Changelog:
0.5

  • FileMonitor is now handled by a SingleThreadScheduledExecutor instead of the raw Thread.start and Thread.sleep
  • Javadoc added
Available here http://www.fridrik.it/files/Smith-0.5.zip

[/]

java.net has just approved Smith for the java-tools community. The project is currently in the "incubator".
Next few days I will load the available documentation

Bookmark it: https://agentsmith.dev.java.net/

eskatos's picture

Thanks federico for this contribution to the java community.
I hope I'll have time to put Agent Smith to production here in the next weeks :)

Federico,

Does Agent Smith offer some functionality above what you get with HotSwap (http://java.sun.com/j2se/1.4.2/docs/guide/jpda/enhancements.html#hotswap)?

Cheers,
Adrian

adrians wrote:

Federico,

Does Agent Smith offer some functionality above what you get with HotSwap (http://java.sun.com/j2se/1.4.2/docs/guide/jpda/enhancements.html#hotswap)?

Cheers,
Adrian


Yes I think so. Afaik, the difference is that jpda requires native code. But I've never investigated it :(

federico wrote:

Yes I think so. Afaik, the difference is that jpda requires native code. But I've never investigated it :(

Good work federico. Anyway, IntelliJ Idea does this natively.

Grolubao wrote:

Good work federico. Anyway, IntelliJ Idea does this natively.

Good for IntelliJ users! One day I should give a try to that IDE

Grolubao wrote:

Good work federico. Anyway, IntelliJ Idea does this natively.

Does it also reloads class in jars? Smith still has not such feature but I plan to add it as soon as possible

Great work federico. I tried it out and it works fine when you change existing code. But the agent crashes when you add or remove a line of code. Is this the correct behavior?

SEVERE: error
java.lang.UnsupportedOperationException: class redefinition failed: attempted to change the schema (add/remove fields)
at sun.instrument.InstrumentationImpl.redefineClasses0(Native Method)
at sun.instrument.InstrumentationImpl.redefineClasses(Unknown Source)
at it.fridrik.agent.Smith.fileModified(Smith.java:141)
at it.fridrik.filemonitor.FileMonitor.notifyModifiedListeners(FileMonitor.java:178)
at it.fridrik.filemonitor.FileMonitor.checkAddAndModify(FileMonitor.java:120)
at it.fridrik.filemonitor.FileMonitor.checkAddAndModify(FileMonitor.java:113)
at it.fridrik.filemonitor.FileMonitor.checkAddAndModify(FileMonitor.java:113)
at it.fridrik.filemonitor.FileMonitor.run(FileMonitor.java:85)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(Unknown Source)
at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Nima

federico wrote:

Yes I think so. Afaik, the difference is that jpda requires native code. But I've never investigated it :(

Federico,

What do you mean by "requires native code"? I use Eclipse to do HotSwap with web applications all the time. Is your agent meant to be used with IDEs that don't support this feature?

Cheers,
Adrian

adrians wrote:

Federico,

What do you mean by "requires native code"? I use Eclipse to do HotSwap with web applications all the time. Is your agent meant to be used with IDEs that don't support this feature?

Cheers,
Adrian

Adrian,

do you care to tell me how you do your hotswap in eclipse. I'm currently using ant's deploy and undeploy tasks.

Thanks,
Nima

@Nima
Yes it an expected behaviour. The current implementation of the Instrumentation interface does not support adding/removing methods/fields. This includes also changing a method signature (as it is actually a removal and an addition)

As the Instrumentation javadoc says, later versions may not have such limitation, but that's the current implementation available with the jdk (and no, upgrading to java6 won't solve :( )

Btw the agent has not crashed. It is simply telling you that, this time, you need to reload, as it can't go that far. But for any other classes you'll change (without changing their schemas) it will keep on working

@adrians
yes, please, tell me how to achieve the same result!! I would love to use a more mature solution than mine if already available

Nima wrote:

Adrian,

do you care to tell me how you do your hotswap in eclipse. I'm currently using ant's deploy and undeploy tasks.

Thanks,
Nima

I simply start tomcat in debug mode with the Eclipse WTP plugin server connector and the web application which I've deployed with an ant task (just like you have) is updated when I modify any classes and save them. Of course there are limitations to the kind of modifications you can do, as outlined in the link I mentioned above. The limitations seem to be the same as with Agent Smith, that's why I was asking what was the difference.

I'm really astonished by my ignorance. It just works!
It is really disappointing... I should then consider Smith a closed project :)

Well, surely this one works better than Smith.

Thank you Adrians

bbakerman's picture

I can also recommend Jetty with the Jetty debug plugin as it does not require a WAR file to be deployed.

You simply point it to your Eclipse project.

Hotswap works for most changes as well.

l.garulli's picture

bbakerman wrote:

I can also recommend Jetty with the Jetty debug plugin as it does not require a WAR file to be deployed.

You simply point it to your Eclipse project.

Hotswap works for most changes as well.

Yes,
I'm using Jetty for debugging. Hot changes works always but when you work on annotations, create/remove atributes/methods or change the method signature.

What I need is the same mechanism for production environment without invoking the JVM in debug optimizing performance!

Federico, does Agent Smith could fit well in this sceario?

Yes it can do.

Your comment makes me think that probably Smith can be used in a different environment than the original one. And probably the license should be changed to asl.

Oh and btw, Smith 0.7 has been released.
Mayor addition is jar monitoring: if a jar file changes, Smith will redefine the changed class files it contains.

Check the bottom of the project page because the parameters syntax has changed (and if you are using java6, also check the javadoc of the SmithServlet)
The java5 syntax now looks like
-javaagent:${TOMCAT}/common/lib/smith-0.7.jar=classes=${MY_WEBAPP}/WEB-INF/classes,jars=${MY_WEBAPP}/WEB-INF/lib,period=1000

jar monitoring is optional (if the parameter is missing, it will be disabled)

bye

0.8 released

from the changelog
0.8
License change: switched to ASL2.0
Header update and some javadoc added

quicky: Smith has just graduated from the javatools incubator and is now part of the javatools project.
https://agentsmith.dev.java.net/

smith 0.9.1 released
Slight speed up and slight API change (private to protected: nothing you should care about)

For the record, AgentSmith 1.0 has just been released. More info at https://agentsmith.dev.java.net/