jruby / jruby-rack

Rack for JRuby and Java appservers
MIT License
397 stars 136 forks source link
deployment jruby jruby-rack rack-application rack-environment servlets tomcat warbler

JRuby-Rack

JRuby-Rack is a lightweight adapter for the Java Servlet environment that allows any (Ruby) Rack-based application to run unmodified in a Java Servlet container. JRuby-Rack supports Rails as well as any Rack-compatible Ruby web framework.

For more information on Rack, visit http://rack.github.io/.

This README (master) targets JRuby-Rack 1.2 (unreleased) please use the 1.1-stable branch for current stable 1.1.x releases.

Gem Version Build Status

Compatibility

JRuby-Rack 1.1.x aims to be compatible with JRuby >= 1.6.4 (we recommend 1.7.x), Generally, any container that supports Java Servlet >= 2.5 (JEE 5) is supported.

JRuby-Rack 1.2.x is expected to officially support JRuby >= 1.7.10 and will be compiled against the Java Servlet 3.0 API.

Getting Started

The most-common way to use JRuby-Rack with a Java server is to get Warbler.

Warbler depends on the latest version of JRuby-Rack and ensures it gets placed in your WAR file when it gets built.

If you're assembling your own WAR using other means, you can install the jruby-rack gem. It provides a method to locate the jar file:

require 'jruby-rack'
FileUtils.cp JRubyJars.jruby_rack_jar_path, '.'

Otherwise you'll need to download the latest jar release, drop it into the WEB-INF/lib directory and configure the RackFilter in your application's web.xml (see following examples).

Alternatively you can use a server built upon JRuby-Rack such as Trinidad with sensible defaults, without the need to configure a deployment descriptor.

Rails

Here's sample web.xml configuration for Rails. Note the environment and min/max runtime parameters. For multi-threaded (a.k.a. threadsafe!) Rails with a single runtime, set min/max both to 1. Otherwise, define the size of the runtime pool as you wish.

<context-param>
  <param-name>rails.env</param-name>
  <param-value>production</param-value>
</context-param>
<context-param>
  <param-name>jruby.min.runtimes</param-name>
  <param-value>1</param-value>
</context-param>
<context-param>
  <param-name>jruby.max.runtimes</param-name>
  <param-value>1</param-value>
</context-param>

<filter>
  <filter-name>RackFilter</filter-name>
  <filter-class>org.jruby.rack.RackFilter</filter-class>
  <!-- optional filter configuration init-params : -->
  <init-param>
    <param-name>resetUnhandledResponse</param-name>
    <param-value>true</param-value> <!-- true (default), false or buffer -->
  </init-param>
  <init-param>
    <param-name>addsHtmlToPathInfo</param-name>
    <param-value>true</param-value> <!-- true (default), false -->
  </init-param>
  <init-param>
    <param-name>verifiesHtmlResource</param-name>
    <param-value>false</param-value> <!-- true, false (default) -->
  </init-param>
</filter>
<filter-mapping>
  <filter-name>RackFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
  <listener-class>org.jruby.rack.rails.RailsServletContextListener</listener-class>
</listener>

(Other) Rack Applications

The main difference when using a non-Rails Rack application is that JRuby-Rack looks for a "rackup" file named config.ru in WEB-INF/config.ru or WEB-INF/*/config.ru. Here's a sample web.xml configuration :

<filter>
  <filter-name>RackFilter</filter-name>
  <filter-class>org.jruby.rack.RackFilter</filter-class>
  <!-- optional filter configuration init-params (@see above) -->
</filter>
<filter-mapping>
  <filter-name>RackFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
  <listener-class>org.jruby.rack.RackServletContextListener</listener-class>
</listener>

If you don't have a config.ru or don't want to include it in your web app, you can embed it directly in the web.xml as follows (using Sinatra as an example):

<context-param>
  <param-name>rackup</param-name>
  <param-value>
    require 'rubygems'
    gem 'sinatra', '~&gt; 1.3'
    require './lib/app'
    set :run, false
    set :environment, :production
    run Sinatra::Application
  </param-value>
</context-param>

Be sure to escape angle-brackets for XML !

Servlet Filter

JRuby-Rack's main mode of operation is as a filter. This allows requests for static content to pass through and be served by the application server. Dynamic requests only happen for URLs that don't have a corresponding file, much like many Ruby/Rack applications expect. The (default) filter we recommend using is org.jruby.rack.RackFilter, the filter supports the following (optional) init-params:

The application can also be configured to dispatch through a servlet instead of a filter, the servlet class name is org.jruby.rack.RackServlet.

Servlet Environment Integration

Rails

Several aspects of Rails are automatically set up for you.

JRuby Runtime Management

JRuby runtime management and pooling is done automatically by the framework. In the case of Rails, runtimes are pooled by default (the default will most likely change with the adoption of Rails 4.0). For other Rack applications a single shared runtime is created and shared for every request by default. As of 1.1.9 if jruby.min.runtimes and jruby.max.runtimes values are specified pooling is supported for plain Rack applications as well.

We do recommend to boot your runtimes up-front to avoid the cost of initializing one while a request kicks in and find the pool empty, this can be easily avoided by setting jruby.min.runtimes equal to jruby.max.runtimes. You might also want to consider tuning the jruby.runtime.acquire.timeout parameter to not wait too long when all (max) runtimes from the pool are busy.

JRuby-Rack Configuration

JRuby-Rack can be configured by setting these key value pairs either as context init parameters in web.xml or as VM-wide system properties.

Initialization

There are often cases where you need to perform custom initialization of the Ruby environment before booting the application. You can create a file called META-INF/init.rb or WEB-INF/init.rb inside the war file for this purpose. These files, if found, will be evaluated before booting the Rack environment, allowing you to set environment variables, load scripts, etc.

For plain Rack applications, JRuby-Rack also supports a magic comment to solve the "rackup" chicken-egg problem (you need Rack's builder loaded before loading the config.ru, yet you may want to setup the gem version from within the rackup file). As we ship with the Rack gem bundled, otherwise when executing the provided config.ru the bundled (latest) version of Rack will get loaded.

Use rack.version to specify the Rack gem version to be loaded before rackup :

# encoding: UTF-8
# rack.version: ~>1.3.6 (before code is loaded gem '~>1.3.6' will be called)

Or the equivalent of doing bundle exec rackup ... if you're using Bundler :

# rack.version: bundler (requires 'bundler/setup' before loading the script)

Logging

JRuby-Rack sets up a delegate logger for Rails that sends logging output to javax.servlet.ServletContext#log by default. If you wish to use a different logging system, configure jruby.rack.logging as follows:

For those loggers that require a specific named logger, set it with the jruby.rack.logging.name option, by default "jruby.rack" name will be used.

Building

Checkout the JRuby-Rack code using git :

git clone git://github.com/jruby/jruby-rack.git
cd jruby-rack

Ensure you have Maven installed. It is required for downloading jar artifacts that JRuby-Rack depends on.

Build the .jar using Maven :

mvn install

the generated jar should be located at *target/jruby-rack-.jar**

Alternatively use Rake, e.g. to build the gem (skipping specs) :

jruby -S rake clean gem SKIP_SPECS=true

You can not use JRuby-Rack with Bundler directly from the git (or http) URL (gem 'jruby-rack', :github => 'jruby/jruby-rack') since the included .jar file is compiled and generated on-demand during the build (it would require us to package and push the .jar every time a commit changes a source file).

Releasing

Support

Please use github to file bugs, patches and/or pull requests. More information at the wiki or ask us at #jruby's IRC channel.