Class: Rack::JettyRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/jetty_standalone/jetty_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ JettyRunner

Returns a new instance of JettyRunner.



28
29
30
# File 'lib/rack/jetty_standalone/jetty_runner.rb', line 28

def initialize(opts = {})
  @options = opts
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/rack/jetty_standalone/jetty_runner.rb', line 26

def options
  @options
end

Instance Method Details

#destroyObject



128
129
130
# File 'lib/rack/jetty_standalone/jetty_runner.rb', line 128

def destroy()
  @jetty && @jetty.destroy
end

#joinObject



112
113
114
# File 'lib/rack/jetty_standalone/jetty_runner.rb', line 112

def join
  @jetty.join
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rack/jetty_standalone/jetty_runner.rb', line 32

def run()
  thread_pool = QueuedThreadPool.new
  thread_pool.setMinThreads((options[:min_threads] || 10).to_i)
  thread_pool.setMaxThreads((options[:max_threads] || 200).to_i)
  thread_pool.setLowThreads((options[:low_threads] || 50).to_i)
  thread_pool.setSpawnOrShrinkAt(2)

  @jetty = Java::org.mortbay.jetty.Server.new
  @jetty.setThreadPool(thread_pool)
  @jetty.setGracefulShutdown(1000)

  stats_on = options[:with_stats] || true

  if options[:use_nio] || true
    http_connector = SelectChannelConnector.new
    http_connector.setLowResourcesConnections(20000)
  else
    http_connector = SocketConnector.new
  end
  http_connector.setHost(options[:host] || 'localhost')
  http_connector.setPort(options[:port].to_i)
  http_connector.setMaxIdleTime(30000)
  http_connector.setAcceptors(2)
  http_connector.setStatsOn(stats_on)
  http_connector.setLowResourceMaxIdleTime(5000)
  http_connector.setAcceptQueueSize((options[:accept_queue_size] || thread_pool.getMaxThreads).to_i)
  http_connector.setName("HttpListener")
  @jetty.addConnector(http_connector)

  if options[:ssl_port] && options[:keystore] && options[:key_password]
    https_connector = SslSocketConnector.new

    https_connector.setKeystore(options[:keystore])
    https_connector.setKeystoreType(options[:keystore_type] || 'JKS')
    https_connector.setKeyPassword(options[:key_password])
    https_connector.setHost(http_connector.getHost)
    https_connector.setPort(options[:ssl_port].to_i)
    https_connector.setMaxIdleTime(30000)
    https_connector.setAcceptors(2)
    https_connector.setStatsOn(stats_on)
    https_connector.setLowResourceMaxIdleTime(5000)
    https_connector.setAcceptQueueSize(http_connector.getAcceptQueueSize)
    https_connector.setName("HttpsListener")
    @jetty.addConnector(http_connector)
  end

  contextHandlers = ContextHandlerCollection.new

  root = Context.new(contextHandlers, "/", Context::NO_SESSIONS)
  root.set_init_params(options)
  root.set_resource_base(options[:resource_base] || '.')
  root.add_filter(FilterHolder.new(RackFilter.new), "/*", Handler::DEFAULT)
  root.add_event_listener(RackServletContextListener.new)
  root.add_servlet(ServletHolder.new(DefaultServlet.new), "/")

  handlers = HandlerCollection.new
  handlers.addHandler(contextHandlers)
  handlers.addHandler(DefaultHandler.new)

  if options[:request_log] || options[:request_log_path]
    request_log_handler = RequestLogHandler.new

    request_log_handler.setRequestLog(options[:request_log] || NCSARequestLog.new(options[:request_log_path]))
    handlers.addHandler(request_log_handler)
  end
  if stats_on
    mbean_container = MBeanContainer.new(ManagementFactory.getPlatformMBeanServer)

    @jetty.getContainer.addEventListener(mbean_container)
    mbean_container.start

    stats_handler = StatisticsHandler.new
    stats_handler.addHandler(handlers)
    @jetty.addHandler(stats_handler)
  else
    @jetty.addHandler(handlers)
  end
  @jetty.start
end

#running?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/rack/jetty_standalone/jetty_runner.rb', line 116

def running?
  @jetty && @jetty.isStarted
end

#stopObject



124
125
126
# File 'lib/rack/jetty_standalone/jetty_runner.rb', line 124

def stop()
  @jetty && @jetty.stop
end

#stopped?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/rack/jetty_standalone/jetty_runner.rb', line 120

def stopped?
  !@jetty || @jetty.isStopped
end