Create Actor

This is probably a very simple mistake, but I cannot get it to work correctly. I am using akka 2.2.3 to create an actor based application in scala.

The simplified setup is as follows:

object Main { def main(args: Array[String]) = { val system = ActorSystem("Test") val discoverer = system.actorOf(Props[Discoverer], "discoverer") implicit val timeout = Timeout(5.seconds) val not = discoverer ? Messages.Find(something) not.onComplete { case Success(va) => println(va) case Failure(err) => println(err) } ... } } 

And the main actor

 class Discoverer extends Actor { override def preStart() = { val refresher = context.actorOf(Props[Refresher], "refresher") refresher ! Refresh } def receive = { case _ => sender ! Answer } } 

And the actor Refresher

  class Refresher extends Actor { ... } 

What you should remove from this is that none of my participants have parameterized constructors.

However, if I try to run my application, it does not work with

 [ERROR] [12/09/2013 13:17:06.893] [Test-akka.actor.default-dispatcher-3] [akka://Test/user/discoverer] no matching constructor found on class Discoverer$Refresher for arguments [] 

What is my mistake? Shouldn't I create my actors using .actorOf(Props[Class], "actorname") ?

+8
scala akka
source share
2 answers

If you want to do this work with nested classes, you will need to create an instance of the nested actor by passing a reference to the attached actor as an argument to the constructor. The error you see suggests that there is no-args constructor, so there is a hint. The code for the work will look like this:

 object InnerTest { def main(args: Array[String]) { val sys = ActorSystem("test") sys.actorOf(Props[OuterActor]) } } class OuterActor extends Actor{ override def preStart = { context.actorOf(Props(classOf[InnerActor], this), "my-inner-actor") } def receive = { case _ => } class InnerActor extends Actor{ def receive = { case _ => } } } 

I assume this is due to an attempt to create an instance of a non-static inner class (via reflection) without pointing to the outer class. I determined this by reading this post:

https://www.assembla.com/spaces/akka/tickets/3675#/activity/ticket :

+11
source share

Refresher is a Discoverer inner class, so if you want to create a Refresher instance, you need to do this in the context of the Discoverer instance.

Take this example:

 class A{ class B{} } 

I can do new A , but new B will return an error. I must do:

 val a = new A val b = new aB 

So that the acc could not create this actor.

+1
source share

All Articles