<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codemunchies &#187; maven</title>
	<atom:link href="http://codemunchies.com/tag/maven/feed/" rel="self" type="application/rss+xml" />
	<link>http://codemunchies.com</link>
	<description>Satisfy your cravings</description>
	<lastBuildDate>Mon, 19 Sep 2011 19:32:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Reduce development time with JRebel</title>
		<link>http://codemunchies.com/2010/02/reduce-development-time-with-jrebel/</link>
		<comments>http://codemunchies.com/2010/02/reduce-development-time-with-jrebel/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 08:58:41 +0000</pubDate>
		<dc:creator>Jaran Nilsen</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[jrebel]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[tomcat]]></category>

		<guid isPermaLink="false">http://codemunchies.com/?p=289</guid>
		<description><![CDATA[Let the log output from a container restart fly by for good.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcodemunchies.com%2F2010%2F02%2Freduce-development-time-with-jrebel%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcodemunchies.com%2F2010%2F02%2Freduce-development-time-with-jrebel%2F&amp;source=codemunchies&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p style="text-align: justify;">I first became aware of <a href="http://www.zeroturnaround.com/jrebel/">JRebel</a> about a year ago and became addicted after a lightning talk by <a href="http://www.ekabanov.net/">Jevgeni Kabanov</a> at <a href="http://www.javazone.no">JavaZone 09</a> and the introduction of the tool into my Java development toolbox a couple of days later.</p>
<p style="text-align: justify;">I&#8217;m quite sure every developer who has spent a significant amount of time developing web applications in various application containers will nod their head when I say that a great deal of time is wasted watching the log output from an application container restart fly by.</p>
<p style="text-align: justify;">For me, JRebel showed me a problem area of my application development which I really didn&#8217;t know existed. I had simply accepted it as a part of the development cycle. In this article we will look at how JRebel reduces the need for container restarts in a small web application project. We&#8217;ll perform some changes to our code and see how testing in Tomcat with and without JRebel behaves.</p>
<div id="attachment_297" class="wp-caption alignright" style="width: 190px"><a href="http://www.flickr.com/photos/giovannijl-s_photohut/419945378/"><img class="size-full wp-image-297 " title="Speed up" src="http://codemunchies.com/wp-content/uploads/2009/11/419945378_4ead41a76d_m.jpg" alt="Speed up" width="180" height="240" /></a><p class="wp-caption-text">http://www.flickr.com/photos/giovannijl-s_photohut/ / CC BY-SA 2.0</p></div>
<h3>What&#8217;s JRebel?</h3>
<p style="text-align: justify;">JRebel is a  JVM agent which instantly reloads your code upon change without having to restart the container or redeploy the application[<a href="#1">1</a>]. It integrates into your JVM as a classloader extension and enables reloading of classes without having to create a new classloader which is often the case when redeploying or restarting application containers.</p>
<p style="text-align: justify;">One of the major differences from other hot swap tools is the support for method signature changes and configuration changes for frameworks such as <a href="http://www.springsource.org">Spring</a> and <a href="http://code.google.com/p/google-guice/">Guice</a>. This means you can perform major refactoring of your classes without having to restart or redeploy.</p>
<p style="text-align: justify;">Lets go through the steps to get it all up and running &#8211; so you can see for yourself!</p>
<h3>JRebel with Tomcat and Eclipse</h3>
<p style="text-align: justify;">In this article we will use JRebel with <a href="http://eclipse.org/">Eclipse Galileo</a> and <a href="http://tomcat.apache.org">Tomcat 6.0</a>. I will not be guiding you through the installation and setup of JRebel, as there are excellent, easy to understand guides for this over on the JRebel home pages. We will also be using Spring Framework, to show some of the benefits of JRebel&#8217;s Spring application context support.</p>
<h3 style="text-align: justify;">Our example project</h3>
<p>Our example project in this article will be a simple web application created using the <a href="http://maven.apache.org/plugins/maven-archetype-plugin/examples/webapp.html">Maven webapp archetype</a>. We will create a simple controller and view using <a href="http://static.springsource.org/spring/docs/2.5.x/reference/mvc.html">Spring MVC</a>, and perform various changes to the controller &#8211; to see how a Tomcat instance with and without JRebel will behave and cope with these changes.</p>
<p>To simplify the tutorial I&#8217;ve set up the basic demo project for you, which is ready to be imported into Eclipse and used as the basis for the rest of this article. <a href="http://codemunchies.com/wp-content/uploads/2010/02/jrebel-demo.zip">Download the project package here</a>.</p>
<p>Here are the different modifications we will perform and compare the behaviour of Tomcat with and without JRebel:</p>
<ol>
<li>Modify the request handler method, alter the content of our <em>model</em>.</li>
<li>Modify the request handler method by refactoring.</li>
<li>Method signature modification.</li>
<li>Dependency injection using Spring&#8217;s autowire feature.</li>
</ol>
<p>These are three rather small modifications to our project, but they are sufficient to show the power of JRebel in many web application developments.</p>
<h3>Let&#8217;s get down to business!</h3>
<p>I&#8217;ll assume you have gotten our example project imported into Eclipse and performed a test deployment on the Tomcat server.</p>
<h4>1. Modify request handler method, alter the content of our model.</h4>
<p>First we do a very simple modification and alter the message that we are putting into the model:</p>
<pre class="brush: java; toolbar: false">model.put("message", "Hello, Codemunchies fans!");</pre>
<p>This change is easily handled by both Tomcat instances and we immediately see the change in our browser, as well as JRebel telling us that the IndexController has been reloaded to reflect the changes.</p>
<pre>JRebel: Reloading class
    'com.codemunchies.jrebel.IndexController'.
JRebel-Spring: Reconfiguring bean 'indexController'
    [com.codemunchies.jrebel.IndexController]</pre>
<h4>2. Modify request handler method by refactoring</h4>
<p>Our request handler method currently puts a simple message into the model, which is then rendered in the JSP. Our second modification will be to extract this into a separate method and change the message.</p>
<p>Our code now looks like this:</p>
<pre class="brush: java; toolbar: false">@RequestMapping("/index.html")
public String handleRequest(ModelMap model) {

	populateModel(model);
	return "index";
}

private void populateModel(ModelMap model) {
	model.put("message", "Hello, JRebel tester!");
}</pre>
<p>Upon saving the IndexController.java file we immediately get a hot swap error from Eclipse telling us certain code changes cannot be swapped.</p>
<p><a href="http://codemunchies.com/wp-content/uploads/2010/02/cm-hotswap-err1.png"><img class="aligncenter size-full wp-image-328" title="cm-hotswap-err1" src="http://codemunchies.com/wp-content/uploads/2010/02/cm-hotswap-err1.png" alt="Hotswap error" width="450" height="239" /></a></p>
<p>In our JRebel instance of Tomcat, you will currently not see any log output indicating that a change to the .class file has been made. Don&#8217;t worry, this is perfectly normal, as JRebel will not act on your changes until resources are requested. We do a reload in our browser and watch JRebel kick into action &#8211; lightning fast:</p>
<pre style="padding-left: 30px;">JRebel: Reloading class
    'com.codemunchies.jrebel.IndexController'.
JRebel-Spring: Reconfiguring bean 'indexController'
    [com.codemunchies.jrebel.IndexController]</pre>
<p><a href="http://codemunchies.com/wp-content/uploads/2010/02/cm-jrebel-change.png"><img class="aligncenter size-full wp-image-329" title="cm-jrebel-change" src="http://codemunchies.com/wp-content/uploads/2010/02/cm-jrebel-change.png" alt="JRebel test" width="450" height="264" /></a></p>
<p>No context reloading is performed by Tomcat and the change we made to our class is live in a few milliseconds. In order for our non-rebellious Tomcat instance to reflect the changes a restart was necessary, and the server spent 2.2 seconds restarting with my test setup. <em>It shall be noted that you can achieve faster reload times by only reloading the module you&#8217;re working on through the Tomcat Manager application. </em></p>
<p>These reload times may not seem like much, but consider a much heavier application, with a dozen controllers, a couple of data sources and Hibernate session factories&#8230; You&#8217;re quickly spending a significant amount of time waiting for the context reloading!</p>
<h4>3. Method signature modification</h4>
<p>The limitations of the hot swapping feature provided by the <a href="http://java.sun.com/javase/technologies/core/toolsapis/jpda/">JDPA</a> are clear. Many developers may find it sufficient for their needs, but the rest of these examples will show what you gain by using JRebel.</p>
<p>In this third example we&#8217;re going to change the signature of our populateModel method to take a second parameter which will be the message we want to put into our model. Our code should now look like this:</p>
<pre class="brush: java; toolbar: false">@RequestMapping("/index.html")
public String handleRequest(ModelMap model) {

	populateModel(model, "Message after method signature modification.");
	return "index";
}

private void populateModel(ModelMap model, String message) {
	model.put("message", message);
}</pre>
<p>Again, JRebel kicks in upon a refresh in our browser and the changes are immediately visible:</p>
<p><a href="http://codemunchies.com/wp-content/uploads/2010/02/cm-jrebel-signaturechange.png"><img class="aligncenter size-full wp-image-330" title="cm-jrebel-signaturechange" src="http://codemunchies.com/wp-content/uploads/2010/02/cm-jrebel-signaturechange.png" alt="JRebel signature change" width="450" height="264" /></a></p>
<h4>4.  Dependency injection using Spring&#8217;s autowire feature</h4>
<p>Our last example will be a little more extensive &#8211; this is where the fun starts. We will introduce a new service class which we will inject into our IndexController using Spring&#8217;s autowire feature, and finally use this service to obtain the data we want to put into our model.</p>
<p>First we create our service class, let&#8217;s call the interface DataService and our implementation DataServiceImpl. The example code is shown below.</p>
<pre class="brush: java; toolbar: false">public interface DataService {
	String getData();
}</pre>
<pre class="brush: java; toolbar: false">import org.springframework.stereotype.Service;

@Service
public class DataServiceImpl implements DataService {

	@Override
	public String getData() {
		return "Hello! I'm a rebel in disguise!";
	}
}</pre>
<p>We then inject this service into our IndexController and modify our populateModel method to obtain the model data from the service, as shown below:</p>
<pre class="brush: java; toolbar: false">@Controller
public class IndexController {
	@Autowired
	private DataService dataService;

	@RequestMapping("/index.html")
	public String handleRequest(ModelMap model) {
		populateModel(model);
		return "index";
	}

	private void populateModel(ModelMap model) {
		model.put("message", dataService.getData());
	}
}</pre>
<p>Remember to touch, or otherwise mark the context file(s) where your beans are defined as edited, or JRebel will not reload your context. This is especially important when you are using the component scan feature of Spring &#8211; it&#8217;s easy to forget when you&#8217;re never editing those XML files.</p>
<p>Upon refresh, JRebel immediately reloads the Spring context and our bean:</p>
<pre>INFO: Loading XML bean definitions from file
    [.../jrebel-demo/src/main/webapp/WEB-INF/jd-servlet.xml]
JavaRebel-Spring: Reloading Spring bean definitions in
    '.../jrebel-demo/src/main/webapp/WEB-INF/jd-servlet.xml'.
INFO: Pre-instantiating singletons in ...
JRebel: Reloading class 'com.codemunchies.jrebel.IndexController'.
JRebel-Spring: Reconfiguring bean 'indexController'
    [com.codemunchies.jrebel.IndexController]</pre>
<p>And our browser shows us that the changes we made are in effect:</p>
<p><a href="http://codemunchies.com/wp-content/uploads/2010/02/cm-jrebel-depinj.png"><img class="aligncenter size-full wp-image-331" title="cm-jrebel-depinj" src="http://codemunchies.com/wp-content/uploads/2010/02/cm-jrebel-depinj.png" alt="Dependency Injection" width="450" height="264" /></a></p>
<p>Again, JRebel has saved us from a tiresome Tomcat reload and if we multiply these reload times with the number of such changes we normally do during an average day of development, we quickly see that there&#8217;s time to save.</p>
<p>JRebel is not perfect and you will need a container restart now and then, but it is still a huge time and frustration saver and is well worth the license fee. At the beginning of February JRebel displayed the following statistics for my last 30 days of development:</p>
<blockquote><p>Over the last 30 days JRebel prevented the need for at least 215 redeploys/restarts. JRebel saved you between 5.7 and 11.6 hours.</p></blockquote>
<p><em>Deployment and container restart times may vary depending on the container you use and your development pattern. This article is by no means attempting to benchmark Tomcat or JRebel, but is showing how you in many cases may save valuable development time.</em></p>
<h3>References</h3>
<p>[<a name="1">1</a>] http://www.zeroturnaround.com/jrebel/faq/#What_is_JRebel</p>
<div xmlns:cc="http://creativecommons.org/ns#" about="http://www.flickr.com/photos/snapr/484776493/">Thanks to snapr at Flickr for use of front page image <a rel="cc:attributionURL" href="http://www.flickr.com/photos/snapr/">http://www.flickr.com/photos/snapr/</a> / <a rel="license" href="http://creativecommons.org/licenses/by/2.0/">CC BY 2.0</a></div>
]]></content:encoded>
			<wfw:commentRss>http://codemunchies.com/2010/02/reduce-development-time-with-jrebel/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

