I can't get a child actor to receive messages - scala

I'm new to akka and I'm wondering how to get a child actor to receive messages.
I have one parent actor which prints out that it received a message and then sends a message to its child which then prints out that it received a message.
When I run the program, only the parent actor prints its message. Is there
something I'm missing?
The parent actor
class TestDirector(name: String) extends Actor {
import TestDirector._
implicit val timeout = Timeout(5.seconds)
private var child: ActorRef = _
override def preStart(): Unit = {
println(name + " pre-start")
child = context.actorOf(props("test-file"), name = "child-actor")
}
override def receive: Receive = {
case TestDirectory(dir) =>
println("Test Director")
child ! TestWorker.TestFile
}
}
object TestDirector {
case class TestDirectory(dirName: String)
case class TestResponse(message: String)
def props(dirName: String) = Props(classOf[TestDirector], dirName)
}
The child actor
class TestWorker(fileName: String) extends Actor {
import TestWorker._
override def receive: Receive = {
case TestFile => println("Hello world")
}
}
object TestWorker {
case object TestFile
def props(fileName: String) = Props(classOf[TestWorker], fileName)
}

In TestDirector.preStart, you say
child = context.actorOf(props("test-file"), name = "child-actor")
Those "props" are TestDirector.props, which you imported a few lines above, so you're endlessly creating new TestDirectors. I think what you want instead is TestWorker.props:
child = context.actorOf(TestWorker.props("test-file"), name = "child-actor")

Related

Akka: Test scheduled actors using Testkit

I have a parent actor which creates a child actor and the child actor greets parent every minute as below
class MasterActor extends Actor with ActorLogging {
override def receive: Receive = {
case "Greet" =>
print("Hey child!!")
case "CreateChild" =>
context.actorOf(Props[ChildActor])
}
}
class ChildActor extends Actor with ActorLogging {
import context.dispatcher
override def preStart(): Unit = {
super.preStart()
context.system.scheduler.schedule(Duration("1 minutes").asInstanceOf[FiniteDuration],
Duration("1 minutes").asInstanceOf[FiniteDuration], context.parent, "Greet")
}
override def receive: Receive = {
case _ =>
print("child receives something")
}
}
I am new to actor system, How can I test schedule scenario using TestKit?
I tried something like below in my test but that is not working
"Master actor" should {
"receive a Greet message every minute" in {
val probe = TestProbe
val actor = system.actorOf(Props(new Child() {
import context.dispatcher
override def preStart() =
context.system.scheduler.scheduleOnce(Duration("1 seconds").asInstanceOf[FiniteDuration], probe.ref, "Greet")
}))
probe.expectMsg("Greet")
}
}
You can read about it at the timing-assertions section in the testing of akka docs. There is the within function that should help you.
For example you can try:
"Master actor" should {
"receive a Greet message every minute" in {
within(62 seconds) {
val probe = TestProbe
val actor = system.actorOf(Props[Child])
probe.expectMsg("Greet")
}
}
}
What I would do, not to wait so long, is putting the delay timeout in the configuration, and change it to a few millis in the test scenario.

Akka actor get remaining message list

