Monday, May 21, 2007

JRuby on Rails - Agility for the Enterprise (TS-9370 at JavaOne 2007)

These are the notes I took on Charles and Toms talk at the 2007 JavaOne conference. The slides are also available and I'll link to the presentation when its up too, though for now, you have to put up with this...

"See how the other 8% lives"
Ruby is gaining in popularity and developer mindshare. This is largely due to the killer app that is Ruby on Rails (or "RoR"). JRuby is a Java implementation of the Ruby Interpreter (commonly referred to as "Matz's Ruby Interpreter" or MRI after its creator). Because Rails is written in Ruby, it is possible to run Rails apps on JRuby. Charles Nutter and Thomas Enebo have come to talk about it.

Charles started off life as a Java EE architect. Tom was a web app architect. Now they are both working on JRuby after having been hired by Sun. The aim is for it to become a first class language citizen on the JVM.

"What is Ruby?"
  • It's a dynamically typed language
  • It's fully object oriented
  • It is designed to be simple, productive and fun to use
  • It is interpreted (rather than compiled)
  • The original interpreter, MRI, is written in C (and is sometimes referred to as "CRuby" by the JRuby crowd
  • The implementation (which is open source) is the only specification (though this may change)
  • It was created to be "more powerful than Perl and more OO than Python"
Pure OO
Example:
7.class => Fixnum
There is also sweet syntactic sugar for literals:
  • For regex
  • Literal arrays on 1 line
  • Associative arrays / hashes
  • Strings: "foo"
Dynamically Typed
Ruby doesn't care about an Objects type, only about the operations which it can perform. Consequently it throws "no method error" runtime exceptions.

Modules
Ruby only has single inheritance. However it also includes Modules (also called "Mixins") which you can include in your classes to provide additional functionality. They are a mix between Java's Interfaces and Abstract Classes.

Blocks
Blocks (which are also referred to as "closures" in other languages) are methods that you can pass around your code and invoke when you want to. This invokation is on the end of the method call and means that you can remove repetitive code and defer things such as the logic of iteration to collections themselves, allowing them to call out to the block for the processing logic at each step.

They also allow you to internalise transaction code. E.g.
File.open(filename) { | file | line = file.gets }
In this example the file is autoamtically closed. You need not worry about this as a programmer.

It is for these reasons that closures are under consideration for inclusion in the Java language.

Meta Programming
In Ruby Classes and Modules are always open. This means that you can dynamically include them at any point. You can also provide dispatch methods such as method_missing which intercepts and safely handles calls to non existent methods. There is an equivalent const_missing variable.

"What is JRuby?"
The JRuby project was started in 2002 and is a Pure Java, open source implementation of MRI. It is aiming for full compatability with the current Ruby (v. 1.8). This compatability is the primary focus, over performance.

It integrates with Java via the Bean Scripting Framework, JSR-223 and Spring. It is supported by a growing set of external projects such as Goldspike and ActiveRecord-JDBC.

"Why JRuby over Ruby?"
  • Soon it will be faster (the team are working on a compiler so you can run your Ruby applications as JVM bytecode)
  • Scales better (it uses native threads whereas CRuby uses green threads)
  • Native unicode support (CRuby is working on this now, JRuby leverages Java)
  • Integration with the Java libraries (include "java")
  • Easier path into the enterprise - how? See below...
"Why JRuby over Java?"
  • Language features available now (blocks, modules, metaprogramming)
  • Ruby applications / libraries (Rails, RSpec, Rake, Raven, etc)
Ruby on Rails
Rails is a full stack MVC framework written in Ruby which is open sourced under the MIT licence. It was released in 2004 and was written by David Heinemeier Hansson. RoR books already outsell those on Perl. It has a single threaded, share-nothing architecture and ships with MacOS X Leopard.

Rails Precepts
  • Convention over configuration - Based on the philosophy of "why punish the common cases?" this encourges standard practices by making them as simple as possible. In many cases, there is nothing for the developer to do at all. This makes everything simpler and smaller
  • Don't Repeat Yourself ("DRY") - Everything in RoR is written with the minimum of repetition. Repeated code is hard to adapt, maintain and less agile
  • Agility - RoR apps run from the forst command you type. There is no configuration of your environment. This allows for true iterative development from the very beginning. You can show what you do and see your changes live
"Why use Rails?"
  • Greatly Simplified - There is a lot less code in a Rails app than a comparable Java Web App
  • Instant Applications
  • Growing and excited community
  • Small applications are trivial to create
  • Excellent language
"Why use JRuby on Rails?"
  • You can deploy to Java Application Servers as a self contained WAR (using Goldspike)
  • These java web containers environments are already pervasive - it is easier for an enterprise to switch their MVC framework than a whole deployment architecture. Therefore the barrier to entry is reduced
  • Broader, more scalable database support (with ActiveRecord-JDBC)
  • Integration with Java libraries and legacy services - leverage your existing APIs and services
Rails Project Layout
Each Rails project has the same layout (Convention over Configuration at work).
myapp
app
controller - the "C" of MVC
helpers - DRY extracted code from the other app elements
model - the "M" of MVC
views - the "V" of MVC
config - config files
db - db config and migrations files
lib - libraries
public - static html content
test - unit tests
You put your code in the controllers and views. There is no need to wire everything up unlike in Struts or JSF.

Rails Tools - CLI
# rails myapp - Creates a rails app with the above structure called "myapp"
# ruby script/generate - Generates the MVC, migrations and scaffolding etc for your rails app
Components
  • ActiveRecord - "ORM on steroids"
  • ActivePack - View/Controller
  • ActiveWebServer
  • ActiveMail - Mail support
  • ActiveSupport
The Demo
NOTE: What follows are my notes from the demo of pertinent points made. No attempt is made to convey the content of the demo as a whole)
  • We are running in Rails development mode
  • "Migration" - This is a DB agnostic way to specify a DDL in Ruby. They are versioned and allow for a replayable record of DB schema changes (hence "migration"). This allows a developer to save these to VCS and in the future set up the DB to a specific schema version
  • Rake is like make/ant but written in Ruby. It's not just for Ruby
  • # rake db:migrate - generate a schema on your DB for the migration chosen
  • Scaffolding - allows you to be up and running quickly. It generates the simple MVC components for an entity
  • Deployment to a Java Application Server (e.g. Glassfish)
    • Use goldspike to create a WAR file automatically which contains my application and all the libaries (Ruby and Java) which I need to run my Rails app.
    • war.rb is the config file
    • Library dependencies are drawn from maven
    • # RAILSENV=PRODUCTION rake db : migration - Change to production mode
    • # rake war : standalone : create - WAR up your app. This is all you need to deploy
"Why Run on Glassfish?"
  • Multithreading - when you run Rails in an application server extra JRuby servers spin up as you need them. In CRuby you need a "pack" of single threaded Mongrel web servers.
  • Pool DB connections (there is no DB connection pooling on CRuby Rails)
  • Access any JNDI API resource from your Rails app
  • Access any JPA datasource
  • Therefore this is a new way to do the front end of your Java EE application
Futures
  • Rubify the JEE platform APIs
  • Port the Ruby native libraries (some Ruby libraries are partially or wholly written in C and cannot be used with JRuby)
  • Mingle from Thoughtworks - Software tool to manage agile IT projects written totally in JRuby
  • ActiveJPA and ActiveHibernate

2 comments:

Gail said...

I am enjoying your JavaOne writeups. I attended most of these talks, and while I took notes, I think reading your posts are great.

Thanks.

http://www.gailanderson.org/

Andrew Law said...

Thanks Gail. I'm glad it's worth my time. I've loads more but just need the time to get them typed up. I really enjoyed it.