Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TorqueBox - Ultrapassando a fronteira entre Jav...

Avatar for abstractj abstractj
February 02, 2012

TorqueBox - Ultrapassando a fronteira entre Java e Ruby

Avatar for abstractj

abstractj

February 02, 2012
Tweet

More Decks by abstractj

Other Decks in Programming

Transcript

  1. @abstractj Java há 12 anos, Ruby n00b TorqueBox Contributor DynJS

    Contributor Caelum & ConcreteS Friday, November 4, 2011
  2. Expressiva? import java.util.Calendar; public class Beer { private Long beerId;

    private String description; private Calendar createdAt; public Long getBeerId() { return beerId; } public void setBeerId(Long beerId) { this.beerId = beerId; } public String getDescription() { return description; } public void setDescription(String description) this.description = description; } public Calendar getCreatedAt() { return createdAt; } public void setCreatedAt(Calendar createdAt) this.createdAt = createdAt; } } Friday, November 4, 2011
  3. ENTERPRISE WAR JAX-RPC Struts Spring JAAS EJB JNI EAR SOA

    JAX-WS JSF Java-FX JNDI RMI JMX JAXP JAXB JDO JAX-RS JAXR JSTL StAX JAF JDOM AWT Swing JEE JSE JCP JVM JME NIO JSP JRE POJO JavaBean EJBQL JPQL NPE Friday, November 4, 2011
  4. require 'java' pdf = com.itextpdf.text.Document.new para = com.itextpdf.text.Paragraph.new 'Brought to

    you by JRuby' file = java.io.FileOutputStream.new 'pdf_demo.pdf' com.itextpdf.text.pdf.PdfWriter.get_instance pdf, file pdf.open pdf.add para pdf.close JRuby Friday, November 4, 2011
  5. “You get true multithreading that can use all your computer’s

    cores from one process, plus a virtual machine that’s been tuned for a decade and a half.” Using JRuby - Bringing Ruby to Java Friday, November 4, 2011
  6. TorqueBox “The power of JBoss with the expressiveness of Ruby”

    Jim Crossley - MagicRuby Conference Friday, November 4, 2011
  7. ➜ wget http://torquebox.org/builds/LATEST/torquebox-dist- bin.zip ➜ unzip torquebox-dist-bin.zip ➜ export TORQUEBOX_HOME=$PWD/torquebox-2.x

    ➜ export JBOSS_HOME=$TORQUEBOX_HOME/jboss ➜ export JRUBY_HOME=$TORQUEBOX_HOME/jruby ➜ export PATH=$JRUBY_HOME/bin:$PATH Friday, November 4, 2011
  8. Gemfile source "http://torquebox.org/2x/builds/584/gem- repo" source 'http://rubygems.org' gem 'rails', '3.1.1' gem

    'activerecord-jdbcsqlite3-adapter' gem 'jruby-openssl' gem 'json' gem 'jquery-rails' gem "torquebox-rake-support", "2.x.incremental. 584" gem "torquebox", "2.x.incremental.584" Friday, November 4, 2011
  9. Caching class BeersController < ApplicationController caches_action :most_popular, :expires_in => 30.seconds

    def most_popular @popular_beers = Beer.most_popular(:limit => 25) end end Friday, November 4, 2011
  10. class TorqueBoxCommand < Thor TASK_ORDER = %w(deploy undeploy start cli

    env help) map "run" => "start" desc "run", "Run TorqueBox" method_option :clustered, :type => :boolean, :desc => "Run TorqueBox in clustered mode" def start setup_environment TorqueBox::DeployUtils.run_server(:clustered => options.clustered, :max_threads => options['max-threads'], :bind_address => options['bind-address']) end end Internals Friday, November 4, 2011
  11. Internals public class RubyJobProxy implements Job { @Override public void

    execute(JobExecutionContext context) throws JobExecutionException { Ruby ruby = null; try { ruby = this.runtimePool.borrowRuntime( resolver.getComponentName() ); JobComponent job = (JobComponent)resolver.resolve( ruby ); watchDog(context); job.run(); } catch (Exception e) { throw new JobExecutionException( e ); } finally { if (ruby != null) { this.runtimePool.returnRuntime( ruby ); } } } Friday, November 4, 2011
  12. Internals public class RubyJobProxy implements Job { @Override public void

    execute(JobExecutionContext context) throws JobExecutionException { Ruby ruby = null; try { ruby = this.runtimePool.borrowRuntime( resolver.getComponentName() ); JobComponent job = (JobComponent)resolver.resolve( ruby ); watchDog(context); job.run(); } catch (Exception e) { throw new JobExecutionException( e ); } finally { if (ruby != null) { this.runtimePool.returnRuntime( ruby ); } } } Friday, November 4, 2011
  13. ../app/jobs class JobDemo def initialize #Codigo de inicializacao end def

    run # O que você quer executar end end Friday, November 4, 2011
  14. O Ministério da Saúde adverte: NOSQL COMO MENSAGERIA PODE TRAZER

    DANOS A SUA SAÚDE E A DE SEUS FAMILIARES Friday, November 4, 2011
  15. Tasks class EmailerTask < TorqueBox::Messaging::Task def send_welcome(payload) to = "#{payload[:name]}

    <#{payload[:address]}>" # send welcome email to the user end end Friday, November 4, 2011
  16. Tasks class UserController < ApplicationController def register user = User.new(params[:user])

    EmailerTask.async(:send_welcome, :address => user.email, :name => user.name) end end Friday, November 4, 2011
  17. Processors include TorqueBox::Messaging class PrintHandler < MessageProcessor def on_message(body) puts

    "Processing #{body} of #{message}" end def configure(opts) @color = opts['color'] end end Friday, November 4, 2011
  18. Queues include TorqueBox req = Messaging::Queue.new '/queues/questions' res = Messaging::Queue.new

    '/queues/answers' Thread.new do req.publish "What time is it?" puts res.receive( :timeout => 1000 ) end Friday, November 4, 2011
  19. class EmailerTask include TorqueBox::Messaging::Backgroundable always_background :send_welcome def send_welcome(payload) to =

    "#{payload[:name]} <#{payload[:address]}>" # long running task end end Future Friday, November 4, 2011
  20. class BeerService def initialize @queue = Messaging::Queue.new(“beer”) end def start

    @queue.publish “Testing” end def stop # O que fazer quando o serviço receber um stop end end Services Friday, November 4, 2011
  21. Java package br.com.rubyconf; public class Beer { //gets e sets

    public void say(String message) { // Execução do método } } Deploy do jar na aplicação app/ models/ views/ controllers/ lib/beer.jar Friday, November 4, 2011
  22. Ruby class BeerController < ApplicationController include TorqueBox::Injectors def create beer

    = inject(br.com.rubyconf.Beer ) beer.say “Ruby is for Java” end end Friday, November 4, 2011
  23. JNDI class MyService include TorqueBox::Injectors def initialize opts={} @factory =

    inject("java:comp/env/jdbc/myDB") end end Friday, November 4, 2011
  24. Destinations class MyService include TorqueBox::Injectors def initialize opts={} @inbound =

    inject("/topic/beerpub") @outbound = inject("/queue/beer") end end Friday, November 4, 2011