I have an actor computing something intensive, and only the last result ideally should count.
I would like that if it receive multiple messages of the same type A(data), only the last one is handled and the previous ones are discarded.
How can I achieve that?
Custom mailbox
You can try implement some custom mailbox, containing 0 or 1 message:
import akka.actor.{ActorRef, ActorSystem}
import akka.dispatch._
import com.typesafe.config.Config
class SingleMessageQueue extends MessageQueue {
var message = Option.empty[Envelope]
def enqueue(receiver: ActorRef, handle: Envelope) = message = Some(handle)
def dequeue() = {
val handle = message.orNull
message = None
handle
}
def numberOfMessages = message.size
def hasMessages = message.nonEmpty
def cleanUp(owner: ActorRef, deadLetters: MessageQueue) = message.foreach(deadLetters.enqueue(owner, _))
}
final case class SingleMessageMailbox() extends MailboxType with ProducesMessageQueue[SingleMessageQueue] {
def this(settings: ActorSystem.Settings, config: Config) = this()
override def create(owner: Option[ActorRef], system: Option[ActorSystem]): MessageQueue = new SingleMessageQueue
}
and next enable it for your actor as descrived in the mailbox section of the docs
Split actors
You can introduce pair of actors.
Manager, receiving a job, resending it to Worker whenever it's not working right now
Worker doing actual work and notifing it's manager when it's done
example:
import akka.actor.{Actor, ActorRef, Props}
object Worker {
case class Job()
case object JobDone
}
import Worker.{Job, JobDone}
class Worker extends Actor {
override def receive = {
case Job() ⇒
// your long job
context.parent ! JobDone
}
}
class Manager extends Actor {
var nextJob = Option.empty[(Job, ActorRef)]
val worker = context.actorOf(Props[Worker])
def working: Receive = {
case job: Job ⇒ nextJob = Some((job, sender))
case JobDone ⇒
nextJob match {
case Some((job, snd)) ⇒ worker.tell(job, snd)
case None ⇒ context.become(free)
}
}
def free: Receive = {
case job: Job ⇒
worker.tell(job, sender)
context.become(working)
}
override def receive = free
}

Decorating actors with some actions

I read about dangerous case there.
Now, I'm working on an extension which is supposed to trace incoming messages. I.e. when a message arrive I have to notify some external system that the message's arrived.
Is it safe to create actor object within another actor and pass it to props factory?
class HandlingActorWrapper(val decoratee: Actor) extends Actor {
override def receive: Receive = {
System.out.println("Message recieved");
decoratee.receive
}
}
object HandlingActorWrapper{
def props(actor: Actor) = Props(new HandlingActorWrapper(actor))
}
class MyActor extends Actor {
override def receive: Receive = {
//handling messages
}
}
class SomeActor extends Actor {
var child : ActorRef = _
override def preStart() = {
//>>|HERE
child = context.system.actorOf(HandlingActorWrapper.props(new MyActor))
}
//other
}

postRestart and preRestart methods are not getting invoke in akka actots

