Skip to content

Instantly share code, notes, and snippets.

@rondinif
Last active December 16, 2015 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rondinif/5486638 to your computer and use it in GitHub Desktop.
Save rondinif/5486638 to your computer and use it in GitHub Desktop.

Notes about: JRuby: Calling Java Code From A Rack App And Keeping It In Memory

The following gist are available as support to the answer: JRuby: Calling Java Code From A Rack App And Keeping It In Memory

Sinatra Deployment

rack up (WEBrick)

> rackup config.ru
[StatefulCalculator] Welcome !
INFO  WEBrick 1.3.1
INFO  ruby 1.9.2 (2012-02-22) [java]
INFO  WEBrick::HTTPServer#start: pid=2357 port=9292
0:0:0:0:0:0:0:1 - - [29/Apr/2013 13:10:51] "GET /add_one HTTP/1.1" 200 47 0.0980
0:0:0:0:0:0:0:1 - - [29/Apr/2013 13:10:51] "GET /favicon.ico HTTP/1.1" 404 490 0.0210
0:0:0:0:0:0:0:1 - - [29/Apr/2013 13:11:20] "GET /add_one HTTP/1.1" 200 47 0.0210
0:0:0:0:0:0:0:1 - - [29/Apr/2013 13:11:20] "GET /favicon.ico HTTP/1.1" 404 490 0.0180
0:0:0:0:0:0:0:1 - - [29/Apr/2013 13:11:22] "GET /add_one HTTP/1.1" 200 47 0.0150
0:0:0:0:0:0:0:1 - - [29/Apr/2013 13:11:22] "GET /favicon.ico HTTP/1.1" 404 490 0.0160
^C[2013-04-29 13:49:45] INFO  going to shutdown ...
[2013-04-29 13:49:45] INFO  WEBrick::HTTPServer#start done.

trinidad (Tomcat)

[ 01:04:38 ] > trinidad
Initializing ProtocolHandler ["http-bio-3000"]
Starting Servlet Engine: Apache Tomcat/7.0.39
jruby 1.6.7 (ruby-1.9.2-p312) (2012-02-22 3e82bc8) (Java HotSpot(TM) 64-Bit Server VM 1.7.0_15) [darwin-x86_64-java]
using 1:5 runtime pool with acquire timeout of 5.0 seconds
[StatefulCalculator] Welcome !
added application to pool, size now = 1
Starting ProtocolHandler ["http-bio-3000"]
^CPausing ProtocolHandler ["http-bio-3000"]
Stopping ProtocolHandler ["http-bio-3000"]
Destroying ProtocolHandler ["http-bio-3000"]

Use Case

Note: the http port used depends on your deployment Note: Remains specific considerations to do about thread synchronization: Sinatra will use Mutex#synchronize method to place a lock on every request to avoid race conditions among threads. If your sinatra app is multithreaded and not thread safe, or any gems you use is not thread safe, you would want to do set :lock, true so that only one request is processed at a given time. .. Otherwise by default lock is false, which means the synchronize would yield to the block directly.

source: https://github.com/zhengjia/sinatra-explained/blob/master/app/tutorial_2/tutorial_2.md

Tested by rondinif on April 29, 2013. About the env:

System Version: OS X 10.8.3 (12D78)
Kernel Version: Darwin 12.3.0
$ jruby -v
jruby 1.6.7 (ruby-1.9.2-p312) (2012-02-22 3e82bc8) (Java HotSpot(TM) 64-Bit Server VM 1.7.0_15) [darwin-x86_64-java]
$ echo $JRUBY_OPTS
--1.9

LOCAL GEMS

bouncy-castle-java (1.5.0147, 1.5.0146.1) bundler (1.3.5) daemons (1.1.9) eventmachine (1.0.3 java) jruby-jars (1.7.3) jruby-launcher (1.0.15 java) jruby-openssl (0.8.7, 0.7.7) jruby-rack (1.1.13.2) rack (1.5.2) rack-protection (1.5.0) rake (10.0.4, 0.8.7) rubygems-bundler (1.1.1) rubyzip (0.9.9) rvm (1.11.3.7) sinatra (1.4.2) tilt (1.3.7) trinidad (1.4.4) trinidad_jars (1.2.3) warbler (1.3.6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment