Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.
See all our reports

Get Groovy with JRebel and Grails

grails_meme
Developing with Grails just got so much faster! Let’s start with a quick background on Grails, then write some code with JRebel.
Grails is a really popular framework for rapidly writing applications on the JVM. With Grails, you can write quick prototypes all the way to production-quality apps quickly and with minimal boilerplate code thanks to Grails’s code generation capabilities and convention-over-configuration nature.
DOWNLOAD JREBEL NOW

Grails was created as a JVM-based response to the Ruby on Rails rapid prototype movement and to enable developers to write applications quickly without having to worry about standard configuration. Grails leverages existing, proven frameworks like Hibernate and Spring.


One thing that developers using Grails especially appreciate is productivity. Restarting a Grails application can take a while, and not having to restart that app to make code changes would be great, maybe the holy grail of productivity. Spring-loaded is included in the Grails runtime, but it is unreliable and often causes more trouble than it’s worth. JRebel comes to save the day! With JRebel, you can make changes to your Grails applications, even scaffolding new domains and controllers, and JRebel will reliably reload the changed code into the JVM without stopping the application or losing state.

“Grails comes with its own reloading, but that has proven to be just flaky enough where I don’t trust it. Using JRebel, I find the opposite: the application reloads reliably and predictably. That means I can focus on the product, not the tools.” – Andres Jaan Tack (TransferWise)

FOLLOW ALONG WITH JREBEL NOW

Let’s see this in action. I’ve taken the Grails Pet Clinic application from github and enabled JRebel in my Eclipse. When I run my application, I see the JRebel banner and some relevant messages in the console output:

JRebel-Grails2: Development mode is forced for all Grails 2 applications

and the application is now running at http://localhost:8080/petclinic

Let’s start changing some code and see JRebel reload the code changes instantly. First, let’s change the Owner form validator, we’ll need to change the constraints on the Owner class in the Owner.groovy file under the domain folder. Let’s make address optional, in the constraints block, change: address blank: false to address nullable: true
Before the change, the address is clearly a required field:
owner_before
After the change, the address is now optional:
owner_after
Thanks JRebel!

In the console, we can see Grails compiled the changed class and then JRebel reloaded the code in the JVM instantly:

| Compiling 1 source files.
2015-02-25 13:20:40 JRebel: Reloading class 'org.grails.samples.Owner'.
2015-02-25 13:20:40 JRebel: Reloading class 'org.grails.samples.Owner$__clinit__closure1'.
2015-02-25 13:20:40 JRebel: Reinitialized class 'org.grails.samples.Owner'.
2015-02-25 13:20:44 JRebel: Reloading class 'org.grails.samples.PetController'.
2015-02-25 13:20:44 JRebel: Reloading class 'org.grails.samples.OwnerController'.
2015-02-25 13:20:44 JRebel: Reloading class 'org.grails.samples.Pet'.
2015-02-25 13:20:44 JRebel: Reinitialized class 'org.grails.samples.PetController'.
2015-02-25 13:20:44 JRebel: Reinitialized class 'org.grails.samples.OwnerController'.
2015-02-25 13:20:44 JRebel: Reinitialized class 'org.grails.samples.Pet'.

Now let’s add a new domain object and controller with scaffolding by executing the create-domain-class and create-controller commands in the Grails console:

create-domain-class org.grails.samples.BlogClass
create-controller org.grails.samples.BlogClass

This generates the normal Groovy files, a fairly empty controller and empty domain class. Let’s add a field to our new class and then set our controller to scaffold the domain class. Meanwhile, our application hasn’t stopped running.

Let’s put some code into our BlogClass domain now:

def scaffold = BlogClass

As soon as we save the files, Grails compiles the code, and JRebel picks up the changes and reloads the code into the JVM. We can verify the code works by going to http://localhost:8080/petclinic/BlogClass in our browser and it works!
workingmapping

As you can see, JRebel let us make a fairly significant change to our application running grails while the app remained up! No redeploy, and we were able to reload the code changes instantly. Now, that’s productive! Try JRebel for yourself in your app and see how you can see over one full work-month per year!


SET UP JREBEL IN THE NEXT 10 MIN!

 

If you have any questions you can find me on twitter at @akoblentz or leave a comment below!

  • Grails reloading mechanism might not be bullet proof, but it still is heaps better than Java HotSwap and good enough to avoid spending $365 (more than three times what I pay for Intellij licence), which as an independent developer I find it difficult to justify or even afford. If it had its former pricing I would not hesitate to purchase it. As it is, only big companies can affort it IMO.