I have to actor classes which looks similar to this form:
class ActorSupervisorOne(prop: Prop) extends Actor {
val dbSuper = context.actorOf(prop)
val subActor = context.actorOf(Props(new SubActorClass(dbSuper) with **SomeHandlersOne**))
def receive = {
case msg =>
subActor forward msg
}
}
class ActorSupervisorTwo(prop: Prop) extends Actor {
val dbSuper = context.actorOf(prop)
val subActor = context.actorOf(Props(new SubActorClass(dbSuper) with **SomeHandlersTwo**))
def receive = {
case msg =>
subActor forward msg
}
}
The only difference between them in mixing trait. Abstract it with type parameter or abstract type member won't work. I've tried the following solution, but it looks ugly and still have code duplication:
abstract class Super extends Actor {
_: {
val handler: Props
} =>
lazy val actor = context.actorOf(handler)
def receive = {
case msg =>
actor forward msg
}
}
class ActorSupervisorOne(val dbSuper: ActorRef) extends Super {
val handler = Props(new SubActorClass(dbSuper) with SomeHandlersOne)
actor
}
class ActorSupervisorTwo(val dbSuper: ActorRef) extends Super {
val handler = Props(new SubActorClass(dbSuper) with SomeHandlersTwo)
actor
}
But in this case i have to call actor to initialize it correctly or it won't work. Are there any other solution how this can be reduced?
You could probably use reflection to choose the SomeHandlersXYZ at runtime, but if you don't want to resort to reflection then I don't think that there is a way of achieving what you want without at least duplicating the subactor instantiation code (see this answer of mine for an explanation). You could do it the following way (sketched), where you basically pass in a factory function:
class ActorSupervisor(prop: Prop, getSubActor: Actor => SubActorClass) extends Actor {
val dbSuper = context.actorOf(prop)
val subActor = context.actorOf(Props(getSubActor(dbSuper)))
def receive = {
case msg =>
subActor forward msg
}
}
val asOne = new ActorSupervisor(..., a => new SubActorClass(a) with SomeHandlersOne)
val asTwo = new ActorSupervisor(..., a => new SubActorClass(a) with SomeHandlersTwo)
What about this solution:
class ActorSupervisor(subActor: => Actor) extends Actor {
val actor = context.actorOf(Props(statsProps))
def receive = {
case msg =>
actor forward msg
}
}
and then, like in Malte Schwerhoff you can create new actor like this:
val first = new ActorSupervisor(new SubActorClass(...) with SomeHandlersOne)
I think that more elegant colution can be achived with macros, but i'm not good at them
You can pass the puck on onReceive.
class ClassMixed(params: Options*)
extends BaseClass(params: _*)
with Mixin {
override def receive =
mixinReceive orElse receive
}
where Mixin has a method called mixinReceive and BaseClass overrides receive
Related
I have a actor like this:
class MyActor[T] extends Actor {
def receive = {
case Option1(t: T) =>
doWork(t) onComplete .....
case Option2 =>
}
def doWork(T): Future[T]{
}
}
Then I have an actor that inherits from the above:
class OtherActor extends MyActor {
val m = mutable.Map.empty[Int, User]
override def doWork(..) = {
//
}
}
Now in my OtherActor actor I want to add some methods to the receive method, how can I do this?
You can define the additional behavior in a Receive block inside OuterActor and chain that behavior to its parent's behavior with orElse:
class OtherActor extends MyActor {
val m = mutable.Map.empty[Int, User]
override def doWork(...) = ???
val otherBehavior: Receive = {
case ...
}
override def receive = otherBehavior.orElse(super.receive)
}
This is possible because Receive is just a type alias for PartialFunction[Any, Unit]. More information on composing actor behaviors is found here.
As a side note, you should prefer var m = immutable.Map.empty[Int, User] instead of val m = mutable.Map.empty[Int, User] in order to help avoid exposing the actor's state, as described in this answer.
class HomeController #Inject (implicit actorSystem:ActorSystem, materializer :Materializer) extends Controller
{
case class sendMsg(msg:JsValue)
class MYWSACTOR(out:ActorRef,mainActor:ActorRef) extends Actor
{
def receive =
{
case msg : JsValue =>
mainActor ! sendMsg(msg)
case sendMsg(msg) =>
{
/* validation part */
out ! msg
}
}
}
}
val mainActor = actorSystem.actorOf(Props[MYWSACTOR],"mainActor")
def object MYWSACTOR
{
def props(out:ActorRef) = Props(new MYWSACTOR(out,mainActor))
}
def socket = WebSocket.accept[JsValue,JsValue]
{
request =>
ActorFlow.actorRef(out => MYWSACTOR.props(out))
}
I am new to Akka and Scala . I am trying to make a chat application using Akka in Scala and Play framework 2.5.3 .I used the above code from the official documentation. I just wanted to create another actor (mainActor in above code) so that message from the client is validated first and then be sent back. But the problem is mainActor is unable to send the msg to another class case sendMsg. Also , if i try to create the actor at any different point, it gives me compilation error Unexpected Exception: Provisional Exception
Here is hopefully what you probably wanted:
case class SendMsg(msg: JsValue)
case class ReceivedMessage(wsActor: ActorRef, msg: JsValue)
class MyWsActor(out: ActorRef, mainActor:ActorRef) extends Actor {
def receive = {
case msg : JsValue =>
mainActor ! ReceivedMessage(self, msg)
case SendMsg(msg) =>
out ! msg
}
}
object MyWsActor {
def props(out: ActorRef, mainActor: ActorRef) = Props(new MyWsActor(out, mainActor))
//or if you want to instantiate your Props with reflection
def props2(out: ActorRef, mainActor: ActorRef) = Props(classOf[MyWsActor], out, mainActor)
}
class MainActor() extends Actor {
def receive = {
case ReceivedMessage(wsActor, msg) => {
/*
Do sth with msg
*/
val result = msg
wsActor ! SendMsg(result) //here sender is your MyWsActor
}
}
}
class HomeController #Inject() (
actorSystem: ActorSystem,
materializer: Materializer
) extends Controller {
val mainActor = actorSystem.actorOf(Props[MainActor], "mainActor")
def socket = WebSocket.accept[JsValue,JsValue] {
request =>
ActorFlow.actorRef(out => MyWsActor.props(out, mainActor))
}
}
The key is that you can send, with actual message, also ActorRef (or by default you can get a sender ActorRef calling sender()) and then you can replay to this ActorRef.
I have a parent actor named "manager" which creates several child actors.
These child actors then send back their response via "sender tell", i.e directly back to "manager".
I want to create a unit test for this manager actor, and therefore need to inject a probe to forward the messages from the manager to its children.
I used the following post:
http://www.superloopy.io/articles/2013/injecting-akka-testprobe.html
However i'm still having some trouble getting this done correctly.
In order to simplify the situation, attached is code describing the actors and unit test i wrote for just one child.
Manager class:
trait ManagerChildProvider {
def createTimestampPointChild: Actor
}
trait ProductionManagerChildProvider extends ManagerChildProvider {
def createTimestampPointChild = new TimeDifferenceCalculationActor
}
object Manager {
def apply() = new Manager("cid1") with ProductionManagerChildProvider
}
class Manager(name: String) extends Actor with ActorLogging {
this: ManagerChildProvider =>
#Autowired private val modelParams = new ModelParams //list of parameters
val timeDifference = context.actorOf(Props(createTimestampPointChild))
def receive = {
case p#TimePoint(tPoint) =>
timeDifference ! p
case _ =>
log.error("Unknown message type")
}
}
Child class:
class TimeDifferenceCalculationActor extends Actor with ActorLogging {
var previousTimestamp: Long = -1
def receive = {
case tPoint(timestamp) =>
if (previousTimestamp != -1) {
sender ! Result(1)
}
case _ =>
log.error("Unknown message type")
}
}
Test class:
object BarSpec {
class Wrapper(target: ActorRef) extends Actor {
def receive = {
case x => target forward x
}
}
}
trait ChildrenProvider {
def newFoo: Actor
}
class BarSpec extends TestKitSpec("BarSpec") {
import Manager._
import BarSpec._
trait TestCase {
val probe = TestProbe()
trait TestChildrenProvider extends ManagerChildProvider {
def newBar = new Wrapper(probe.ref)
}
val actor = system.actorOf(Props(new Manager(componentId = "cid1") with TestChildrenProvider))
}
"Bar" should {
"involve child in doing something" in new TestCase {
actor ! tPoint(1)
actor ! tPoint(2)
probe.expectMsg(tPoint(1))
//probe.reply("ReplyFromChild")
//expectMsg("ReplyFromParent")
}
}
}
Additional test class:
abstract class TestKitSpec(name: String) extends TestKit(ActorSystem(name)) with MustMatchers with BeforeAndAfterAll with ImplicitSender with WordSpecLike{
override def afterAll() {
system.shutdown()
}
}
Currently i get the following errors:
Error:(36, 42) object creation impossible, since method > createTimestampPointChild in trait ManagerChildProvider of > type => akka.actor.Actor is not defined
val actor = system.actorOf(Props(new Manager(componentId = "cid1") with TestChildrenProvider))
Error:(11, 16) overriding method run in trait BeforeAndAfterAll of type > (testName: Option[String], args: > org.scalatest.Args)org.scalatest.Status;
method run in trait WordSpecLike of type (testName: Option[String], > args: org.scalatest.Args)org.scalatest.Status needs `abstract override' > modifiers
abstract class TestKitSpec(name: String) extends > TestKit(ActorSystem(name))
any help with these specific errors or with the task in general would be highly appreciated
Akka and Scala newbie here, please feel free to edit the question as necessary in order to clearly articulate my intent in the domain of Scala and Akka.
Before I show code snippets, here's the problem I want to solve: I essentially want to develop a common module for my team to use when they're developing their applications using Akka actors. I want to allow them to mixin a trait which will extend their receive functionality at runtime, mainly for logging purposes. I'm running into compile errors, which I'll explain soon.
But first, take for example, a simple main:
object Test extends App {
val system = ActorSystem("system")
val myActor = system.actorOf(Props(new MyActor), "myActor")
myActor ! "Hello world!"
}
Here's an example implementation of an actor that a team member might implement in his application:
class MyActor extends Actor with ActorLogger {
override def receive: Receive = {
case msg => {
log.info("testing ...")
}
case _ => throw new RuntimeException("Runtime Ex")
}
}
And here's an example of how I would provide a common trait for them to mixin:
trait ActorLogger extends Actor {
val log: DiagnosticLoggingAdapter = Logging(this)
abstract override def receive: Receive = {
case msg: Any => {
if (msg.isInstanceOf[String]) {
println("enter")
log.mdc(Map[String, Any]("someKey" -> 123))
super.receive(msg)
log.clearMDC()
println("exit")
}
}
case _ => throw new RuntimeException("Runtime Ex")
}
}
As you can see, I'm trying to add data to an MDC if the message so happens to be String (a basic example, in reality, I would check for some custom type of our own).
The error I get is:
Error:(29, 16) overriding method receive in trait ActorLogger of type =>
MyActor.this.Receive;
method receive needs `abstract override' modifiers
override def receive: Receive = {
^
What's wrong here? And is stackable traits the right to go away to achieve something like this? If not, what is the most idiomatic way?
More generally, is there another pattern being applied here besides "interceptor" pattern?
Thanks for all the help!
A solution without a hack with akka package:
import akka.actor.{Actor, ActorSystem, Props}
trait MyActorExtension extends Actor {
def receiveExtension: Receive = PartialFunction.empty
}
abstract class MyActor extends MyActorExtension {
protected def receiveMsg: Receive
def receive: Receive = receiveExtension orElse receiveMsg
}
trait ActorLogger1 extends MyActor with MyActorExtension {
abstract override def receiveExtension = {
case msg =>
println(s"********** Logging # 1: $msg")
super.receiveExtension.applyOrElse(msg, receiveMsg)
}
}
trait ActorLogger2 extends MyActor with MyActorExtension {
abstract override def receiveExtension = {
case msg =>
println(s"########## Logging # 2: $msg")
super.receiveExtension.applyOrElse(msg, receiveMsg)
}
}
class SpecificActor extends MyActor with ActorLogger1 with ActorLogger2 {
def receiveMsg = {
case someMsg =>
println(s"SpecificActor: $someMsg")
}
}
object Test extends App {
val system = ActorSystem("system")
val mySpecificActor = system.actorOf(Props(new SpecificActor), "SpecificActor")
mySpecificActor ! "Hello world!"
}
#### Logging # 2: Hello world!
****** Logging # 1: Hello world!
SpecificActor: Hello world!
aroundReceive is for Akka internal use and the stackable trair pattern is not that comfortable for this case.
I recommend you using Receive Pipeline for easy message interception.
I think that you need something like this
package akka
import akka.MsgsProt._
import akka.actor.{ Actor, ActorSystem, Props }
import scala.concurrent.duration._
sealed trait MsgProt
object MsgsProt {
case object FooMsg extends MsgProt
case object BarMsg extends MsgProt
}
trait Foo extends Actor {
override protected[akka] def aroundReceive(receive: Actor.Receive, msg: Any): Unit = msg match {
case FooMsg => println("Foo message")
case msg => super.aroundReceive(receive, msg)
}
}
trait Bar extends Actor {
override protected[akka] def aroundReceive(receive: Actor.Receive, msg: Any): Unit = msg match {
case BarMsg => println("Bar message")
case msg => super.aroundReceive(receive, msg)
}
}
class MyActor extends Actor with Foo with Bar {
override def receive: Actor.Receive = {
case _ => println("Nothing I know")
}
}
object Foo extends App {
val system = ActorSystem("foobar")
val myActor = system.actorOf(Props[MyActor])
implicit val timeout = 2 seconds
myActor ! FooMsg
myActor ! BarMsg
myActor ! "wrong message"
system.awaitTermination(10 seconds)
}
The output of this program is:
Foo message
Bar message
Nothing I know
Most important part is that package declaration - akka. Because method aroundReceive is limited only to akka package so you have to have some.package.akka and inside you can use that method aroundReceive. I think that it looks more like a hack not a solution but works. You can see more usage of this inside Akka itself ex. akka.DiagnosticActorLogging. But this is basically solution that you want to do with stacking Actors behaviour.
Consider these two traits:
trait Poked extends Actor {
override def receive = {
case Poke(port, x) => ReceivePoke(port, x)
}
def ReceivePoke(port: String, x: Any)
}
trait Peeked extends Actor {
override def receive = {
case Peek(port) => ReceivePeek(port)
}
def ReceivePeek(port: String)
}
Now consider I can create a new Actor that implements both traits:
val peekedpoked = actorRef(new Actor extends Poked with Peeked)
How do I compose the receive handlers? i.e., the receiver should be something like the following code, though "automatically generated" (i.e., all traits should compose):
def receive = (Poked.receive: Receive) orElse (Peeked.receive: Receive) orElse ...
You can use super[T] to reference members of particular super classes/traits.
For example:
trait IntActor extends Actor {
def receive = {
case i: Int => println("Int!")
}
}
trait StringActor extends Actor {
def receive = {
case s: String => println("String!")
}
}
class IntOrString extends Actor with IntActor with StringActor {
override def receive = super[IntActor].receive orElse super[StringActor].receive
}
val a = actorOf[IntOrString].start
a ! 5 //prints Int!
a ! "Hello" //prints String!
Edit:
In response to Hugo's comment, here's a solution that allows you to compose the mixins without having to manually wire their receives together. Essentially it involves a base trait with a mutable List[Receive], and each mixed-in trait calls a method to add its own receive to the list.
trait ComposableActor extends Actor {
private var receives: List[Receive] = List()
protected def registerReceive(receive: Receive) {
receives = receive :: receives
}
def receive = receives reduce {_ orElse _}
}
trait IntActor extends ComposableActor {
registerReceive {
case i: Int => println("Int!")
}
}
trait StringActor extends ComposableActor {
registerReceive {
case s: String => println("String!")
}
}
val a = actorOf(new ComposableActor with IntActor with StringActor).start
a ! 5 //prints Int!
a ! "test" //prints String!
The only thing to keep in mind is that the order of the receives should not be important, since you won't be able to easily predict which one is first in the chain, though you could solve that by using a mutable hashmap instead of a list.
You can use empty Receive in base actor class and chain receives in their definitions.
Sample for Akka 2.0-M2:
import akka.actor.Actor
import akka.actor.Props
import akka.event.Logging
import akka.actor.ActorSystem
class Logger extends Actor {
val log = Logging(context.system, this)
override def receive = new Receive {
def apply(any: Any) = {}
def isDefinedAt(any: Any) = false
}
}
trait Errors extends Logger {
override def receive = super.receive orElse {
case "error" => log.info("received error")
}
}
trait Warns extends Logger {
override def receive = super.receive orElse {
case "warn" => log.info("received warn")
}
}
object Main extends App {
val system = ActorSystem("mysystem")
val actor = system.actorOf(Props(new Logger with Errors with Warns), name = "logger")
actor ! "error"
actor ! "warn"
}