Dynamic Class Reloading

One of the things you want to avoid when doing java web development in general, is restarting your web container too often. As many real-life applications have a significant start-up time, this overhead incurs a very significant penalty in terms of developer productivity.

In the section below I will explain how we use jZeno during the development cycle, and show a technique to avoid this penalty.

jZeno comes with a class loading technique that allows you to instantly load new versions of your compiled classes into the running application. Normally you will be able to totally change the structure of your screens, components, business facades, etc... and then hit the RELOAD button on any jZeno screen to instantly reload these new class files in your servlet container, without even needing to re-login to your application, wait for hibernate to load it's mappings, the application server to restart, or anything else..


A jZeno project will have 2 source folders that split your application into a non-reloadable part (the src folder) and a reloadable part (the rsrc folder). In general you want to put just about anything into the reloadable part, unless you are prevented by technical reasons.

You can take a look at the way the jZeno template project is configured to see how we have set up both source folders. You'll notice that the src folder will compile to the web application's WEB-INF/classes folder, and the rsrc folder is set up to compile to the WEB-INF/rclasses output folder.

A typical application will only have it's domain model classes (mapped to a database with hibernate) and a small number of support classes in it's src (non-reloadable) folder.

The benefit of allmost never having to restart your application server comes with a minor down-side as well. It is not possible to refer from a class in the src folder to any of the classes in your rsrc folder (the other direction is no problem). Given that the src folder will contain your domain model, it's actually good practise to keep that layer from creating dependecies towards the business or presentation layer (which will be in the rsrc folder)


Hot Code Replacing and Restarting

Whenever you attempt to update a class that's in the src folder you will need to restart your application..., unless you only want to change to the implementation of one or more methods. If that is the case you can still get away with not restarting the application server by using 'hot code replacing'. To do that start a debug session to your application (see Debugging your application), apply the changes you want, and save your file. Now eclipse will not only compile your class, but will attempt to replace the bytecode of your changed method(s) right inside of the running VM ! Unless you get an error from eclipse things should be ok.

Changing the signature of a method, adding methods/instance variables, removing methods/instance variables are not possible this way and will require a full restart of your application server. When you have an existing debug session and you attempt any of these kinds of changes the IDE will complain :


Libraries

This tells you that you cannot perform the hot-code replacement and need a full server restart... :-(


Some More Tips