I am following this tutorial here is my code
case class ArtGroupDeleteFromES (uuidList:List[String])
class ArtGroupDeleteESActor extends Actor{
val log = LoggerFactory.getLogger(this.getClass)
override def preStart() {
log.debug("preStart Starting ArtGroupDeleteESActor instance hashcode # {}",
this.hashCode())
}
override def postStop() {
log.debug("postStop Stopping ArtGroupDeleteESActor instance hashcode # {}",
this.hashCode())
}
override def preRestart(reason: Throwable, message: Option[Any]) {
log.debug("I am restarting")
log.debug("ArtGroupDeleteESActor: preRestart")
log.debug(s" MESSAGE: ${message.getOrElse("")}")
log.debug(s" REASON: ${reason.getMessage}")
super.preRestart(reason, message)
}
override def postRestart(reason: Throwable) {
log.debug("restart completed!")
log.debug("ArtGroupDeleteESActor: postRestart")
log.debug(s" REASON: ${reason.getMessage}")
super.postRestart(reason)
}
def receive = {
case ArtGroupDeleteFromES(uuidList) =>
throw new Exception("Booom")
sender ! true
}
case message =>
log.warn("Received unknown message: {}", message)
unhandled(message)
}
}
and here is the how i am sending this actor a message
class ArtGroupDeletionActor extends Actor{
val log = LoggerFactory.getLogger(this.getClass)
override val supervisorStrategy = OneForOneStrategy(
maxNrOfRetries = 10, withinTimeRange = 10 seconds) {
case _:Exception => Restart
}
val artGroupDeleteESActor=context.actorOf(Props[ArtGroupDeleteESActor]
.withDispatcher("akka.actor.ArtGroupDeleteESActor-dispatcher")
,name = "ArtGroupDeleteESActor")
def receive = {
case DeleteArtGroup(uuidList) =>
val future1 = ask(artGroupDeleteESActor, ArtGroupDeleteFromES(uuidList)).mapTo[Boolean]
var isDeletedfromES = Await.result(future1, timeout.duration)
case message =>
log.warn("Unhandled message received : {}", message)
unhandled(message)
}
}
object test extends App{
val artGroupDeletionActor=system.actorOf(Props[ArtGroupDeletionActor]
.withDispatcher("akka.actor.ArtGroupDeletionActor-dispatcher")
,name = "ArtGroupDeletionActor")
artGroupDeletionActor ! DeleteArtGroup(List("123"))
}
the PostRestart() and preRestart() methods are not invoking,but preStart() and postStop() gets called, please guide me where i am doing wrong
(for simplicity I'll call your actors Parent and Child from now on)
What happens here is that when an exception occurs inside Child.receive, it doesn't send a response to Parent, instead, the actor system sends some control instruction for the supervision strategy. However, Parent is blocked on Await waiting for completion of future1, which only happens after the timeout exceeds, and then, in turn, a TimeoutException is thrown inside Parent.receive, killing (restarting) the Parent actor itself, and thus the supervising message of an exception in Child is then passed to deadLetters, never restarting the Child.
You should never, ever, ever block inside an actor, so this is incorrect:
val future1 = ask(artGroupDeleteESActor, ArtGroupDeleteFromES(uuidList)).mapTo[Boolean]
var isDeletedfromES = Await.result(future1, timeout.duration)
Instead, you have to either utilize some kind of message identification to distinguish one reply from another in concurrent environment, or add an onComplete to the Future and send a message to self in the closure (beware: no logic other than sending a message should be executed inside the closure to the Future!).
So, option A:
case class ArtGroupDeleteFromES(id: Long, uuidList: List[String])
case class ArtGroupDeleteFromESResult(id: Long, success: Boolean)
class Parent extends Actor {
override val supervisionStrategy = ...
var msgId = 0L
var pendingRequesters = Map.empty[Long, ActorRef]
val child = context.actorOf(Props[Child])
def nextId = {
msgId += 1
msgId
}
def receive = {
case DeleteArtGroup(uuidList) =>
val id = nextId
pendingRequesters += id -> sender() // store a reference to the sender so that you can send it a message when everything completes
child ! DeleteArtGroupFromES(nextId, uuidList)
case ArtGroupDeleteFromESResult(id, success) =>
// process result...
pendingRequesters(id) ! "done"
pendingRequesters -= id
}
}
And option B:
case class ArtGroupDeleteFromES(uuidList: List[String])
case class ArtGroupDeleteFromESResult(replyTo: ActorRef, success: Boolean)
class Parent extends Actor {
override val supervisionStrategy = ...
val child = context.actorOf(Props[Child])
def receive = {
case DeleteArtGroup(uuidList) =>
val requester = sender() // when the future completes, sender may have already changed, so you need to remember it
(child ? DeleteArtGroupFromES(uuidList)).onComplete {
case Success(success) => self ! ArtGroupDeleteFromESResult(requester, success)
case Failure(e) =>
log.warn("Could not delete...", e)
self ! ArtGroupDeleteFromESResult(requester, success = false)
}
}

how to perform supervision in akka

I have two actors one is parent and one is child ,The child actor is responsible for fetching data from MongoDB against an given id and reply back the data to the calling actor which is a parent in my case ,Now i want to apply supervision in my child actor i know how to perform supervision strategy but how to do it in my code that's confusing me
i am catching exception in my try/catch block so that every type of exception will be caught but then i am stuck on the point how to app,y supervision as i don't know exactly what exception my code will throw in future here is my code please help me
ReadOnlyAdminQueryActor.scala(Patent Actor)
class ReadOnlyAdminQueryActor extends Actor{
val log = LoggerFactory.getLogger("controller")
case ReadOnlyAdminReadByID(idList)=>
var RetunedLists = new MutableList[ReadOnlyAdmin]()
RetunedLists= readById(idList)
sender ! RetunedLists //return list of ReadOnlyAdmin objects to the calling actor (matched uuid results)
def readById(idList:MutableList[Int]):MutableList[ReadOnlyAdmin]= {
var connection=MongoFactory.getConnection
var collection=MongoFactory.getCollection(connection, "readOnlyAdmin")
var RetunedList = new MutableList[ReadOnlyAdmin]()
var id:Int=0
var email:String=""
var SecondryEmail:Option[String]=None
var FirstName:String=""
var LastName:String=""
var userStatus:String=""
log.info("readOnlyAdmin query class data method readByID")
for(Id<-idList){
val q=QueryBuilder.start("_id").is(Id)
val cursor=collection.find(q.get)
var obj=new BasicDBObject
try {
while(cursor.hasNext)
{
obj=cursor.next().asInstanceOf[BasicDBObject]
id=obj.getString("_id").toInt
email=obj.getString("Email")
SecondryEmail=Option(obj.getString("SecondryEmail"))
FirstName=obj.getString("FirstName")
LastName=obj.getString("LastName")
userStatus=obj.getString("UserStatus")
val readOnlyAdmin=new ReadOnlyAdmin(id,FirstName, LastName, email, SecondryEmail ,"",UserStatus.withName(userStatus))
RetunedList+=readOnlyAdmin //adding objects in a list
}//end of while
}//end of try
catch
{
case e: Exception => log.error("printStackTrace"+e.printStackTrace)
}
finally{
cursor.close()
MongoFactory.closeConnection(connection)
}
}//end for loop
RetunedList
}
}
ReadOnlyAdminReadMongoActor.scala (Child Actor)
class ReadOnlyAdminReadMongoActor extends Actor{
val log = LoggerFactory.getLogger("controller")
val ReadOnlyAdminQueryActor=context.actorOf(Props[ReadOnlyAdminQueryActor].withDispatcher("akka.actor.readOnlyAdminReadMongoActor-dispatcher"), name = "ReadOnlyAdminQueryActor")
case ReadOnlyAdminReadFromMongoById(readOnlyAdmin,idList)=>
var RetunedLists = new MutableList[ReadOnlyAdmin]()
implicit val timeout = Timeout(10 seconds)//wait for 10 seconds
override val supervisorStrategy: SupervisorStrategy = {
OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 10 seconds) {
case x: Exception => ???
}
}
val future:Future[MutableList[ReadOnlyAdmin]] = ask(ReadOnlyAdminQueryActor,ReadOnlyAdminReadByID(idList)).mapTo[MutableList[ReadOnlyAdmin]]
future.onComplete {
case Success(result)=>
RetunedLists=result
for(a<-RetunedLists)
{
log.info ("id is "+a.getUuid+"First name is "+a.getFirstName
+"Last name is "+a.getLastName+"Email is "+a.getEmail
+"secondry email is "+a.getSecondryEmail+"user status is "+a.getUserStatus)
}
case Failure(e)=>
log.error(" in failure")
log.error("printStackTrace"+e.printStackTrace)
}
object Test extends App{
val system = ActorSystem("TestSystem")
val readOnlyAdmin= new ReadOnlyAdmin
var uuidsList = new MutableList[Int]()
uuidsList+=123
val ReadOnlyAdminReadMongoActor=system.actorOf(Props[ReadOnlyAdminReadMongoActor].withDispatcher("akka.actor.readOnlyAdminReadMongoActor-dispatcher"), name = "ReadOnlyAdminReadMongoActor")
ReadOnlyAdminReadMongoActor ! ReadOnlyAdminReadFromMongoById(readOnlyAdmin,uuidsList)
}
how can i perform supervision in a correct way and how do i came to know which exception will be thrown in my child actor also Fortunately, Java libraries explicitly say what they're going to throw, and Scala libraries almost always throw very little or no even in IDE no information is shown when we hover the mouse on the code
please help me thanks in advance
The supervisor strategy belongs with the Parent, not the Child. The child should throw its exceptions and the parent determines how to handle the failure. In the code below, the Child actor will be restarted 3 times, then terminate:
class Parent extends Actor {
override def preStart(): Unit = {
self ! "Start Child"
}
def receive = {
case "Start Child" => context.actorOf(Props[Child])
}
override def supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 3) {
case ex: Throwable => Restart
}
}
class Child extends Actor {
override def preStart() = {
self ! "Throw"
}
def receive = {
case "Throw" => throw new Exception("Throwing")
}
}