JRuby+ Rails =Awesome Java Web Framework!Nick Siegernsieger@engineyard.comJfokus 2011
Ruby: Developer  Happiness   =
“Ruby is the glue     that doesnt set”                     – Dave Thomashttp://pragdave.pragprog.com/pragdave/2006/06/glue...
Ruby =                     innovationhttp://www.flickr.com/photos/vermininc/2777441779/
Testing RSpechttp://rspec.info/             http://cukes.info/
RubyDynamic language of the cloud
Ruby: Dynamic,Object-Orientedhttp://www.flickr.com/photos/listenmissy/4869202176/
Duck-Typingdef area(width = 10, height = 2 * width)  width * heightendp   area           #   =>   200p   area 5         # ...
Duck-Typingp area "10", 4 # => "10101010"# From Ruby API docs:#   String#*(num)##   Returns a new String containing num co...
Flexible Syntaxdef set_options(env, opts)endset_options(:production, {"caching" => "on", "debug" => "false"})set_options(:...
Blocks       list = [1, 2, 3, 4]       list.each {|n| puts n }Ruby       list.each do |n|         puts n       end       L...
Blocks       File.open(__FILE__) do |file|         file.each_line do |line|Ruby       puts line         end       end     ...
Open Classesmsg = "Scramble this so you cant read it!"msg.rot13!  # => NoMethodError: undefined method `rot13! for  #    "...
Open Classesclass String  def rot13!    0.upto(length - 1) do |i|      case self[i]      when ?a..?z         self[i] = ?a ...
Open Classesputs msg.rot13!  # => "Fpenzoyr guvf fb lbh pnag ernq vg!"puts msg.rot13!  # => "Scramble this so you cant rea...
Rails 3Dynamic framework of the cloud
Opinionated             Framework              Request-    Convention    Defaults Place for               based         ov...
Place for everything            application code            configuration &             environments                routes...
Request-based MVCRequest    Routing    Controller                                      Model            Action     Action ...
Convention over ConfigurationURL          GET /people             resources :peopleRouting        #=> people#index        ...
Defaults with Choices                 Default        Alternatives                                DataMapper, MongoMapper,O...
Why Rails?    COMPARING JVM WEB       FRAMEWORKS                      Matt Raible                http://raibledesigns.com ...
Why Rails?Consider...              Information                  AvailableProject                     Development          ...
Installing Rails INSTALL   gem install rails
New Application$ rails new coolapp -m http://jruby.org      create      create README      create Rakefile      ...
Dependencies with Bundler$ cd coolapp$ bundle installFetching source index for http://rubygems.org/Using rake (0.8.7)Using...
Generate Scaffolding$ rails generate scaffold person email:string password:string      invoke active_record      create   ...
Migrate Database$ rake db:migrate(in /Users/nicksieger/Projects/rails/coolapp)== CreatePeople: migrating =================...
Start Dev Server$ rails server=> Booting WEBrick=> Rails 3.0.3 application starting in development on http://0.0.0.0:3000=...
First Page
Controllerclass PeopleController < ApplicationController  # GET /people  # GET /people.xml  def index    @people = Person....
Modelclass Person < ActiveRecord::Baseend
Console$ rails consoleLoading development environment (Rails 3.0.3)irb(main):001:0> Person.create :email => "nsieger@engin...
Rails 3 and JRubyhttp://weblog.rubyonrails.org/2010/8/29/rails-3-0-it-s-done
http://ci.jruby.org/
JRubyDynamic toolkit of the cloud
Getting JRubyhttp://jruby.org/download
JRuby via Maven        Group ID: org.jrubyArtifact IDs: jruby, jruby-complete
JRubydrive java • embed • compile
Drive Java       synth = javax.sound.midi.MidiSystem.synthesizerRuby   synth.open       channel = synth.channels[0]       ...
Drive Java       frame = javax.swing.JFrame.new "Music Frame"Ruby   frame.set_size 600, 100       frame.layout = java.awt....
Drive Java       KEYS.each do |value, char|         button = javax.swing.JButton.new char         button.add_action_listen...
MIDI Swing
Embedimport org.jruby.embed.ScriptingContainer;public class EmbedJRuby {    public static void main(String[] args) {      ...
http://wiki.jruby.org/      RedBridge
Compile# engine.rbrequire javajava_package democlass Engine  java_implements java.lang.Runnable  java_signature void run()...
Compile// Starter.javaimport demo.Engine;public class Starter {    public static void main(String[] args) {        Runnabl...
Compile$ jrubyc --javac engine.rb Starter.javaGenerating Java class Engine to demo/Engine.javajavac -d . -cp jruby.jar:. d...
Compile// Engine.javapackage demo;public class Engine implements Runnable {    public void run() { ... }}
Compile$ java -cp jruby.jar:. StarterThe #<Engine:0x59c958af> is running.
JRuby 1.6             Release soon!ruby 1.9.2 • c-ext • perf • dynopt • java
activerecord-jdbc ActiveRecord with JDBC databasesINSTALL   gem install activerecord-jdbc-adapter
Warbler              INSTALL    gem install warbler• Create a Java EE .war file from a Rails application• “Looks like Java”...
JRuby DeploymentRuby servers   WAR files     Cloud  WEBrick      GlassFish   EY AppCloud  Trinidad      Tomcat     AppEngi...
Enterprise Software Evolving and adapting long-running   projects with legacy codebases
Sagrada Família,Barcelona, Spain
passion                       facade   nativity   facadescaffolded interior
Ryugyuong Hotel,2005     North Korea      2010
seismic retrofit
Szkieletor,Kraków, Poland
Hybrid Rails/Java App                    ActionDispatch  Rails       ActionController/ActionView  MVC                     ...
https://github.com/nicksieger/        spring-petclinic
Metaphor     Use Ruby, JRuby, and Rails to...Sagrada      • Build new facade fasterFamilia      • Scaffolding during refac...
engineyard.com/services
Resources                    Resources for                 Getting Started with  JRuby.org         Ruby on RailsJRubyConf ...
Nick Siegernsieger@engineyard.comSlides: http://j.mp/sieger-jfokus
Imageshttp://en.wikipedia.org/wiki/File:Sagrada_Familia_01.jpghttp://www.flickr.com/photos/gonzalvo/4257293127/http://www.fl...
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
Upcoming SlideShare
Loading in...5
×

JRuby + Rails = Awesome Java Web Framework at Jfokus 2011

6,427

Published on

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,427
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
85
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

JRuby + Rails = Awesome Java Web Framework at Jfokus 2011

  1. 1. JRuby+ Rails =Awesome Java Web Framework!Nick Siegernsieger@engineyard.comJfokus 2011
  2. 2. Ruby: Developer Happiness =
  3. 3. “Ruby is the glue that doesnt set” – Dave Thomashttp://pragdave.pragprog.com/pragdave/2006/06/glue_that_doesn.html
  4. 4. Ruby = innovationhttp://www.flickr.com/photos/vermininc/2777441779/
  5. 5. Testing RSpechttp://rspec.info/ http://cukes.info/
  6. 6. RubyDynamic language of the cloud
  7. 7. Ruby: Dynamic,Object-Orientedhttp://www.flickr.com/photos/listenmissy/4869202176/
  8. 8. Duck-Typingdef area(width = 10, height = 2 * width) width * heightendp area # => 200p area 5 # => 50p area 5, 20 # => 100p area "10", 4 # => ?
  9. 9. Duck-Typingp area "10", 4 # => "10101010"# From Ruby API docs:# String#*(num)## Returns a new String containing num copies of# the receiver.## "Ho! " * 3 #=> "Ho! Ho! Ho! "area true, false # => NoMethodError: undefined method `* for # true:TrueClass
  10. 10. Flexible Syntaxdef set_options(env, opts)endset_options(:production, {"caching" => "on", "debug" => "false"})set_options(:production, "caching" => "on", "debug" => "false")set_options :production, {"caching" => "on", "debug" => "false"}set_options :production, "caching" => "on", "debug" => "false"
  11. 11. Blocks list = [1, 2, 3, 4] list.each {|n| puts n }Ruby list.each do |n| puts n end List<Integer> list = Arrays.asList(1, 2, 3, 4);Java for (Integer n : list) { System.out.println(n); }
  12. 12. Blocks File.open(__FILE__) do |file| file.each_line do |line|Ruby puts line end end BufferedReader file = new BufferedReader(new FileReader("Blocks.java")); try { String line; while ((line = buf.readLine()) != null) {Java System.out.println(line); } } finally { file.close(); }
  13. 13. Open Classesmsg = "Scramble this so you cant read it!"msg.rot13! # => NoMethodError: undefined method `rot13! for # "Scramble this so you cant read it!":String
  14. 14. Open Classesclass String def rot13! 0.upto(length - 1) do |i| case self[i] when ?a..?z self[i] = ?a + ((self[i] - ?a) + 13) % 26 when ?A..?Z self[i] = ?A + ((self[i] - ?A) + 13) % 26 end end self endend
  15. 15. Open Classesputs msg.rot13! # => "Fpenzoyr guvf fb lbh pnag ernq vg!"puts msg.rot13! # => "Scramble this so you cant read it!"
  16. 16. Rails 3Dynamic framework of the cloud
  17. 17. Opinionated Framework Request- Convention Defaults Place for based over witheverything Configuration MVC Choices
  18. 18. Place for everything application code configuration & environments routes (URL structure) database migrations static assets (images, js, css) tests
  19. 19. Request-based MVCRequest Routing Controller Model Action Action ActiveRecord Dispatch Controller View DatabaseResponse ActionView
  20. 20. Convention over ConfigurationURL GET /people resources :peopleRouting #=> people#index # app/controllers/people_controller.rb class PeopleController < ApplicationController def indexController @people = Person.all end end # app/models/person.rbModel class Person < ActiveRecord::Base endView app/views/people/index.html.erb
  21. 21. Defaults with Choices Default Alternatives DataMapper, MongoMapper,ORM ActiveRecord Sequel, Any object with ActiveModel HAML, XML Builder,View Templates ERb Markaby, RedCloth (Textile), BlueCloth (Markdown)JavaScript Prototype jQueryFramework MySQL, PostgreSQL,Database SQLite3 Oracle, more via JRuby + JDBCTest Framework Test::Unit RSpec, Cucumber
  22. 22. Why Rails? COMPARING JVM WEB FRAMEWORKS Matt Raible http://raibledesigns.com Images by Stuck in Customs - http://www.flickr.com/photos/stuckincustoms © 2010, Raible Designs © 2010 Raible Designshttp://j.mp/raible-jvm-frameworks
  23. 23. Why Rails?Consider... Information AvailableProject Development Books, skilledmaturity Docs speed developers
  24. 24. Installing Rails INSTALL gem install rails
  25. 25. New Application$ rails new coolapp -m http://jruby.org create create README create Rakefile ...
  26. 26. Dependencies with Bundler$ cd coolapp$ bundle installFetching source index for http://rubygems.org/Using rake (0.8.7)Using abstract (1.0.0)...Using rails (3.0.3)Your bundle is complete!
  27. 27. Generate Scaffolding$ rails generate scaffold person email:string password:string invoke active_record create db/migrate/20101214020707_create_people.rb create app/models/person.rb invoke test_unit create test/unit/person_test.rb create test/fixtures/people.yml route resources :people ...
  28. 28. Migrate Database$ rake db:migrate(in /Users/nicksieger/Projects/rails/coolapp)== CreatePeople: migrating ===========================-- create_table(:people) -> 0.0040s -> 0 rows== CreatePeople: migrated (0.0040s) ==================
  29. 29. Start Dev Server$ rails server=> Booting WEBrick=> Rails 3.0.3 application starting in development on http://0.0.0.0:3000=> Call with -d to detach=> Ctrl-C to shutdown server[2010-12-13 20:11:28] INFO WEBrick 1.3.1[2010-12-13 20:11:28] INFO ruby 1.8.7 (2010-12-10) [java][2010-12-13 20:11:28] INFO WEBrick::HTTPServer#start: pid=21022 port=3000
  30. 30. First Page
  31. 31. Controllerclass PeopleController < ApplicationController # GET /people # GET /people.xml def index @people = Person.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @people } end end def show; end def new; end def edit; end def create; end def update; end def destroy; endend
  32. 32. Modelclass Person < ActiveRecord::Baseend
  33. 33. Console$ rails consoleLoading development environment (Rails 3.0.3)irb(main):001:0> Person.create :email => "nsieger@engineyard.com", ...=> #<Person id: 1, email: "nsieger@engineyard.com", ...>
  34. 34. Rails 3 and JRubyhttp://weblog.rubyonrails.org/2010/8/29/rails-3-0-it-s-done
  35. 35. http://ci.jruby.org/
  36. 36. JRubyDynamic toolkit of the cloud
  37. 37. Getting JRubyhttp://jruby.org/download
  38. 38. JRuby via Maven Group ID: org.jrubyArtifact IDs: jruby, jruby-complete
  39. 39. JRubydrive java • embed • compile
  40. 40. Drive Java synth = javax.sound.midi.MidiSystem.synthesizerRuby synth.open channel = synth.channels[0] import javax.sound.midi.*;Java Synthesizer synth = MidiSystem.getSynthesizer(); synth.open(); final MidiChannel channel = synth.getChannels()[0];
  41. 41. Drive Java frame = javax.swing.JFrame.new "Music Frame"Ruby frame.set_size 600, 100 frame.layout = java.awt.FlowLayout.new import java.awt.*;Java JFrame frame = new JFrame("Music Frame"); frame.setSize(600, 100); frame.setLayout(new java.awt.FlowLayout());
  42. 42. Drive Java KEYS.each do |value, char| button = javax.swing.JButton.new char button.add_action_listener do |e|Ruby channel.note_on value, 99 end frame.add button end for (Iterator i = keys.entrySet().iterator(); i.hasNext(); ) { Map.Entry entry = (Map.Entry) i.next(); final Integer value = (Integer) entry.getKey(); String name = (String) entry.getValue(); JButton button = new JButton(name);Java button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { channel.noteOn(value, 99); } }); frame.add(button); }
  43. 43. MIDI Swing
  44. 44. Embedimport org.jruby.embed.ScriptingContainer;public class EmbedJRuby { public static void main(String[] args) { ScriptingContainer container = new ScriptingContainer(); container.runScriptlet("puts Hello from Ruby"); }}
  45. 45. http://wiki.jruby.org/ RedBridge
  46. 46. Compile# engine.rbrequire javajava_package democlass Engine java_implements java.lang.Runnable java_signature void run() def run puts "The #{self.inspect} is running." endend
  47. 47. Compile// Starter.javaimport demo.Engine;public class Starter { public static void main(String[] args) { Runnable engine = new Engine(); engine.run(); }}
  48. 48. Compile$ jrubyc --javac engine.rb Starter.javaGenerating Java class Engine to demo/Engine.javajavac -d . -cp jruby.jar:. demo/Engine.java Starter.java
  49. 49. Compile// Engine.javapackage demo;public class Engine implements Runnable { public void run() { ... }}
  50. 50. Compile$ java -cp jruby.jar:. StarterThe #<Engine:0x59c958af> is running.
  51. 51. JRuby 1.6 Release soon!ruby 1.9.2 • c-ext • perf • dynopt • java
  52. 52. activerecord-jdbc ActiveRecord with JDBC databasesINSTALL gem install activerecord-jdbc-adapter
  53. 53. Warbler INSTALL gem install warbler• Create a Java EE .war file from a Rails application• “Looks like Java” to the ops staff deploy Rails warble app.war to java app appserver
  54. 54. JRuby DeploymentRuby servers WAR files Cloud WEBrick GlassFish EY AppCloud Trinidad Tomcat AppEngine TorqueBox JBoss AWS EB
  55. 55. Enterprise Software Evolving and adapting long-running projects with legacy codebases
  56. 56. Sagrada Família,Barcelona, Spain
  57. 57. passion facade nativity facadescaffolded interior
  58. 58. Ryugyuong Hotel,2005 North Korea 2010
  59. 59. seismic retrofit
  60. 60. Szkieletor,Kraków, Poland
  61. 61. Hybrid Rails/Java App ActionDispatch Rails ActionController/ActionView MVC ActiveModel Java Java JDBC SOAP Backend POJOs DataSource interface
  62. 62. https://github.com/nicksieger/ spring-petclinic
  63. 63. Metaphor Use Ruby, JRuby, and Rails to...Sagrada • Build new facade fasterFamilia • Scaffolding during refactoringRyugyong • Revive a project with a new face Hotel Seismic • Reinforce business rules with a DSL retrofit • Harden securitySzkieletor • Find novel uses for abandoned code
  64. 64. engineyard.com/services
  65. 65. Resources Resources for Getting Started with JRuby.org Ruby on RailsJRubyConf 2010 Rails for Videos Zombies
  66. 66. Nick Siegernsieger@engineyard.comSlides: http://j.mp/sieger-jfokus
  67. 67. Imageshttp://en.wikipedia.org/wiki/File:Sagrada_Familia_01.jpghttp://www.flickr.com/photos/gonzalvo/4257293127/http://www.flickr.com/photos/mgrenner57/263392884/http://www.flickr.com/photos/koocheekoo/38407225/http://www.flickr.com/photos/27649557@N07/5000528445/http://www.flickr.com/photos/gpaumier/446059442/http://www.flickr.com/photos/ilm/12831049/http://en.wikipedia.org/wiki/File:Ryugyong_Hotel_-_May_2005.JPGhttp://en.wikipedia.org/wiki/File:Ryugyong_Hotel_October_2010.jpghttp://en.wikipedia.org/wiki/File:ExteiorShearTruss.jpghttp://en.wikipedia.org/wiki/File:ExtReenfDetail.jpghttp://en.wikipedia.org/wiki/File:Szkieleteor_in_krakow.JPGhttp://www.flickr.com/photos/bazylek/3194294047/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×