Eclipse WTP Tomcat hot deploy

One of the reasons why JavaEE does not have many succesful projects is that its development time is usually longer than other competitors’ time (PHP, Ruby, Python, ASP.NET, …) . Ok, we all know the burden of overly complicated design and over-engineered implementations from Sun, so let’s skip them and go to the one to be resolved: the Hot-Deploy time. In other words, the time for developers to see the web-page refreshed with their changes applied.

The word “hot deploy” used above may not reflect its original meaning, not only because my poor English, but also because (Vietnamese) developers have different concepts of it: “automatic hot swap”, “automatic redeploy webapp”, “automatic restart container” .

Hot deploy concepts

This is how I understand the differences:

1/ Hot deploy container: automatic restart whole servlet container (Tomcat)
2/ Hot deploy webapp: automatic reload context root and all classes of respective webapp.
3/ Hot deploy classes: hot-swap only the re-compiled classes (runtime)

It is obvious that #1 is slower than #2 and #2 is slower than #3: Hot-swap or hot-code replacing .

Ok, now what? Let’s try to reduce the hot-deploy time in Java developement in the well-known combination: Eclipse + WTP plugins + Tomcat. Assumed that you already have an Eclipse with WTP/WST plugins installed (e.g. Eclipse JavaEE version).

Create a New Server WTP for web apps

From the New menu, select Other… -> Server -> Server. For your server type (probably “Apache Tomcat 6”), specify the path to your Tomcat installation directory, e.g. “/opt/apache-tomcat-6.0.18” or “D:\USR\apache-tomcat-6.0.18” . Add your web project as a ‘Resource’ to this server (you may modify the context root first).

Adjust the server’s settings

Double click on the Server in your Server view (its name is something like “Tomcat v6.0 Server at localhost-config”), it will display the “Overview” tab:

  • Server location: Use Tomcat installation (actually, either “workspace metadata” or “custom location” can work as well, but let’s use the most simple for beginners’ sake)
  • Port: modify the HTTP port from 8080 to the one you desire. You may also modify the remaining 2 ports respectively to avoid port conflicts between Tomcat instances.
  • Timeout: better increase them 100 or 200 seconds more.
  • Server options: not necessary to check any options.

Enable classes hot-swap

The main settings for hot-deploy here:

  • Publishing: Auto publish when resources changes (so as to hot-deploy text resources like .properties, .html, .jsp, … and packaged resources like .jar, .zip ) , the interval should be small (1 or 0 sec).
  • Switch to “Modules” tab, since you already added the web app to the server, there should be at least a “module” with the specified context root (path URL). Select that module, then click “Edit” button, uncheck the “Auto reloading enabled” checkbox, click OK. Now your module should have “Auto Reload” setting as disabled (equivalence to <Context reloadable=”false” … /> in server.xml/context.xml of Tomcat)
  • Save the changes (at least make sure the two above has been applied already).

Start the server in Debug mode

Once you started the web-app in Debug (right click -> choose “Debug…”), the changes you make to your JSPs or inside Java methods will be instantly hotswapped into your running webapp, therefore reduce the development time (at least the wasted time looking at console when reloading web-app)

Why?

Since Java 1.4.2 , the JPDA supports hot-swap classes on debug mode, by manipulating class loaders at runtime. Eclipse makes use of it via WTP under the name Hot Code Replace . Setting auto-publish helps replacing text files and recompiled jar, but not for classes. By default, Tomcat’s context reloading will reload all classes using its class loaders and therefore does not take advantage of hot-deployed classes.
Note that JPDA is not the best solution for hot code replacing, the proven one here must be either JRebel or DynamicCodeEvolution. Some web frameworks (Tapestry, Stripes, Wicket, Grails, Roo) also has their own classloader handlings to support quick reload. They’re all inspired by some standalone JAR files around which I don’t remember (probably the pioneers for JRebel). And FYI, Tomcat Sysdeo plugin and Jetty can also support HCR , in case you don’t want to use Tomcat WTP.

