akka.transactor
Interface Transactor

All Superinterfaces:
Actor

public interface Transactor
extends Actor

An actor with built-in support for coordinated transactions.

Transactors implement the general pattern for using Coordinated where coordination messages are sent to other transactors then the coordinated transaction is entered. Transactors can also accept explicitly sent Coordinated messages.

Simple transactors will just implement the atomically method which is similar to the actor receive method but runs within a coordinated transaction.

Example of a simple transactor that will join a coordinated transaction:


 class Counter extends Transactor {
   val count = Ref(0)

   def atomically = implicit txn => {
     case Increment => count transform (_ + 1)
   }
 }
 

To coordinate with other transactors override the coordinate method. The coordinate method maps a message to a set of SendTo objects, pairs of ActorRef and a message. You can use the include and sendTo methods to easily coordinate with other transactors. The include method will send on the same message that was received to other transactors. The sendTo method allows you to specify both the actor to send to, and message to send.

Example of coordinating an increment:


 class FriendlyCounter(friend: ActorRef) extends Transactor {
   val count = Ref(0)

   override def coordinate = {
     case Increment => include(friend)
   }

   def atomically = implicit txn => {
     case Increment => count transform (_ + 1)
   }
 }
 

Using include to include more than one transactor:


 override def coordinate = {
   case Message => include(actor1, actor2, actor3)
 }
 

Using sendTo to coordinate transactions but send on a different message than the one that was received:


 override def coordinate = {
   case Message => sendTo(someActor -> SomeOtherMessage)
   case SomeMessage => sendTo(actor1 -> Message1, actor2 -> Message2)
 }
 

To execute directly before or after the coordinated transaction, override the before and after methods. These methods also expect partial functions like the receive method. They do not execute within the transaction.

To completely bypass coordinated transactions override the normally method. Any message matched by normally will not be matched by the other methods, and will not be involved in coordinated transactions. In this method you can implement normal actor behavior, or use the normal STM atomic for local transactions.

See Also:
Coordinated

Nested Class Summary
 
Nested classes/interfaces inherited from interface akka.actor.Actor
Actor.emptyBehavior$
 
Method Summary
 scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> after()
          A Receive block that runs after the coordinated transaction.
 scala.PartialFunction<java.lang.Object,scala.collection.immutable.Set<SendTo>> alone()
          Default coordination - no other transactors.
 scala.Function1<scala.concurrent.stm.InTxn,scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit>> atomically()
          The Receive block to run inside the coordinated transaction.
 scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> before()
          A Receive block that runs before the coordinated transaction is entered.
 scala.PartialFunction<java.lang.Object,scala.collection.immutable.Set<SendTo>> coordinate()
          Override this method to coordinate with other transactors.
 scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> doNothing()
          Default catch-all for the different Receive methods.
 scala.collection.immutable.Set<SendTo> include(scala.collection.Seq<ActorRef> actors)
          Include other actors in this coordinated transaction and send them the same message as received.
 scala.collection.immutable.Set<SendTo> nobody()
          Empty set of transactors to send to.
 scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> normally()
          Bypass transactionality and behave like a normal actor.
 scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> receive()
          Implement a general pattern for using coordinated transactions.
 scala.collection.immutable.Set<SendTo> sendTo(scala.collection.Seq<scala.Tuple2<ActorRef,java.lang.Object>> pairs)
          Include other actors in this coordinated transaction and specify the message to send by providing ActorRef -> Message pairs.
 TransactorSettings settings()
           
 
Methods inherited from interface akka.actor.Actor
aroundPostRestart, aroundPostStop, aroundPreRestart, aroundPreStart, aroundReceive, context, postRestart, postStop, preRestart, preStart, self, sender, supervisorStrategy, unhandled
 

Method Detail

settings

TransactorSettings settings()

receive

scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> receive()
Implement a general pattern for using coordinated transactions.

Specified by:
receive in interface Actor
Returns:
(undocumented)

coordinate

scala.PartialFunction<java.lang.Object,scala.collection.immutable.Set<SendTo>> coordinate()
Override this method to coordinate with other transactors. The other transactors are added to the coordinated transaction barrier and sent a Coordinated message. The message to send can be specified or otherwise the same message as received is sent. Use the 'include' and 'sendTo' methods to easily create the set of transactors to be involved.

Returns:
(undocumented)

alone

scala.PartialFunction<java.lang.Object,scala.collection.immutable.Set<SendTo>> alone()
Default coordination - no other transactors.

Returns:
(undocumented)

nobody

scala.collection.immutable.Set<SendTo> nobody()
Empty set of transactors to send to.

Returns:
(undocumented)

include

scala.collection.immutable.Set<SendTo> include(scala.collection.Seq<ActorRef> actors)
Include other actors in this coordinated transaction and send them the same message as received. Use as the result in 'coordinated'.

Parameters:
actors - (undocumented)
Returns:
(undocumented)

sendTo

scala.collection.immutable.Set<SendTo> sendTo(scala.collection.Seq<scala.Tuple2<ActorRef,java.lang.Object>> pairs)
Include other actors in this coordinated transaction and specify the message to send by providing ActorRef -> Message pairs. Use as the result in 'coordinated'.

Parameters:
pairs - (undocumented)
Returns:
(undocumented)

before

scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> before()
A Receive block that runs before the coordinated transaction is entered.

Returns:
(undocumented)

atomically

scala.Function1<scala.concurrent.stm.InTxn,scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit>> atomically()
The Receive block to run inside the coordinated transaction. This is a function from InTxn to Receive block.

For example:


 def atomically = implicit txn => {
   case Increment => count transform (_ + 1)
 }
 

Returns:
(undocumented)

after

scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> after()
A Receive block that runs after the coordinated transaction.

Returns:
(undocumented)

normally

scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> normally()
Bypass transactionality and behave like a normal actor.

Returns:
(undocumented)

doNothing

scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> doNothing()
Default catch-all for the different Receive methods.

Returns:
(undocumented)