score:0

I hope this solves the problem: http://alexminnaar.com/introduction-to-the-multithreading-problem-and-the-akka-actor-solution.html

Note: I just noticed that this post is old. I am posting answer in 2018.

Update1: (Explanation from link, after mentioned in comments)

This is one approach among multiple possible strategies.

There are two Actors used

  1. BankAccount - To manage balance

        object WireTransfer {
    
        case class Transfer(from: ActorRef, to: ActorRef, amount: BigInt)
    
        case object Done
    
        case object Failed
    
        }
    
        //actor implementing the actions of a wire transfer between two bank account actors
        class WireTransfer extends Actor {
    
        import WireTransfer._
    
        def receive = LoggingReceive {
        //If Transfer message is received, send withdraw message to 'from' and wait for reply
        case Transfer(from, to, amount) =>
        from ! BankAccount.Withdraw(amount)
        context.become(awaitFrom(to, amount, sender))
        }
    
        //If Withdraw was successful, send deposit to other bank account actor, or else give them a failure message
        def awaitFrom(to: ActorRef, amount: BigInt, customer: ActorRef): Receive = LoggingReceive {
        case BankAccount.Done =>
        to ! BankAccount.Deposit(amount)
        context.become(awaitTo(customer))
        case BankAccount.Failed =>
        customer ! Failed
        context.stop(self)
        }
    
        //If deposit was successful, send 'Done' to original actor that sent Transfer message
        def awaitTo(customer: ActorRef): Receive = LoggingReceive {
        case BankAccount.Done =>
        customer ! Done
        context.stop(self)
        }
        }
    
  2. WireTransfer - To transfer the balance from one account to another.

        object WireTransfer {
    
        case class Transfer(from: ActorRef, to: ActorRef, amount: BigInt)
    
        case object Done
    
        case object Failed
    
        }
    
        //actor implementing the actions of a wire transfer between two bank account actors
        class WireTransfer extends Actor {
    
        import WireTransfer._
    
        def receive = LoggingReceive {
        //If Transfer message is received, send withdraw message to 'from' and wait for reply
        case Transfer(from, to, amount) =>
        from ! BankAccount.Withdraw(amount)
        context.become(awaitFrom(to, amount, sender))
        }
    
        //If Withdraw was successful, send deposit to other bank account actor, or else give them a failure message
        def awaitFrom(to: ActorRef, amount: BigInt, customer: ActorRef): Receive = LoggingReceive {
        case BankAccount.Done =>
        to ! BankAccount.Deposit(amount)
        context.become(awaitTo(customer))
        case BankAccount.Failed =>
        customer ! Failed
        context.stop(self)
        }
    
        //If deposit was successful, send 'Done' to original actor that sent Transfer message
        def awaitTo(customer: ActorRef): Receive = LoggingReceive {
        case BankAccount.Done =>
        customer ! Done
        context.stop(self)
        }
        }
    

Related Query

More Query from same tag