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.

把鐵路開進視窗裡

2,259 views

Published on

Published in: Technology, Education
  • Be the first to comment

把鐵路開進視窗裡

  1. 1. Deploy JRuby on Rails to Windows Server 2008 R2
  2. 2. About Me• (Lu Wei Jen)• • http://www.handlino.com• Facebook: http://www.facebook.com/weijenlu• Twitter: weijenlu
  3. 3. Agenda• Why Windows• Why JRuby• Using Java’s Resources• JRoR on Windows Environment• Deployment
  4. 4. Why Windows
  5. 5. It’s All About Money
  6. 6. Q1 2010 units Q1 2010 dollarsWindows 1,379,487 (75.3%) Windows $5.1 billion (48.9%)Unix 65,451 (3.6%) Unix $2.3 billion (22.2%)Linux 380,429 (20.8%) Linux $1.7 billion (16.2%)Q4 2009 units Q4 2009 dollarsWindows 1,434,225 (73.9%) Windows $5.4 billion (41.6%)Unix 84,851 ( 4.4%) Unix $3.9 billion (29.9%)Linux 412,041 (21.2%) Linux $1.9 billion (14.7%) http://www.zdnet.com/blog/microsoft/idc-windows-server-still-rules-the-server-roost/6424
  7. 7. More Jobs
  8. 8. More JobsMore Rubiest
  9. 9. More Jobs More RubiestMore Resources
  10. 10. More Jobs More RubiestMore Resources More Money
  11. 11. Why JRuby• You can use java resources.• More easy to deploy to Windows.• True multithreading.• JRuby is just a .jar file. http://bit.ly/cPNg9f
  12. 12. Using Java Resources
  13. 13. Wrapping a Library(1)require java# create objectspdf = com.itextpdf.text.Document.newpara =com.itextpdf.text.Paragraph.newUsing Java resources from JRubyfile = java.io.FileOutputStream.newpdf_demo.pdf https://gist.github.com/e4b1d813394e58188e59
  14. 14. Wrapping a Library(2)# get_instance is an alias forgetInstancecom.itextpdf.text.pdf.PdfWriter.get_instance(pdf, file)pdf.openpdf.add parapdf.close https://gist.github.com/e4b1d813394e58188e59
  15. 15. Wrapping a Library(3)• Run • jruby -J-cp itextpdf-5.1.2.jar pdf_demo.rb
  16. 16. Classpath(1)• Environment Variable • set RUBYOPT=-IC:jrubyitext • set CLASSPATH=%CLASSPATH%C:jruby itext
  17. 17. Classpath(2)require javarequire itextpdf-5.1.2.jarpdf=com.itextpdf.text.Document.new ... https://gist.github.com/238e0aefd53420ad50bf
  18. 18. Classpath(3)• Run • jruby pdf_demo.rb
  19. 19. Loading Classes(1)Document = com.itextpdf.text.DocumentParagraph = com.itextpdf.text.Paragraph ...pdf = Document.newpara = Paragraph.new Loading Classes ... https://gist.github.com/82571dca5b9ae6fb9709
  20. 20. Loading Classes(2)java_import com.itextpdf.text.Documentjava_import com.itextpdf.text.Paragraph ...pdf = Document.newpara = Paragraph.new Loading Classes ... https://gist.github.com/3f494f48834c8db4f06d
  21. 21. Loading Classes(3)module IText include_package "com.itextpdf.text"end ...pdf = IText::Document.newpara = IText::Paragraph.new Loading... ... https://gist.github.com/da54b499d1ce8e2d38e3
  22. 22. Loading Classes(4)#for java stringjava_import "java.lang.String" do "JString"end ...data = JString.new(avoid class nameconflict)para = Paragraph.new(data) https://gist.github.com/f4acbe684e4c1d9982b1
  23. 23. Using Objects’ Static Methods(1)API: com.itextpdf.text.PageSize http://api.itextpdf.com/itext/com/itextpdf/text/PageSize.html
  24. 24. Using Objects’ Static Methods(2)#for itextjava_import com.itextpdf.text.Documentjava_import com.itextpdf.text.PageSize ...pdf = Document.new(PageSize.getRectangle("A5")) https://gist.github.com/a680fe7f47ab00967914
  25. 25. Using Objects’ Static Fieldsjava_import com.itextpdf.text.Documentjava_import com.itextpdf.text.PageSize ...pdf = Document.new(PageSize::A5) https://gist.github.com/a680fe7f47ab00967914
  26. 26. Using Objects’ constructor(1)API: com.itextpdf.text.Document http://api.itextpdf.com/itext/com/itextpdf/text/Document.html
  27. 27. Using Objects’ constructor(2)java_import com.itextpdf.text.Documentjava_import com.itextpdf.text.PageSize ...pdf = Document.newpdf = Document.new PageSize::A5pdf = Document.new(PageSize::A5, 30,10, 100, 20)
  28. 28. Get/Set Methods(1)para = Paragraph.new Get/Set methodspara.setAlignment(10)para.alignment = 10puts para.getAlignment #=> 10puts para.alignment #=> 10 https://gist.github.com/50f2d2c82f43db1a886f
  29. 29. Get/Set Methods(2)pdf = Document.newpdf.setPageSize(PageSize::A5)pdf.set_page_size(PageSize::A5)#pdf.page_size = PageSize::A5 is not workputs pdf.getPageSizeputs pdf.page_size https://gist.github.com/50f2d2c82f43db1a886f
  30. 30. Using Objects’ Instance Fields(1)// PrivateField.javapublic class PrivateField{ private int pField = 0; public PrivateField(int i){ pField = i; }} https://gist.github.com/c078f579c92e0a11fa43
  31. 31. Using Objects’ Instance Fields(2)#private_field.rbclass PrivateField field_accessor :pFieldendobj = PrivateField.new(5)puts obj.pField #=> 5obj.pField = 10puts obj.pField #=> 10 https://gist.github.com/6ae88c3a4472a5b91140
  32. 32. Data Type Conversionhttp://kenai.com/projects/jruby/pages/CallingJavaFromJRuby#Ruby_to_Java
  33. 33. Data Type Conversion (Array)public class ArrayDemo { public static String getType(Object o){ return o.getClass().getName(); }} https://gist.github.com/1172412
  34. 34. Data Type Conversion (Array)require javajava_import "ArrayDemo"puts ArrayDemo.get_type(["a", "b"])#=>org.jruby.RubyArrayputs ArrayDemo. get_type(["a", "b"].to_java)#=> [Ljava.lang.Object; https://gist.github.com/717f4e5061b8f3f042d5
  35. 35. Multithread(1)java_import java.lang.Runnablejava_import java.lang.Thread https://gist.github.com/1172409
  36. 36. Multithread(2)class GeneratePdf include Runnable def initialize @runner = Thread.current_thread end def run # Generate PDF endend
  37. 37. Multithread(3)(1..100).each do Thread.new(GeneratePdf.new).startend
  38. 38. Multithread(4)-peach require rubygems require peach (1..100).to_a.peach do |i| # Generate PDF end https://gist.github.com/1172403
  39. 39. JRoR on Windows Environment
  40. 40. Using MSSQL(1)Step1: add adapter#Gemfilegem activerecord-jdbc-adaptergem activerecord-jdbcmssql-adapter
  41. 41. Using MSSQL(2) Step2: Download sqljdbc4.jar Step3: add classpath#config/environment.rbrequire "#{Rails.root}/lib/java/sqljdbc4.jar"
  42. 42. Using MSSQL(3)Step4: config databasedevelopment: adapter: jdbc username: sa password: password driver: com.microsoft.sqlserver.jdbc.SQLServerDriver url: jdbc:sqlserver://hostname:1433;databaseName=myapp pool: 5 timeout: 5000
  43. 43. Application Server Trinidad• Simple library to run rails and rackup applications into an embedded Apache Tomcat• https://github.com/trinidad/trinidad• add gem ‘trinidad’ to Gemfile• jruby -S trinidad
  44. 44. Running Trinidad as Windows Service(1)• add gem ‘trinidad_init_services’ to Gemfile• https://github.com/trinidad/ trinidad_init_services• It’s base on Apache Commons Daemon
  45. 45. Running Trinidad as Windows Service(2)• If you are using 64-bit Server, you should replace procrun.exe to 64-bit version.• jruby -S trinidad_init_service
  46. 46. IIS Proxy to Trinidad• using ISAPI_Rewrite 3• config: RewriteEngine on RewriteBase / RewriteLog "C:myapplogrewrite.log" RewriteLogLevel 9 RewriteProxy (.*) http://localhost:3000/$1• Single server license: $99
  47. 47. Background Process Resque• redis: • https://github.com/dmajkic/redis/downloads• resque: • https://github.com/defunkt/resque• jruby" -S rake resque:workers VERBOSE=true COUNT=3 QUEUE=* RAILS_ENV=production
  48. 48. DeploymentUsing NSIS to Deploy Rails App
  49. 49. Steps• Install JRuby• Copy app to suitable folder• Batch file for install gems• Batch file for database setting• Batch files for running resque, redis
  50. 50. Install JRubySection -InstallJRuby SEC0000 SetOutPath $INSTDIR File /r jruby_windowsjre_1_6_3.exe ExecWait $INSTDIRjruby_windowsjre_1_6_3.exe -q -dir"$INSTDIRjruby"SectionEnd
  51. 51. Copy App to Suitable FolderSection -CopyMyapp SEC0001 SetOutPath $INSTDIRmyapp SetOverwrite on File /r Myapp*SectionEnd
  52. 52. Batch File for Install Gems(1)Section -CreateFileForGemInstall SEC0003 FileOpen $9 $INSTDIRMyappscriptinstall_gem.bat w FileWrite $9 @echo off$r$n FileWrite $9 CD /D "$INSTDIRMyapp"$r$n FileWrite $9 set PATH=$INSTDIRjrubybin$r$n FileWrite $9 "$INSTDIRjrubybinjruby" -S gem install"vendorcachebundler-1.0.18.gem"$r$n FileWrite $9 "$INSTDIRjrubybinjruby" -S bundleinstall --local$r$n FileClose $9SectionEnd
  53. 53. Batch File for Database SettingSection -CreateFileForDBConfig SEC0005 FileOpen $9 $INSTDIRMyappscriptconfig_db.bat w FileWrite $9 @echo off$r$n FileWrite $9 CD /D "$INSTDIRMyapp"$r$n FileWrite $9 "$INSTDIRjrubybinjruby" -S rakedb:migrate RAILS_ENV=production$r$n FileWrite $9 "$INSTDIRjrubybinjruby" -S rakedb:seed RAILS_ENV=production$r$n FileClose $9SectionEnd
  54. 54. Batch File for RedisSection -CreateFileForRedis SEC0007 FileOpen $9 $INSTDIRMyappscriptredis.bat w FileWrite $9 @echo off$r$n FileWrite $9 "$INSTDIRredisredis-server.exe"$r$n FileClose $9SectionEnd
  55. 55. Batch File for ResqueSection -CreateFileForResque SEC0008 FileOpen $9 $INSTDIRMyappscriptresque.bat w FileWrite $9 @echo off$r$n FileWrite $9 CD /D "$INSTDIRMyapp"$r$n FileWrite $9 "$INSTDIRjrubybinjruby" -S rake resque:workersVERBOSE=true COUNT=3 QUEUE=* RAILS_ENV=production$r$n FileClose $9SectionEnd
  56. 56. Redis and Resque are runningon the “Command Prompt”.hmm.....It’s not a good idea.
  57. 57. Using FireDaemon• Windows Service Creation, Management, Monitoring and Control• http://www.firedaemon.com/purchase- firedaemon.php• Single License:$49
  58. 58. Q&A

×