Groovy Integration

Groovy is a good choice among languages if we’re looking for easy metaprogramming and dynamic typing while preserving Java semantics. In this section, we’ll use the Clojure STM and the Multiverse STM through Akka in Groovy Apps. To use Multiverse directly, refer to the Multiverse documentation.

Using Clojure STM in Groovy

Using Clojure STM is as simple as using instances of clojure.lang.Ref for managed references and invoking the runInTransaction method of LockingTransaction to place a piece of code in a transaction. Let’s give it a shot using the account transfer example. Create a class called Account to hold a managed reference to its immutable state, represented by the variable currentBalance:

polyglotSTM/groovy/clojure/Transfer.groovy ...

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.