How to start an actor Scala aka

The following class raises an error in the new HelloWorld line:

Exception in thread "main" akka.actor.ActorInitializationException: You cannot create an instance of [HelloWorld] explicitly using the constructor (new). You have to use one of the 'actorOf' factory methods to create a new actor. See the documentation. at akka.actor.ActorInitializationException$.apply(Actor.scala:219) at akka.actor.Actor$class.$init$(Actor.scala:436) at HelloWorld.<init>(HelloWorld.scala:4) at Driver$.main(HelloWorld.scala:38) at Driver.main(HelloWorld.scala) 

So I'm trying: val hw = actorOf(new HelloWorld) But this causes a compiler error:

 not found: value actorOf 

How to implement HelloWorld below?

Reading other Scala docs requires the action method to be defined inside a class that extends Actor and then calls the start method in that class, is there any reason to use actorOf instead of defining an action method?

The following class is taken from Scala akka docs http://doc.akka.io/docs/akka/2.2.0/scala.html :

 import akka.actor.Actor import akka.actor.Actor._ import akka.actor.Props class HelloWorld extends Actor { override def preStart(): Unit = { // create the greeter actor val greeter = context.actorOf(Props[Greeter], "greeter") // tell it to perform the greeting greeter ! Greeter.Greet } def receive = { // when the greeter is done, stop this actor and with it the application case Greeter.Done => context.stop(self) } object Greeter { case object Greet case object Done } class Greeter extends Actor { def receive = { case Greeter.Greet => println("Hello World!") sender ! Greeter.Done } } } object Driver { def main(args: Array[String]) { new HelloWorld } } 
+4
source share
3 answers

You need to edit your main as shown below. Secondly, in line 5 you need to change it to context.actorOf(Props(new Greeter)) . This is because your Greeter does not have an apply function, so you need to manually create a Greeter object manually.

Work code below:

 import akka.actor.ActorSystem class HelloWorld extends Actor { override def preStart(): Unit = { // create the greeter actor val greeter = context.actorOf(Props(new Greeter), "greeter")//line 5 // tell it to perform the greeting greeter ! Greeter.Greet } def receive = { // when the greeter is done, stop this actor and with it the application case Greeter.Done => context.stop(self) } object Greeter { case object Greet case object Done } class Greeter extends Actor { def receive = { case Greeter.Greet => println("Hello World!") sender ! Greeter.Done } } } object Driver { def main(args: Array[String]) { val system = ActorSystem("Main") val ac = system.actorOf(Props[HelloWorld]) } } 
+8
source

If you want to use your main class, follow these steps:

 import akka.actor.{ActorSystem, Props} object Driver extends App { val system = ActorSystem("System") val hw = system.actorOf(Props[HelloWorld], name = "hw") } 

Which will create a new acting system, and then create an HelloWorld actor using this acting system.

You can also follow akka's instructions: set Akka.Main as the main class and give the program "com.example.HelloWorld" as an argument.

+4
source
 val greeter = context.actorOf(Props(new Greeter), "greeter")//line 5 

I do not think you need a new keyword for Greeter. I believe Coasters do this for you already. If something new should be

+2
source

Source: https://habr.com/ru/post/1495091/


All Articles