Skip to content

Instantly share code, notes, and snippets.

@ericacm
Last active May 15, 2016 10:33
Show Gist options
  • Save ericacm/7234947 to your computer and use it in GitHub Desktop.
Save ericacm/7234947 to your computer and use it in GitHub Desktop.
Stackable actor traits See http://www.slideshare.net/EvanChan2/akka-inproductionpnw-scala2013 for more examples
trait ActorStack extends Actor {
/** Actor classes should implement this partialFunction for standard
* actor message handling
*/
def wrappedReceive: Receive
/** Stackable traits should override and call super.receive(x) for
* stacking functionality
*/
def receive: Receive = {
case x => if (wrappedReceive.isDefinedAt(x)) wrappedReceive(x) else unhandled(x)
}
}
trait Instrument1 extends ActorStack {
override def receive: Receive = {
case x =>
println("Do something before...")
super.receive(x)
println("Do something after...")
}
}
class DummyActor extends Actor with Instrument1 with Instrument2 {
def wrappedReceive = {
case "something" => println("Got something")
case x => println("Got something else: " + x)
}
}
trait Slf4jLogging extends Actor with ActorStack {
val logger = LoggerFactory.getLogger(getClass)
private[this] val myPath = self.path.toString
logger.info("Starting actor " + getClass.getName)
override def receive: Receive = {
case x =>
org.slf4j.MDC.put("akkaSource", myPath)
super.receive(x)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment