Created
November 25, 2013 17:41
-
-
Save IainHull/7645290 to your computer and use it in GitHub Desktop.
Applying Akka's recommended practices for actor creation (See http://doc.akka.io/docs/akka/snapshot/scala/actors.html#Recommended_Practices) to the Cake pattern example taken from answer to this question on StackOverflow (See http://stackoverflow.com/questions/15996098/akka-and-cake-pattern). I have added the props method to the `ServiceActor` o…
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
trait DBComponent { | |
def db: DB | |
type K | |
type V | |
trait DB { | |
def put(key: K, value: V): Unit | |
def get(key: K): Option[V] | |
} | |
} | |
trait ServiceComponent { | |
thisComponent: DBComponent => | |
object ServiceActor { | |
case class Put(key: K, value: V) | |
case class Get(key: K) | |
def props: Props = Props(classOf(ServiceActor), thisComponent) | |
} | |
class ServiceActor { | |
import ServiceActor._ | |
def receive = { | |
case Put(k, v) => db.put(k, v) // db is in scope | |
case Get(k) => sender ! db.get(k) | |
} | |
} | |
} | |
object AkkaCakeApp extends App with ServiceComponent with DBComponentImpl { | |
val system = ActorSystem("helloakka") | |
val serviceActor = system.actorOf(ServiceActor.props) | |
serviceActor ! ServiceActor.Put(key, value) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment