57
Getting Started with Rails on GlassFish (Hands-on Lab) Arun Gupta, Java EE & GlassFish Guy, Oracle Corp blogs.sun.com/arungupta, @arungupta

Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010

Embed Size (px)

DESCRIPTION

Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010

Citation preview

Getting Started withRails on GlassFish(Hands-on Lab)

Arun Gupta, Java EE & GlassFish Guy,Oracle Corpblogs.sun.com/arungupta, @arungupta

What this session is not ?

• Not a GlassFish tutorial• Not a Rails tutorial• Not a MySQL tutorial

Software Installed ?

• JDK 1.6.0_XX• NetBeans 6.8 (“All” or “Ruby” bundle)• JRuby 1.4.0• MySQL Community Server

15 minutes

What is GlassFish ?

What is GlassFish ?

GlassFish: It's a Community!

GlassFish: Lives on java.net

Since 2005

GlassFish: Open Source and Production Quality

GlassFish: Reference Implementation

Java EE 5Java EE 6

GlassFish v3

What is Ruby-on-Rails ?

• Full-stack Web development framework in Ruby• Popularly known as Rails

• Current Version: Rails 2.3.5• Rails 3.0 coming

Ruby-on-Rails MVC

Web Server

ActionController(Controller)

ActiveRecord(Model)

ActionView(View)

Database

xxx_controller.rb

.rhtml .erbxxx.rb

IncomingRequest

Rails Design Principles

• Convention over Configuration (CoC)• Common cases easily served• Encourages standard practices• Everything simpler and smaller

• Don't Repeat Yourself (DRY)• Framework written around minimizing repetition

• Agile Development• No compile, redeploy, restart cycles• Testing built into the framework

JRuby

• Ruby interpreter written in Java• Current Version: 1.4.0

• 1.5 RC coming soon• Ruby 1.8.7 compatible• Some Ruby 1.9 support

• --1.9 flag (80%)

JRuby

• Solid performance ( ͋Ruby 1.9)• Startup is poor, execution usually better than

1.9• Runs native threads• Foreign Function Interface (FFI)for C

libraries• Adopted from Rubinius, CRuby gem

• Runs Rails great!

Install JRuby

• CLI: Download & Unzip “jruby-bin-1.4.0.zip” from jruby.org

• NetBeans “All” or “Ruby” bundle (pre-installed)

Install Rails & GlassFish Gem

• CLI: “jruby -S gem install rails glassfish”

• NetBeans: Tools -> Ruby Gems -> New Gems• Rails pre-installed• Search “glassfish”, Install 5 minutes

Start Database

• Mac/Linux: mysqld_safe --user root

• Windows: bin/mysqld-nt --user root• Installed as service ?

2 minutes

Install Database Adapter

“jruby -S gem install activerecord-jdbcmysql-adapter”

2 minutes

Create a Rails App

jruby -S rails helloworld -d mysqlcd helloworldjruby script/generate jdbcjruby -S rake db:create

5 minutes

Run the App

cd helloworldjruby -S glassfish

5 minutes

Add CRUD features

jruby script/generate scaffold book title:string author:string

5 minutes

Model View Controller

exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/books exists app/views/layouts/ exists test/functional/ exists test/unit/ create test/unit/helpers/ exists public/stylesheets/ create app/views/books/index.html.erb create app/views/books/show.html.erb create app/views/books/new.html.erb create app/views/books/edit.html.erb create app/views/layouts/books.html.erb create public/stylesheets/scaffold.css create app/controllers/books_controller.rb create test/functional/books_controller_test.rb create app/helpers/books_helper.rb create test/unit/helpers/books_helper_test.rb route map.resources :books dependency model exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/book.rb create test/unit/book_test.rb create test/fixtures/books.yml create db/migrate create db/migrate/20100310221044_create_books.rb

Generate Database Table

jruby -S rake db:migrate

== CreateBooks: migrating ============================-- create_table(:books) -> 0.1690s -> 0 rows== CreateBooks: migrated (0.1710s) =====================

2 minutes

Sample Output

5 minutes

GlassFish Gem

http://rubyforge.org/projects/glassfishgem/

GlassFish Gem: Options

jruby -S glassfish -h

-c: change the context root-e: environment-d: runs as daemon-P: location of PID file-l: log file location-log-level: Logging level (0-7)

GlassFish Gem: Recipes

Why Rails on GlassFish ? - Agile

Why ? - Easy prototyping, yet powerful

Why ? - Best of both worlds

Why ? - Database Connection Pooling

Why ? - Management

Why ? - Monitoring

Directory-based deployment: How ?

asadmin start-domainasadmin deploy --property "jruby.home=JRUBY_HOME" helloworld

CompleteAbsolute

Path

7 minutes

Directory-based deployment: Recipes

Not Just Rails ...

http://blogs.sun.com/arungupta/entry/totd_53_scaffold_in_merbhttp://blogs.sun.com/arungupta/entry/totd_80_sinatra_crud_application

And more ...

http://weblogs.java.net/blog/vivekp/archive/2009/04/plugin_any_ruby.html

Deployment Options

http://blogs.sun.com/arungupta/entry/totd_81_how_to_usehttp://blog.headius.com/2009/04/apache-jruby-rails-glassfish-easy.html

Rails on GlassFish: WAR Deployment

WAR Deployment: How ?

jruby -S gem install warblerjruby -S warbleasadmin deploy xxx.war

http://blogs.sun.com/arungupta/entry/totd_73_jruby_and_glassfish7 minutes

WAR Deployment: How in NetBeans ?

WAR Deployment: Packaging

Rails on GlassFish: Update Center

NetBeans: Develop with Pleasure

http://blogs.sun.com/arungupta/entry/screencast_26_develop_run_debug

10 minutes

Rails Monitoring

• Basic Monitoring• jinfo: System properties and VM CLI flags• jmap: Shared object memory map• jstack: Stack traces of Java threads• jstat: Performance statistics on class, JIT compiler, GC, ...

• Advanced• jconsole / Visual VM• NewRelic, FiveRuns, ...

jConsole

http://blogs.sun.com/arungupta/tags/rubyonrails+jmx

Turbo charging your JRuby Startup

• “-client” or “-server” ? (Upto 50%)• Faster startup or solid runtime ?• export JAVA_OPTS=”-client -D32”• Upto 50% improvement

• For Linux: Class Data Sharing• Delay or disable JRuby's JIT (Upto 10%)

• -X-C• jruby.compile.mode=OFF

• Avoid spawning “sub-Rubies”• Nailgun for small JRuby CLI invocations• Favorite JVM Flags

Who loves Rails on GlassFish ?

Resources

• glassfish.org• blogs.sun.com/theaquarium• glassfish-scripting.dev.java.net• wiki.glassfish.java.net/Wiki.jsp?page=JRuby

• blogs.sun.com/arungupta