Tuesday, January 11, 2011

Eclipse or Netbeans HotSwap for the Agile Java Developer

HotSwap technology has been there for a while yet I still find many developers not taking advantage of it.

Let us see the typical non agile Java development:
  1. Change (Code is changed. Building can be configured to be automated)
  2. Deploy (Can be automated but at least the application will reload, sometimes even the server must be restarted)
  3. Setup (Deal with the specific test case probably navigating some application states before getting to the point)
  4. Test (the change, the feature, the bug fix etc)

I do not want to enter the discussion between compiled vs interpreted or static vs. dynamic and yet I want to be as fast as possible when fixing a bug, changing some logic or adding a quick feature. Java HotSwap technology (backed by JPDA) addresses the issue allowing you in many cases to just:
  1. Change
  2. Test

There are limitations (for example you cannot change a method signature) but there is work in progress to make HotSwap better. UPDATE: In fact as of June 2014 there is a free full solution for this problem which I have tested for Linux and jdk 1.7 but for sure supported in other OS and JVM versions as well: DCEVM + HotswapAgent

In any case the amount of time HotSwap saves can be huge especially if you get smart at debugging (thing that you will do in order to avoid deploy and setup phases).

I have used HotSwap now for years in Eclipse. My current team uses Netbeans and I was tempted to just do the same just to realize that was not straightforward at least on version 6.9.1. However not impossible.

While Eclipse will accept change and test (fix and continue) with just "CTRL+S" Netbeans needs "CTRL+S | Menu -> Debug -> Apply Code changes". However following the steps below you can get "fix and continue" or "change and test" working in Netbeans as well:
  1. From Project properties select "build | compile | Compile on Save | For both ... "
  2. From Netbeans preferences select "Miscelaneous | Java Debugger | General | Apply code changes after save"

It is worth to be said JRebel rocks. Not an open source project but not that expensive either. As far as I am concerned I tend to address those parts that cause 90% of the problem first. Following that logic I have found myself spending way more time troubleshooting than actually developing. So definitely if you are performing big changes in your code, you are probably better using junit/selenium for quick initial development in which case the impact of not having the luxury to change/add method signatures, class fields and so on would not be that big after all. But if you are serious about Plain Java Web Development then try JRebel or if you are low on budget and not working on an open source project I definitely recommend (again) DCEVM + HotSwapAagent.

Of course I am assuming you are connecting to the JVM debugging port. For example I commonly have the below in my tomcat setenv.sh (see differences for pre and post JDK 5):
#debugger < JDK 5
#JAVA_OPTS="$JAVA_OPTS -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"
#debugger >= JDK 5
JAVA_OPTS="$JAVA_OPTS -agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"

Then from Eclipse or Netbeans I connect to the remote port using JPDA with SocketAttach connector. For Netbeans this is done from "Menu | Debug | Attach Debugger"

Finally for those loving dynamic programming take a look at the cool features from the Da Vinci Machine coming up as part of the JDK 7 release.

No comments: