Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on

Published in: Technology, News & Politics
  • Be the first to comment

  • Be the first to like this


  1. 1. NJUG 5.0 <ul><li>Jruby </li></ul><ul><li>Groory </li></ul><ul><li>พักกินข้าว </li></ul><ul><li>Java Update Java Fx </li></ul><ul><li>Ajax with Cold Fusion </li></ul><ul><li>VIOP may be </li></ul>
  2. 2. JRuby @ NJUG By Roofimon NJUG 5.0 May 11, 2008 “ roofx<at>hotmail<dot>com”
  3. 3. Knowledge Resources( ลอกมาจากไหน ) <ul><li>Jruby Wiki </li></ul><ul><li> </li></ul><ul><li>JRuby : Not Just Another JVM Language InfoQ </li></ul><ul><li> </li></ul>
  4. 4. Agenda <ul><li>Brief History of Scripting Language </li></ul><ul><li>Brief History of Ruby </li></ul><ul><li>Brief History of Jruby </li></ul><ul><li>Rails and Jruby on Rails </li></ul><ul><li>Jruby Java and Java Jruby </li></ul><ul><li>Jruby Enterprise Java </li></ul><ul><li>Jruby Spring Bean and Controller </li></ul><ul><li>Jruby Web Services </li></ul><ul><li>Deploy Jruby </li></ul><ul><li>Q and A, Surprise </li></ul>
  5. 5. Brief History of Scripting Lang. <ul><li>First interactive shells were developed in the 1960s, shellscripts </li></ul><ul><li>Scripting languages became complex enough to be used to extend the application </li></ul><ul><li>Scripting languages have been interpretive rather than compiled languages </li></ul><ul><li>Types of scripting languages </li></ul><ul><ul><ul><li>Job control languages and shells, shellscript </li></ul></ul></ul><ul><ul><ul><li>Application-specific languages, macro </li></ul></ul></ul><ul><ul><ul><li>Web browsers, javascript </li></ul></ul></ul><ul><ul><ul><li>Web servers, PHP </li></ul></ul></ul><ul><ul><ul><li>General-purpose dynamic languages, Ruby Python </li></ul></ul></ul>
  6. 6. Brief history of Ruby <ul><li>At that time ( 1993 ) there is no OO script language </li></ul><ul><li>Project must be code-named after a gemstone's name (àla Perl). So my friend came up with &quot;ruby&quot; </li></ul><ul><li>The Ideals of Ruby’s Creator </li></ul><ul><li>“ ruby is simple in appearance, but is very complex inside, just like our human body” </li></ul><ul><li>I believe people want to express themselves when they program. They don't want to fight with the language. Programming languages must feel natural to programmers. </li></ul>
  7. 7. Taste of Ruby <ul><li>Everything is Object </li></ul><ul><li>5.times{ print &quot;We *love* Ruby--it's outrageous!&quot; } </li></ul><ul><li>public class HiFive{ </li></ul><ul><li>public static void main(String args[]){ </li></ul><ul><li>for(int I =0; I <= 4; i++){ </li></ul><ul><li>System.out.println(“We are sick of typing”); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  8. 8. Taste of Ruby <ul><li>Always open </li></ul><ul><li>class Numeric </li></ul><ul><li>def plus(x) </li></ul><ul><li>self.+x </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>temp = 6 </li></ul><ul><li>Code Block </li></ul><ul><li>def simple </li></ul><ul><li>puts 'Here comes the code block!' </li></ul><ul><li>yield </li></ul><ul><li>puts 'There was the code block!' </li></ul><ul><li>end </li></ul><ul><li>simple { puts 'Hooray! The code block is here!' } </li></ul><ul><li>» Here comes the code block! </li></ul><ul><li>Hooray! The code block is here! </li></ul><ul><li>There was the code block !    </li></ul><ul><li>[1,2,3].each { |element| print element } </li></ul>
  9. 9. Taste of Ruby Mixin Cool <ul><li>module BarModule </li></ul><ul><li>def hello_world </li></ul><ul><li>puts &quot;Hello World&quot; </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>class BaseClass </li></ul><ul><li>def class_method </li></ul><ul><li>puts &quot;In class method&quot; </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>class Foo < BaseClass </li></ul><ul><li>include BarModule </li></ul><ul><li>end </li></ul><ul><li>f = </li></ul><ul><li>f.class_method </li></ul><ul><li>f.hello_world </li></ul>
  10. 10. JRuby on Rails <ul><li>History of Rails </li></ul><ul><li>First Released on 2004 (Spring 1.0) </li></ul><ul><li>By David Heinemeier Hansson from his work on Basecamp </li></ul><ul><li>The fundamental Ruby on Rails principles include Convention over Configuration (CoC) and Don't repeat yourself (DRY). </li></ul><ul><li>Jruby did it but on JVM </li></ul>
  11. 11. Why Jruby on Rails <ul><li>Performance </li></ul><ul><li>Unicode? </li></ul><ul><li>Thread (Not Green Thread) </li></ul><ul><li>Existing Library from Java </li></ul><ul><li>Still can use Ruby coding style, cool </li></ul><ul><li>Oracle Mix saids Jruby is the best way </li></ul>
  12. 12. Brief History of Jruby <ul><li>Jruby was originally created by Jan Arne Petersen, in 2001 </li></ul><ul><li>JRuby is a Java implementation of the Ruby interprete </li></ul><ul><li>In September 2006 , Sun Microsystems hired Enebo and Nutter to work on JRuby full time. </li></ul><ul><li>From 2006, users can port RoR(0.9) application to Jruby without any changes </li></ul><ul><li>Microsoft also hired the creator of IronRuby </li></ul>
  13. 13. Brief History of Jruby <ul><li>JRuby 1.1 was released on April 5 2008 </li></ul><ul><li>Highlights: </li></ul><ul><ul><li>reduced memory consumption (permgen) </li></ul></ul><ul><ul><li>stat()/lstat() 300% faster </li></ul></ul><ul><ul><li>improved startup time </li></ul></ul>
  14. 14. Jruby Java (Most Important) <ul><li>Java Resources </li></ul><ul><ul><li>Classes </li></ul></ul><ul><ul><li>Primatives </li></ul></ul><ul><ul><li>Array </li></ul></ul><ul><li>Extend Java </li></ul><ul><ul><li>Interfaces </li></ul></ul><ul><ul><li>Classes </li></ul></ul><ul><li>Java Collection </li></ul>
  15. 15. Jruby Java (Class) <ul><li>include_class(&quot;java.util.HashMap&quot;) {|pkg,name| &quot;JHashMap&quot;} </li></ul><ul><li>m = </li></ul><ul><li>m.put &quot;hello&quot;, &quot;world&quot; </li></ul><ul><li>m.put &quot;goodbye&quot;, &quot;life&quot; </li></ul><ul><li>puts m.get(&quot;hello&quot;) </li></ul><ul><li>iter = m.keySet.iterator </li></ul><ul><li>while iter.hasNext </li></ul><ul><li>key = </li></ul><ul><li>puts &quot;#{key}=#{m.get(key)}&quot; </li></ul><ul><li>end </li></ul>
  16. 16. Jruby Java (Primative) <ul><li>b1 = Java::byte </li></ul><ul><li>b2 = java.lang.Byte::TYPE </li></ul><ul><li>Working with primitives from JRuby isn’t recommended </li></ul>
  17. 17. Jruby Java (Array) <ul><li>Usually you only need to use arrays in the sense of creating them so you can send them in to Java code. </li></ul><ul><li>[1,2,3].to_java # new Object[] {new Integer(1), </li></ul><ul><li># new Integer(2), new Integer(3)} </li></ul><ul><li>[1,2,3].to_java :byte # new byte[] {1,2,3} </li></ul><ul><li>[1,2,3].to_java :char # new char[] {1,2,3} </li></ul><ul><li>[1,2,3].to_java :float # new float[] {1,2,3} </li></ul><ul><li># etc </li></ul><ul><li>[&quot;str&quot;, &quot;str2&quot;].to_java java.lang.String # new String[]{&quot;str&quot;,&quot;str2&quot;} </li></ul>
  18. 18. Jruby Java <ul><li>require 'java' </li></ul><ul><li>include_class 'java.util.TreeSet' </li></ul><ul><li>set = </li></ul><ul><li>set.add &quot;foo&quot; </li></ul><ul><li>set.add &quot;Bar&quot; </li></ul><ul><li>set.add &quot;baz&quot; </li></ul><ul><li>set.each do |v| </li></ul><ul><li>puts &quot;value: #{v}&quot; </li></ul><ul><li>end </li></ul><ul><li>value: Bar </li></ul><ul><li>value: baz </li></ul><ul><li>value: foo </li></ul>
  19. 19. Extend Java by using Interface <ul><li>package com.example; </li></ul><ul><li>public interface ISpeaker { </li></ul><ul><li>public void say(String msg); </li></ul><ul><li>public void addOne(String from); </li></ul><ul><li>} </li></ul>class CallJava include ISpeaker def initialize super @count = 0 end def say(msg) puts &quot;Ruby saying #{msg}&quot; end def addOne(from) # m.synchronize { @count += 1 puts &quot;Now got #@count from #{from}&quot; # } end end
  20. 20. Extend Java by using Class <ul><li>Due to Jruby and Ruby are always Open so </li></ul><ul><li>class FooStream < </li></ul><ul><li>def write(p) </li></ul><ul><li>puts p </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>s = </li></ul><ul><li>s.write([1,2,3].to_java(:byte), 0, 3) </li></ul><ul><li>s.flush </li></ul>
  21. 21. Jruby Java (Most Important) <ul><li>Collections.sort(foos, new Comparator() { </li></ul><ul><li>public int compare(Object o1, Object o2) { </li></ul><ul><li>return ((FooObject)o1).getName().compareTo(((FooObject)o2).getName()); </li></ul><ul><li>} </li></ul><ul><li>}); </li></ul>foos.sort_by {|o|} Of course, if you have Facets or Rails loaded you can do it like this: foos.sort_by &:name
  22. 22. Jruby Java (Most Important) <ul><li>Collections.sort(foos, new Comparator() { </li></ul><ul><li>public int compare(Object o1, Object o2) { </li></ul><ul><li>return ((FooObject)o1).getName().compareTo(((FooObject)o2).getName()); </li></ul><ul><li>} </li></ul><ul><li>}); </li></ul>foos.sort_by {|o|} Of course, if you have Facets or Rails loaded you can do it like this: foos.sort_by &:name
  23. 23. J ava Jruby (S….) <ul><li>Java 6 (using JSR 223: Scripting) </li></ul><ul><li>Embedding with Bean Scripting Framework (BSF) </li></ul><ul><li>Jruby Runtime </li></ul>
  24. 24. Jruby Enterprise Bean <ul><li>Create EJB3 with Jruby </li></ul><ul><li>Bloody hard </li></ul><ul><li>In case we have Active Record Services but would like to wrap them with java </li></ul><ul><li>Create Wrapper Class which using low-level integrated with jRuby </li></ul>
  25. 25. Jruby Spring <ul><li>Spring 2.0 introduces comprehensive support for </li></ul><ul><ul><li>JRuby 0.9 / 1.0 </li></ul></ul><ul><ul><li>Groovy 1.0 / 1.5 </li></ul></ul><ul><ul><li>BeanShell 2.0 </li></ul></ul><ul><li>Each of the supported languages has a corresponding <lang:language/> element: </li></ul><ul><ul><li><lang:jruby/> (JRuby) </li></ul></ul><ul><ul><li><lang:groovy/> (Groovy) </li></ul></ul><ul><ul><li><lang:bsh/> (BeanShell) </li></ul></ul>
  26. 26. Jruby Spring Bean <ul><ul><li>require 'java' </li></ul></ul><ul><ul><li>class RubyMessenger </li></ul></ul><ul><ul><li>include org.springframework.scripting.Messenger </li></ul></ul><ul><ul><li>def setMessage(message) </li></ul></ul><ul><ul><li>@@message = message </li></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><li>def getMessage </li></ul></ul><ul><ul><li>@@message </li></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><li># this last line is not essential (but see below) </li></ul></ul><ul><ul><li> </li></ul></ul>
  27. 27. Jruby Spring Bean <ul><ul><li><lang:jruby id=&quot;messageService&quot; </li></ul></ul><ul><ul><li>refresh-check-delay=&quot; 3000 &quot; </li></ul></ul><ul><ul><li>script interfaces=“xxx.scripting.Messenger&quot; </li></ul></ul><ul><ul><li>script-source=&quot; classpath:RubyMessenger.rb &quot;> </li></ul></ul><ul><ul><li><lang:property name=&quot;message&quot; value=&quot;Hello World!&quot; /> </li></ul></ul><ul><ul><li></lang:jruby> </li></ul></ul>
  28. 28. Jruby Spring Controller <ul><ul><li>require 'java' </li></ul></ul><ul><ul><li>  </li></ul></ul><ul><ul><li>include_class 'org.springframework.web.servlet.ModelAndView' </li></ul></ul><ul><ul><li>include_class 'org.springframework.web.servlet.mvc.Controller' </li></ul></ul><ul><ul><li>  </li></ul></ul><ul><ul><li>class MyController <Controller </li></ul></ul><ul><ul><li>def handleRequest ( request, response ) </li></ul></ul><ul><ul><li>puts &quot;I'm in Ruby&quot; </li></ul></ul><ul><ul><li>mav = @@resultView </li></ul></ul><ul><ul><li>mav.addObject ( &quot;key&quot;,&quot;value&quot; ) </li></ul></ul><ul><ul><li>    return mav </li></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><li>def setResultView ( resultView ) </li></ul></ul><ul><ul><li>@@resultView = resultView </li></ul></ul><ul><ul><li>end   </li></ul></ul><ul><ul><li>def toString </li></ul></ul><ul><ul><li>return &quot;MyController in Ruby&quot; </li></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><li>end   </li></ul></ul><ul><ul><li># needed by spring </li></ul></ul><ul><ul><li> </li></ul></ul>
  29. 29. Jruby Spring Controller <ul><ul><li><lang :jruby id=&quot;myController&quot; </li></ul></ul><ul><ul><li>refresh-check-delay=&quot;3000“ </li></ul></ul><ul><ul><li>script-source=&quot;/WEB-INF/Ruby/MyController.rb&quot; </li></ul></ul><ul><ul><li>script-interfaces=“xxxx.servlet.mvc.Controller&quot; > </li></ul></ul><ul><ul><li><lang :property name=&quot;resultView&quot; value=&quot;view&quot; /> </li></ul></ul><ul><ul><li></lang :jruby > </li></ul></ul>
  30. 30. Sample Application <ul><li>Install Rails(1.8.6) on Jruby </li></ul><ul><li>Create Project </li></ul><ul><li>Active Records </li></ul><ul><li>Scaffolds </li></ul><ul><li>Controller </li></ul><ul><li>Active View </li></ul><ul><li>Deploy (GlassFish V3) </li></ul>
  31. 31. EJB back-end Rails Application <ul><li>Install Rails(1.8.6) on Jruby </li></ul><ul><li>Create Project </li></ul><ul><li>Active Records </li></ul><ul><li>Scaffolds </li></ul><ul><li>Controller </li></ul><ul><li>Active View </li></ul><ul><li>Deploy (GlassFish V3) </li></ul>