$30 off During Our Annual Pro Sale. View Details »

jRuby and Ratpack

Max Mulatz
January 05, 2017

jRuby and Ratpack

Slides for my talk about jRuby and Ratpack.io at the vienna.rb Ruby Usergroup.

Max Mulatz

January 05, 2017
Tweet

More Decks by Max Mulatz

Other Decks in Programming

Transcript

  1. Let the others do
    the heavy lifting

    View Slide

  2. Let the others do
    the heavy lifting
    - boost your Ruby app with Java -

    View Slide

  3. max

    View Slide

  4. max
    ! klappradla
    " klappradla
    # klappradla.me

    View Slide

  5. View Slide

  6. View Slide

  7. View Slide

  8. limits

    View Slide


  9. GIL

    View Slide

  10. blocking IO

    View Slide

  11. View Slide

  12. non-blocking IO

    View Slide

  13. View Slide

  14. View Slide

  15. View Slide

  16. “…gain the performance and
    concurrency benefits of Java without
    writing any Java code or XML”
    -joe kutner-

    View Slide

  17. # ratpack.io

    View Slide

  18. “Ratpack is a set of Java libraries for
    building modern HTTP applications”

    View Slide

  19. “It is built on Java 8, Netty and
    reactive principles"
    # netty.io

    View Slide

  20. “It provides just enough for writing
    practical, high performance, apps.”

    View Slide

  21. code

    View Slide

  22. public class Main {
    public static void main(String... args) throws Exception {
    RatpackServer.start(server -> server
    .handlers(chain -> chain
    .get(ctx -> ctx.render("Hello World"))
    )
    );
    }
    }
    # https:/
    /ratpack.io/manual/current/launching.html#launching

    View Slide

  23. # Gemfile
    gem 'jbundler'
    # Jarfile
    jar 'io.ratpack:ratpack-core', '1.4.0'
    jar 'org.slf4j:slf4j-simple', '1.7.20'

    View Slide

  24. $ bundle install

    View Slide

  25. $ jbundle install

    View Slide

  26. require 'java'
    require 'jruby/core_ext'
    require 'bundler/setup'
    Bundler.require
    java_import 'ratpack.server.RatpackServer'
    RatpackServer.start do |server|
    server.handlers do |chain|
    chain.get do |ctx|
    ctx.render 'Hello World from Ratpack / jRuby'
    end
    end
    end
    # github.com/klappradla/jruby_ratpack_examples/hello_world

    View Slide

  27. # server.rb
    RatpackServer.start do |server|
    server.handlers do |chain|
    chain.get do |ctx|
    ctx.render 'Hello World from Ratpack / jRuby'
    end
    chain.all(Handler::Default)
    end
    end
    # handler/default.rb
    module Handler
    class Default
    def self.handle(ctx)
    ctx.render 'Nothing here, sorry ¯\_(ツ)_/¯'
    end
    end
    end
    # https:/
    /ratpack.io/manual/current/api/ratpack/handling/Handler.html

    View Slide

  28. blocking IO

    View Slide

  29. java_import 'ratpack.server.RatpackServer'
    java_import 'ratpack.exec.Blocking'
    require 'json'
    RatpackServer.start do |server|
    server.handlers do |chain|
    chain.get('music') do |ctx|
    Blocking.get do
    DB[:albums].all
    end.then do |albums|
    ctx.render(JSON.dump(albums))
    end
    end
    end
    end
    # https:/
    /ratpack.io/manual/current/async.html#performing_blocking_operations_eg_io

    View Slide

  30. # server
    RatpackServer.start do |server|
    server.handlers do |chain|
    chain.get('music', Handler::Music)
    end
    end
    # github.com/klappradla/jruby_ratpack_examples/db_example

    View Slide

  31. module Handler
    class Music
    class << self
    def handle(ctx)
    get_data.then do |data|
    render(ctx, data)
    end
    end
    private
    def get_data
    Blocking.get { DB[:albums].all }
    end
    def render(ctx, data)
    ctx.render(Jackson.json(data))
    end
    end
    end
    end
    # github.com/klappradla/jruby_ratpack_examples/db_example

    View Slide

  32. non-blocking HTTP

    View Slide

  33. class Server
    def self.run
    RatpackServer.start do |server|
    server.handlers do |chain|
    chain.all(RequestLogger.ncsa)
    chain.get('music', Handler::Music)
    chain.get('planets', Handler::Planets)
    chain.all(Handler::NotFound)
    end
    end
    end
    end
    # github.com/klappradla/jruby_ratpack_examples/http_example

    View Slide

  34. module Handler
    class Base
    def self.handle(args)
    new(*args).handle
    end
    def initialize(ctx)
    @ctx = ctx
    end
    def handle
    raise NotImplementedError
    end
    private
    attr_reader :ctx
    def response
    @response ||= ctx.get_response
    end
    end
    end
    # github.com/klappradla/jruby_ratpack_examples/http_example

    View Slide

  35. module Handler
    class Planets < Base
    URL = java.net.URI.new('http://swapi.co/api/planets')
    def handle
    call_api.then do |resp|
    render(resp)
    end
    end
    private
    def call_api
    client.get(URL)
    end
    def client
    ctx.get(HttpClient.java_class)
    end
    def render(resp)
    response.send(‘application/json;charset=UTF-8',
    resp.get_body.get_text)
    end
    end
    end
    # github.com/klappradla/jruby_ratpack_examples/http_example

    View Slide

  36. View Slide

  37. # github.com/klappradla/jruby_ratpack_examples/sinatra

    View Slide

  38. # sinatra
    Running 1m test @ http://localhost:5050
    4 threads and 100 connections
    Thread Stats Avg Stdev Max +/- Stdev
    Latency 302.15ms 417.23ms 1.99s 84.58%
    Req/Sec 122.99 63.97 380.00 66.50%
    29176 requests in 1.00m, 83.26MB read
    Socket errors: connect 0, read 0, write 0, timeout 552
    Non-2xx or 3xx responses: 9735
    Requests/sec: 485.58
    Transfer/sec: 1.39MB
    # ratpack
    Running 1m test @ http://localhost:5050
    4 threads and 100 connections
    Thread Stats Avg Stdev Max +/- Stdev
    Latency 183.24ms 255.69ms 1.97s 86.30%
    Req/Sec 328.76 141.23 830.00 68.23%
    78379 requests in 1.00m, 200.23MB read
    Socket errors: connect 0, read 0, write 0, timeout 13
    Non-2xx or 3xx responses: 26268
    Requests/sec: 1305.99
    Transfer/sec: 3.34MB
    # github.com/klappradla/jruby_ratpack_examples/benchmarks

    View Slide

  39. # github.com/klappradla/jruby_ratpack_examples/benchmarks
    # sinatra
    Running 1m test @ http://localhost:5050
    4 threads and 100 connections
    Thread Stats Avg Stdev Max +/- Stdev
    Latency 302.15ms 417.23ms 1.99s 84.58%
    Req/Sec 122.99 63.97 380.00 66.50%
    29176 requests in 1.00m, 83.26MB read
    Socket errors: connect 0, read 0, write 0, timeout 552
    Non-2xx or 3xx responses: 9735
    Requests/sec: 485.58
    Transfer/sec: 1.39MB
    # ratpack
    Running 1m test @ http://localhost:5050
    4 threads and 100 connections
    Thread Stats Avg Stdev Max +/- Stdev
    Latency 183.24ms 255.69ms 1.97s 86.30%
    Req/Sec 328.76 141.23 830.00 68.23%
    78379 requests in 1.00m, 200.23MB read
    Socket errors: connect 0, read 0, write 0, timeout 13
    Non-2xx or 3xx responses: 26268
    Requests/sec: 1305.99
    Transfer/sec: 3.34MB

    View Slide


  40. # github.com/jruby/jruby/wiki/AboutJRuby
    # github.com/klappradla/jruby_ratpack_examples
    # blog.heroku.com/reactive_ruby_building_real_time_apps_with_jruby_and_ratpack

    View Slide