From 65e553a4dfa67a7f5f508423e74d5c698fd17579 Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Thu, 28 Apr 2011 08:19:04 +0200 Subject: [PATCH] Added sample to Transactional Agents --- akka-docs/scala/agents.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/akka-docs/scala/agents.rst b/akka-docs/scala/agents.rst index 1e9ea128a3..dc62000995 100644 --- a/akka-docs/scala/agents.rst +++ b/akka-docs/scala/agents.rst @@ -92,6 +92,29 @@ Transactional Agents If an Agent is used within an enclosing transaction, then it will participate in that transaction. If you send to an Agent within a transaction then the dispatch to the Agent will be held until that transaction commits, and discarded if the transaction is aborted. +.. code-block:: scala + + import akka.agent.Agent + import akka.stm._ + + def transfer(from: Agent[Int], to: Agent[Int], amount: Int): Boolean = { + atomic { + if (from.get < amount) false + else { + from send (_ - amount) + to send (_ + amount) + true + } + } + } + + val from = Agent(100) + val to = Agent(20) + val ok = transfer(from, to, 50) + + from() // -> 50 + to() // -> 70 + Monadic usage -------------