JRuby in the enterprise

83
JRuby in the Enterprise

description

This is from a talk I gave at UberConf 2011 in Denver, CO.

Transcript of JRuby in the enterprise

Page 1: JRuby in the enterprise

JRuby in the Enterprise

Page 2: JRuby in the enterprise

About Me

Advisory Engineer at Constant Contact

Spent several years working for Sun/Javasoft in the distributed computing group

Email: [email protected]

Twitter: @jerrygulla

Github: github.com/jgulla

Page 3: JRuby in the enterprise

What is JRuby?

Just a Ruby interpreter running on the JVM

Ruby 1.8.7 & 1.9.2 compatible.

Page 4: JRuby in the enterprise

Text-To-Join

Distributed app

Written in Ruby as a Rails app

Four rails apps, actually

UI, API, Console, SMS Processor.

Page 5: JRuby in the enterprise
Page 6: JRuby in the enterprise
Page 7: JRuby in the enterprise

JRUBY.ORG

Page 8: JRuby in the enterprise

Why Ruby?

Ruby is…A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.

Page 9: JRuby in the enterprise
Page 10: JRuby in the enterprise

WHY SHOULD YOU SWITCH TO RUBY? IF YOU ARE HAPPY WITH PERL OR PYTHON, YOU DON'T HAVE TO. BUT IF YOU DO FEEL THERE MUST BE A BETTER LANGUAGE, RUBY MAY BE YOUR

LANGUAGE OF CHOICE. LEARNING A NEW LANGUAGE IS HARMLESS. IT GIVES YOU NEW IDEAS AND INSIGHTS. YOU

DON'T HAVE TO SWITCH, JUST LEARN AND TRY IT. YOU MAY FIND YOURSELF COMFORTABLE ENOUGH WITH RUBY TO

DECIDE TO SWITCH TO IT.

-- YUKIHIRO MATSUMOTO (“MATZ”)HTTP://LINUXDEVCENTER.COM/PUB/A/LINUX/2001/11/29/

RUBY.HTML

Page 11: JRuby in the enterprise

Rails 3.x

“Optimized for programmer happiness”.

Page 12: JRuby in the enterprise

Why Not Grails?

Grails is great

Not as big of a break with Java

Wanted a bigger shift to bring new ways of doing things into perspective.

Page 13: JRuby in the enterprise

Why JRuby?

High performance

JVM Highly Tuned

Real threading

Many “pure Ruby” shops using JRuby just for this

Vast array of libraries

No need to abandon Java libraries.

Page 14: JRuby in the enterprise

Why JRuby Continued

Deployment Options

Can deploy as a WAR file

Monitoring Options

JVM tools to monitor all available

Speed of Development.

Page 15: JRuby in the enterprise

Don’t Freak the Ops Guys Out

HTTP://UPLOAD.WIKIMEDIA.ORG/WIKIPEDIA/EN/F/F4/THE_SCREAM.JPG

Page 16: JRuby in the enterprise

Scalabilityhttp://commons.wikimedia.org/wiki/File:Everest-fromKalarPatar.jpg

Page 17: JRuby in the enterprise

Concurrency is a Myth

HTTP://WWW.IGVITA.COM/2008/11/13/CONCURRENCY-IS-A-MYTH-IN-RUBY/

Page 18: JRuby in the enterprise

JRUBY IS, IN FACT, THE ONLY RUBY IMPLEMENTATION THAT WILL ALLOW YOU TO NATIVELY SCALE YOUR RUBY CODE ACROSS MULTIPLE CORES. BY COMPILING RUBY TO BYTECODE AND EXECUTING IT ON THE JVM, RUBY

THREADS ARE MAPPED TO OS THREADS WITHOUT A GIL IN BETWEEN - THAT'S AT LEAST ONE REASON TO LOOK

INTO JRUBY.HTTP://WWW.IGVITA.COM/2008/11/13/CONCURRENCY-IS-A-MYTH-IN-RUBY/

ILYA GRIGORIKHTTP://WWW.IGVITA.COM/ABOUT/

Page 19: JRuby in the enterprise

“Enterprise” JRuby

Page 20: JRuby in the enterprise

“Enterprise” JRuby Cont.

Maven

Liquibase

JDBC

JNDI

Containers

You’re favorite commercial, open-source or home-grown library.

Page 21: JRuby in the enterprise

Let’s Beginhttp://commons.wikimedia.org/wiki/File:Begin_road.JPG

Page 22: JRuby in the enterprise

Set up Ruby

http://commons.wikimedia.org/wiki/File:Panama_Canal_Locks_Construct.jpeg

Page 23: JRuby in the enterprise

Ruby Version Manager

HTTPS://RVM.BEGINRESCUEEND.COM/

Page 24: JRuby in the enterprise

Installing RVM

Via git repo (preferred method)

bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

Other methods at https://rvm.beginrescueend.com/rvm/install/

Page 25: JRuby in the enterprise

Install JRuby (via rvm)rvm install jruby-1.6.3

When it’s done, use it:

rvm use jruby-1.6.3

List of other ruby versions to install:

rvm list known

List versions of ruby you have installed:

rvm list

Page 26: JRuby in the enterprise

JRuby Without RVM

Assume Java, JRuby in your path

jruby -S gem ...

Page 27: JRuby in the enterprise

Install Rails

gem install rails

Can pick a specific version with a “-v” flag.

Page 28: JRuby in the enterprise

Create Your Rails App

Jruby.org hosts a rails template

Sets things up to run under native ruby or JRuby

You can do this yourself, but it’s handier to start this way

rails new uberconf_demo --template=http://jruby.org

Works with native ruby or JRuby.

Page 29: JRuby in the enterprise

Jruby.org Template

Page 30: JRuby in the enterprise
Page 31: JRuby in the enterprise

Demo

Page 32: JRuby in the enterprise

Packaging Your App

http://en.wikipedia.org/wiki/File:Distribution_differences.jpg

Page 33: JRuby in the enterprise

JRuby/Rails/WAR files

In the Java world, WAR files are standard

Fortunately, we have bundler

Gem to package your Rails app as a WAR file

Can even make it “Executable” for stand-alone testing!

http://caldersphere.rubyforge.org/warbler/

Page 34: JRuby in the enterprise

Install it

Easy!

gem install warbler

Page 35: JRuby in the enterprise

Run It

Easy!

warble war

http://en.wikipedia.org/wiki/File:Jeanette_Kwakye_cropped.jpg

Page 36: JRuby in the enterprise

Other Useful Optionswarble compiled # Feature: precompile all Ruby files

warble config # Generate a configuration file to customize your archive

warble executable # Feature: make an executable archive

warble gemjar # Feature: package gem repository inside a war

warble pluginize # Install Warbler tasks in your Rails application

warble version # Display version of Warbler

warble war # Create the project war file

warble war:clean # Remove the project war file

warble war:debug # Dump diagnostic information

Page 37: JRuby in the enterprise

Maven

In the Java world, Maven is still pretty popular

For better or for worse

You can use Maven to invoke warbler to package your app.

Page 38: JRuby in the enterprise
Page 39: JRuby in the enterprise

Database Migrations

http://en.wikipedia.org/wiki/File:Connochaetes_taurinus_-Wildebeest_crossing_river_-East_Africa.jpg

Page 40: JRuby in the enterprise

Database Migrations

Rails uses Rake for ActiveRecord migrations

Nice, but

Hard to scale to multiple developers

Hard to deploy to QA/Production systems without using ruby toolset

Paranoid DBAs can’t see the SQL before running it.

Page 41: JRuby in the enterprise

Enter

liquibase.org

Version control for your database

Other frameworks (Grails) will be adopting it for migrations

Language/Database agnostic.

Page 42: JRuby in the enterprise

Nothing special about using Liquibase with Rails

Use ActiveRecord migrations for initial dev/testing, then migrate to Liquibase

When ready, just take “snapshot” of DB config, then roll out new Liquibase changesets.

Liquibase

Page 43: JRuby in the enterprise

Generate initial changelog:java -jar liquibase.jar --classpath=mysql-connector-java-5.1.14-bin.jar --url=jdbc:mysql://localhost/api_development --driver=com.mysql.jdbc.Driver --username=demo --password=password --changeLogFile=db-changelog.xml generateChangeLog

Drop all tables so you can start over:dropAll

Run updates against a DB:liquibase --changeLogFile=myChangeLog update

Just output SQL, don’t run: updateSQL

Handy Liquibase Commands

Page 44: JRuby in the enterprise

USING LIQUIBASE

WITH MAVEN

Page 45: JRuby in the enterprise

Liquibase with Rails Directly

https://github.com/redbeard/liquibase-rails

Supposed to add “drop in” migration tasks for JRuby/Rails.

Page 46: JRuby in the enterprise

Liquibase Changeset

Page 47: JRuby in the enterprise

JNDI Database AccessDATABASE.YML

Page 48: JRuby in the enterprise

Servlet Filters

You’ll need a custom web.xml config

Copy WARBLER_HOME/web.xml.erb to config/web.xml.erb

Look for it at ~/.rvm/gems/jruby-1.6.2/gems/warbler-1.3.1/web.xml.erb

HTTP://EN.WIKIPEDIA.ORG/WIKI/FILE:DICHROIC_FILTERS.JPG

Page 49: JRuby in the enterprise

web.xml.erb Filter Entry

Page 50: JRuby in the enterprise

Java Integration

Nothing special, just the usual JRuby/Java integration

Looks for JARs in the container classpath, WAR or in yourapp/lib

Page 51: JRuby in the enterprise

Calling Java

HTTPS://GITHUB.COM/JRUBY/JRUBY/WIKI/CALLINGJAVAFROMJRUBY

Page 52: JRuby in the enterprise

Tips, Tricks and Lessons

http://en.wikipedia.org/wiki/File:Coolidge_after_signing_indian_treaty.jpg

Page 53: JRuby in the enterprise

Warbler Config

generate a warbler config file

warble config

Page 54: JRuby in the enterprise
Page 55: JRuby in the enterprise

Pre-Compile Ruby Classes

config.features = %w(compiled)

Page 56: JRuby in the enterprise

Set Min/Max Runtimes

Page 57: JRuby in the enterprise

Set JNDI Source Name

Page 58: JRuby in the enterprise

Set Root Path for App

By default, the root of your app is the WAR name

This may not be what you want.

Page 59: JRuby in the enterprise

Set the Root Path

Create file jboss-web.xml in root of app directory

Tell warbler to include it via warble.rb

Page 60: JRuby in the enterprise

Bundle Up Files into A GemCan be much faster to build/deploy depending on how you do it (there are a lot of files!)

Make sure to test it

Page 61: JRuby in the enterprise

Make Your War Executable

Command Line:warble executable war

In warble.rb:config.features = %w(executable)

Page 62: JRuby in the enterprise

Rails Console

Rails has a “Console Mode” for testing

Great for trying out ActiveRecord queries, etc.

Just use “rails console” to invoke it.

Page 63: JRuby in the enterprise

Gem Versions

Be sure to lock down Gem versions when you’re happy with them

Don’t want any last minute “upgrades!”

Use bundler

Put Gemfile.lock under source control.

Page 64: JRuby in the enterprise

Basic Versioning RulesAfter bundling, always check your Gemfile.lock into version control

After updating your Gemfile, always run bundle install first.

When running an executable, ALWAYS use bundle exec [command]

The only exception is the rails command!

http://yehudakatz.com/2011/05/30/gem-versioning-and-bundler-doing-it-right/

Page 65: JRuby in the enterprise

Handle Database Connection Outage

This is the “default” 500 error page.

Page 66: JRuby in the enterprise

You can Customize this but...

You can do better

Page 67: JRuby in the enterprise

Create “Status” URL

Want a well known URL that Nagios or a Load Balancer can check

Should return something useful (and specific to your test).

Page 68: JRuby in the enterprise

Insert Your Own MiddlewareCreate lib/db_check.rb

Page 69: JRuby in the enterprise

Insert Your Own MiddlewareInsert it into the stack (application.rb)

Page 70: JRuby in the enterprise
Page 71: JRuby in the enterprise

You Now Have Generic Status Ability

Extend for other things useful to your application.

Page 72: JRuby in the enterprise

HTTP://TORQUEBOX.ORG/

Page 73: JRuby in the enterprise

IDEs

Page 74: JRuby in the enterprise
Page 75: JRuby in the enterprise
Page 76: JRuby in the enterprise
Page 77: JRuby in the enterprise
Page 78: JRuby in the enterprise

Resources

Page 79: JRuby in the enterprise

Books

Page 80: JRuby in the enterprise

Websites

http://www.jruby.org/

http://jrubyconf.com/

http://vimeo.com/groups/87639(talks from 2010 JRubyConf)

http://www.engineyard.com/open-source

Page 81: JRuby in the enterprise

Twitter Users

http://twitter.com/#!/headius

http://twitter.com/#!/nicksieger

http://twitter.com/#!/jruby

Page 82: JRuby in the enterprise

Questions?

Page 83: JRuby in the enterprise

Thank You!

Don’t forget to fill out a session evaluation!