Akka example of 51 bank transfers

Source: Internet
Author: User

 BankAccount.scalapackage week52import akka.actor.Actorimport  Akka.event.loggingreceiveobject bankaccount {  case class deposit (amount:  BIGINT)  {    require (amount > 0)   }  case class  withdraw (Amount: bigint)  {    require (amount > 0)    }  case object done  case object failed}class bankaccount  Extends actor {  import bankaccount._  var balance = bigint ( 0)   def receive = loggingreceive {    case deposit ( Amount)  =>      balance += amount       sender ! done    case withdraw (amount)  if amount  <= balance =>      balance -= amount      sender ! Done     case _ => sender ! failed  }}
 wiretransfer.scalapackage week52import akka.actor.actorimport akka.actor.actorrefimport  akka.event.loggingreceiveobject wiretransfer {  case class transfer (from:  actorref, to: actorref, amount: bigint)   case object Done   case object failed}class wiretransfer extends actor {  import  WireTransfer._    def receive = LoggingReceive {     case transfer (From, to, amount)  =>      from  ! bankaccount.withdraw (amount)       context.become (AwaitFrom (To),  amount, sender))   }    def awaitfrom (to: ActorRef,  AMOUNT:&NBSP;BIGINT,&NBSP;CUSTOMER:&NBSP;ACTORREF): receive = loggingreceive {     case bankacCount. Done =>      to ! bankaccount.deposit (amount)        context.become (Awaitto (customer))     case bankaccount.failed  =>      customer ! Failed       Context.stop (self)   }    def awaitto (customer: actorref):  Receive  = LoggingReceive {    case BankAccount.Done =>       customer ! done      context.stop (self)    }}
 transfermain.scalapackage week52import akka.actor.actorimport akka.actor.propsimport  akka.event.LoggingReceiveclass TransferMain extends Actor {  val  Accounta = context.actorof (props[bankaccount],  "AccountA")   val accountb =  context.actorof (props[bankaccount],  "ACCOUNTB")   accounta ! bankaccount.deposit (+)   def receive = LoggingReceive {    case  Bankaccount.done => transfer (  }  def transfer) (AMOUNT:&NBSP;BIGINT):  unit = {    val transaction = context.actorof (Props[ wiretransfer],  "Transfer")     transaction ! wiretransfer.transfer (AccountA,  accountb, amount)     context.become (loggingreceive {       case wiretransfEr. DONE&NBSP;=&GT;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PRINTLN ("Success")          context.stop (self)       case wiretransfer.failed  =>        println ("Failed")          context.stop (self)     })   }}


Akka example of 51 bank transfers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.