Skip to content

Instantly share code, notes, and snippets.

@IainHull
Created November 25, 2013 17:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save IainHull/7645290 to your computer and use it in GitHub Desktop.
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…
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