SavingsBankAccount程序中Scala的Actors的用例

时间:2013-08-29 17:20:30

标签: scala concurrency akka actor

这不是任何形式的任务。我正在使用Akka Actors学习Scala和Scala并发。我这样做的一种方法是提出一些简单的程序设计,并试图提出一个涉及应用于设计的Actors / concurrency的实现:

我想写的程序是储蓄银行账户应用程序,它有一个输入组件和一个输出组件。
  Bank XYZ向客户提供开立储蓄账户的计划  
1)客户ABC在他/她开户时要求提供他的个人信息  
2)客户可以将钱存入他/她的账户  
3)客户可以从他/她的账户中提取资金
4)如果账户余额低于100美元,则通知客户。 (例如,当客户从应用程序的输入屏幕中选择选项3时,他/她被告知当时的帐户余额低于100美元,因此无法提取,除非他们存入更多的钱。)   
5)应用程序应该能够维护客户的交易历史记录并显示交易历史记录(例如:当应用程序显示输入屏幕时,其中一个选项是 - 显示交易历史记录。选择此选项时在提示时显示一个数字,应用程序应显示一个Balance)  
该程序的输入部分包括命令行中显示的4个选项:1。创建帐户2.存款3.提取资金。 4.显示交易历史


现在,我现在想要完成的是理解和识别:
1)在这样的程序中,Akka actors / Scala并发库的用例是什么?如果有任何可以作为此应用程序中的并发角色怎么办?
我可以最好地利用哪些Scala集合库?

1 个答案:

答案 0 :(得分:0)

我希望这能解决问题: http://alexminnaar.com/introduction-to-the-multithreading-problem-and-the-akka-actor-solution.html

注意:我刚注意到这篇文章已经过时了。我在2018年发帖回答。

Update1 :(链接说明,在评论中提到)

这是多种可能策略中的一种方法。

使用了两个演员

  1. BankAccount - 管理余额

        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 - 将余额从一个帐户转移到另一个帐户。

        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)
        }
        }