https://medium.com/swlh/actors-using-scala-akka-part-2-askpattern-and-spawnprotocol-3942e45a5fcc
for {
bankAccount <- bankAccountFuture
balance1 <- bankAccount ? GetBalance
_ = println(balance1)
_ = bankAccount ! Deposit(200)
_ = bankAccount ! Withdraw(50)
balance2 <- bankAccount ? GetBalance
_ = println(balance2)
} yield ()
If this is intended to model transactions in a sequential stepwise fashion (e.g., deposit 200 then withdraw 50), this example is perhaps misleading.
I believe the order of sending (fire-and-forget/tell) messages in that map
call are not necessarily the order that the recieving Actor
would be processing the messages.
https://doc.akka.io/docs/akka/current/typed/interaction-patterns.html#fire-and-forget
To make the processing ordered, it would be necessary to use an ask
and modify the message protocol to have return "response" message or status.
Something along the lines of this:
val myFut = for {
bankAccount <- bankAccountFuture
balance1 <- bankAccount ? GetBalance
msg1 <- bankAccount ? Deposit(200)
msg2 <- bankAccount ? Withdraw(50)
finalBalance <- bankAccount ? GetBalance
} yield finalBalance
Also, the actorSystem.terminate()
call in your example can exit before all the futures have been processed. I suspect this needs an Await.result(myFut, Duration.Inf)
call before the terminate()
call of the actor system.