JRuby Integration

JRuby brings the power of Ruby along with its elegance, expressiveness, and conciseness to the Java platform. In this section, we’ll use Clojure STM and Multiverse STM through Akka in JRuby code. If you’d like to use Multiverse directly, refer to the Multiverse documentation for the JRuby integration API.

Using Clojure STM in JRuby

To use Clojure STM from JRuby, we’ll use clojure.lang.Ref for managed references and the runInTransaction method of LockingTransaction for the transaction. Let’s give that a try using the account transfer example. Create a class called Account that will hold a managed reference to its immutable state represented by the JRuby field @balance:

polyglotSTM/jruby/clojure/account.rb
 
require ​'java'

Get Programming Concurrency on the JVM now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.