JRuby + Rails = Awesome Java Web Framework at Jfokus 2011

  • 6,007 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
6,007
On Slideshare
0
From Embeds
0
Number of Embeds
3

Actions

Shares
Downloads
83
Comments
0
Likes
6

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. JRuby+ Rails =Awesome Java Web Framework!Nick Siegernsieger@engineyard.comJfokus 2011
  • 2. Ruby: Developer Happiness =
  • 3. “Ruby is the glue that doesnt set” – Dave Thomashttp://pragdave.pragprog.com/pragdave/2006/06/glue_that_doesn.html
  • 4. Ruby = innovationhttp://www.flickr.com/photos/vermininc/2777441779/
  • 5. Testing RSpechttp://rspec.info/ http://cukes.info/
  • 6. RubyDynamic language of the cloud
  • 7. Ruby: Dynamic,Object-Orientedhttp://www.flickr.com/photos/listenmissy/4869202176/
  • 8. Duck-Typingdef area(width = 10, height = 2 * width) width * heightendp area # => 200p area 5 # => 50p area 5, 20 # => 100p area "10", 4 # => ?
  • 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. 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. 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. 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. 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. 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. Open Classesputs msg.rot13! # => "Fpenzoyr guvf fb lbh pnag ernq vg!"puts msg.rot13! # => "Scramble this so you cant read it!"
  • 16. Rails 3Dynamic framework of the cloud
  • 17. Opinionated Framework Request- Convention Defaults Place for based over witheverything Configuration MVC Choices
  • 18. Place for everything application code configuration & environments routes (URL structure) database migrations static assets (images, js, css) tests
  • 19. Request-based MVCRequest Routing Controller Model Action Action ActiveRecord Dispatch Controller View DatabaseResponse ActionView
  • 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. 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. 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. Why Rails?Consider... Information AvailableProject Development Books, skilledmaturity Docs speed developers
  • 24. Installing Rails INSTALL gem install rails
  • 25. New Application$ rails new coolapp -m http://jruby.org create create README create Rakefile ...
  • 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. 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. 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. 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. First Page
  • 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. Modelclass Person < ActiveRecord::Baseend
  • 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. Rails 3 and JRubyhttp://weblog.rubyonrails.org/2010/8/29/rails-3-0-it-s-done
  • 35. http://ci.jruby.org/
  • 36. JRubyDynamic toolkit of the cloud
  • 37. Getting JRubyhttp://jruby.org/download
  • 38. JRuby via Maven Group ID: org.jrubyArtifact IDs: jruby, jruby-complete
  • 39. JRubydrive java • embed • compile
  • 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. 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. 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. MIDI Swing
  • 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. http://wiki.jruby.org/ RedBridge
  • 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. Compile// Starter.javaimport demo.Engine;public class Starter { public static void main(String[] args) { Runnable engine = new Engine(); engine.run(); }}
  • 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. Compile// Engine.javapackage demo;public class Engine implements Runnable { public void run() { ... }}
  • 50. Compile$ java -cp jruby.jar:. StarterThe #<Engine:0x59c958af> is running.
  • 51. JRuby 1.6 Release soon!ruby 1.9.2 • c-ext • perf • dynopt • java
  • 52. activerecord-jdbc ActiveRecord with JDBC databasesINSTALL gem install activerecord-jdbc-adapter
  • 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. JRuby DeploymentRuby servers WAR files Cloud WEBrick GlassFish EY AppCloud Trinidad Tomcat AppEngine TorqueBox JBoss AWS EB
  • 55. Enterprise Software Evolving and adapting long-running projects with legacy codebases
  • 56. Sagrada Família,Barcelona, Spain
  • 57. passion facade nativity facadescaffolded interior
  • 58. Ryugyuong Hotel,2005 North Korea 2010
  • 59. seismic retrofit
  • 60. Szkieletor,Kraków, Poland
  • 61. Hybrid Rails/Java App ActionDispatch Rails ActionController/ActionView MVC ActiveModel Java Java JDBC SOAP Backend POJOs DataSource interface
  • 62. https://github.com/nicksieger/ spring-petclinic
  • 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. engineyard.com/services
  • 65. Resources Resources for Getting Started with JRuby.org Ruby on RailsJRubyConf 2010 Rails for Videos Zombies
  • 66. Nick Siegernsieger@engineyard.comSlides: http://j.mp/sieger-jfokus
  • 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/