The following gist are available as support to the answer: JRuby: Calling Java Code From A Rack App And Keeping It In Memory
- StatelessCalculator.java a class used by StatefullCalculator
- StatefulCalculator.java the class shared
- sample_app.rb the sinatra app
- Gemfile This file is used by
$ bundle install
- .rvmrc if you use
rvm
to have a sandboxed gemset and a well defined jruby env.
> 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.
[ 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"]
- to see the value of the shared register ( the register is defined in the java source code ) -=> http://localhost:3000/
- to call a calculation and change the shared register -=> http://localhost:3000/add_one
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
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
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)