JRebel Struts2 Plugin
JRebel is a Java plugin that enables dynamic class reloading for the Java platform. In context of web app development, this means that after editing his code, the developer does not have to redeploy his application to see the changes. As redeploy times range from below 1 minute to up to 5 minutes or even more, this is a significant improvement in the development cycle.
When frameworks like Struts2 are involved, simply reloading the Java bytecode of user-defined classes unfortunately falls short of the mark. This is due to some important aspects of the web application being loaded only once on the initial deployment of the application. In such case, no matter if the configuration is defined in an external XML configuration file or as Java annotations inside reloaded Java classes, the framework’s running configuration does not get updated as necessary when the corresponding resources are editted and reloaded. This is where the JRebel integration plugins step in, namely the Struts2 plugin in our case.
What the JRebel Struts2 plugin basically does is the detection of situations when the Struts2 running configuration could have been changed by the developer, and should thus be reloaded. The “running configuration” here is set of action mappings which map URLs to certain Struts2 action classes. As Struts2 allows the user to specify mappings either in external XML configuration files or by writing Java annotations straight into the action classes, the plugin has to both monitor the XML configuration files and listen for class reload events from the JRebel. If a possibility that an action mapping configuration has change is detected, the plugin triggers the full configuration reload, thus initializing up-to-date action mappings without redeploying the application.
Setting up the plugin is simple. It is disabled by default — to enable it, add -Drebel.struts2-plugin=true to Java options in your container startup script (the same place where you were instructed to add -noverify -javaagent:/path/to/jrebel.jar while setting up JRebel, which you of course also have to do before the using the plugin.. see JRebel installation instruction). Restart your container for the plugin to load, and deploy your Struts2 application.
If you are deploying your application in packaged mode (by copying a .war into app-server’s webapps directory), you should also create a rebel.xml configuration file and make sure it is available in the classpath (i.e. gets included to the WEB-INF/classes directory of your .war). Refer to the Application Configuration section of the Configuration page for details. In the trivial case, something like the following will suffice:
<?xml version="1.0" encoding="UTF-8"?> <application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zeroturnaround.com" xsi:schemaLocation="http://www.zeroturnaround.com/alderaan/rebel-2_0.xsd"> <classpath> <dir name="/home/john/myStrutsApp/target/classes" /> </classpath> </application>
This will mount /home/john/myStrutsApp/target/classes to the virtual classpath, making JRebel automatically reload all changes to classes that get compiled into that directory. As Struts2 also places it’s configuration XMLs directly into the classpath, so this will also make the changed versions of XML configuration files available to the container. The only thing that you have to worry about is setting up your IDE to automatically copy the XML file to the target/classes directory each time it gets updated in it’s original location (which is probably different), so that the development process will truly be a matter of changing your files and hitting F5 in the browser.
A simple test to verify that the Struts2 plugin is properly working would be to comment out some action from the Struts2 XML configuration file, save the file, and query the corresponding URL with your browser. If you now get a 404 then the plugin is working properly (presuming that before commenting you didn’t get a 404 from that URL, of course). Double-check by uncommenting again and hitting F5. The page re-appeared, right? Ok, then your JRebel Struts2 plugin is set up and working properly :)


