Created
April 7, 2014 16:49
-
-
Save adilakhter/10023963 to your computer and use it in GitHub Desktop.
eventstream demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Message and event classes | |
| case class UpdateAccountBalance(userId:Long, amount:Long) | |
| case class BalanceUpdated(userId:Long) | |
| //Actor that performs account updates | |
| class AccountManager extends Actor{ | |
| val dao = new AccountManagerDao | |
| def receive = { | |
| case UpdateAccountBalance(userId, amount) => | |
| val res = for(result <- dao.updateBalance(userId, amount)) yield{ | |
| context.system.eventStream.publish(BalanceUpdated(userId)) | |
| result | |
| } | |
| sender ! res | |
| } | |
| } | |
| //Actor that manages a cache of account balance data | |
| class AccountCacher extends Actor{ | |
| val cache = new AccountCache | |
| override def preStart = { | |
| context.system.eventStream.subscribe(context.self, classOf[BalanceUpdated]) | |
| } | |
| def receive = { | |
| case BalanceUpdated(userId) => | |
| cache.remove(userId) | |
| } | |
| } | |
| //Actor that checks balance after an update to warn of low balance | |
| class LowBalanceChecker extends Actor{ | |
| val dao = new LowBalanceDao | |
| override def preStart = { | |
| context.system.eventStream.subscribe(context.self, classOf[BalanceUpdated]) | |
| } | |
| def receive = { | |
| case BalanceUpdated(userId) => | |
| for{ | |
| balance <- dao.getBalance(userId) | |
| theshold <- dao.getBalanceThreshold(userId) | |
| if (balance < threshold) | |
| }{ | |
| sendBalanceEmail(userId, balance) | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example taken from:
[1] http://stackoverflow.com/questions/16267616/akka-event-bus-tutorial
Read more:
[2] http://doc.akka.io/api/akka/2.0/akka/event/EventStream.html