Some more recommended settings

Running Tomcat in Eclipse (via WTP plugin) is a bit slower than via external command, and running in Debug mode is somehow resource-hogging, which may result in errors like hot-swap failure or OutOfMemoryError . To avoid those issues, you may try some JVM options via Tomcat JRE params: Double-click on your server in the “Servers” view, switch to the “Overview” tab, click on the “Open launch configuration” link, switch to the Arguments tab; there you can add relevant memory settings to the “VM Arguments” section
-client -Djava.awt.headless=true
-Xmx1024m
-Xms256m
-XX:MaxPermSize=1024m
-XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled -XX:+UseConcMarkSweepGC

Limitations

JPDA HCR not applied: to change the signature of a class (add/remove methods or fields) or to add new classes on the fly. Additionally, some method calls (“stack frames”) can’t be modified, including the main method or any method invoked via reflection, that is, by using java.lang.reflect.Method.invoke().
(JRebel and DynamicCodeEvolution can overcome those limitations)

Good luck & have fun :-)

About DucQuoc.wordpress.com

A coder, brother, and player at times...
This entry was posted in Coding, Marketing. Bookmark the permalink.

16 Responses to Eclipse WTP Tomcat hot deploy

  1. Raj says:

    thanks for putting this information up so clearly – it was exactly what I needed!

  2. Pingback: Vietnamese Flash developers | DucQuoc's Blog

  3. JSI says:

    Once more thanks!

  4. Pingback: Eclipse ExtJS jQuery | DucQuoc's Blog

  5. Thanks for the feedback. I’m glad you can find some new or useful information in the post :-) .

  6. Gopal says:

    gr8 thank you ;) exactly what i am looking for

  7. mvg says:

    Hey – Thanks very much. Please capitalize that the server MUST be in Debug, I tried this in the non debug (start) mode and hot swap didn’t work. Any idea why?

  8. @Gopal: you’re welcome

    @mvg: I appreciate your suggestion. But in this article , the line “Start the server in Debug mode” is already posted in bigger font (bold) . And also , I think the reason why the debug mode was mentioned in the section “Why?” ; there was the link to JPDA which may explain better about hot-swap of Java debugging architecture.

  9. Pingback: Logging best practices | DucQuoc's Blog

  10. Pingback: JBoss hot deploy | DucQuoc's Blog

  11. anonymous says:

    Hello! Do you know if they make any plugins to safeguard against
    hackers? I’m kinda paranoid about losing everything I’ve
    worked hard on. Any suggestions?

    • I’m afraid there is no such plugin, since there are many hackers who can do things beyond my knowledge (and even my imagination).
      Whilst we are not so sure about “perfect defense” against hacker, we can save relevant data by using backups, which is pretty easy in these days IMO. You can use some external harddisks (instead of the good old tapes), or some “cloud” storage service (wide variety: from Akamai to GoogleDrive, DropBox, SugarSync, SkyDriver, iCloud, … or Box.net, MediaFire.com, 4Shared, RapidShare, … or even GitHub, GoogleCode, BitBucket, etc) .

  12. 12493 says:

    Solid write up. I’m studying something similar here at Indiana State University. It’s really
    beneficial to learn new stuff from fellow writers
    and collect information from new sources. If it’s okay, I’d greatly appreciate it if I could use a few of the articles on your blog.
    Obviously, I’ll offer a hyperlink to %target_domain% on my blog.

    Kudos for posting.

  13. Hi. I’m wondering if you may be interested in doing a website link swap? I see your blog: https://ducquoc.wordpress.com/2010/11/06/eclipse-wtp-tomcat-hot-deploy/ and my website are based around the same subject. I’d love to swap links or perhaps
    guest author a article for you. Here is my personal e-mail:
    carmenwatson@arcor.de. I highly recommend
    you contact me if you’re even slightly interested. Thank you so much.

  14. tin sao says:

    Thanks in support of sharing such a good thought, post is
    good, thats why i have read it fully

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s