无法解决scala akka示例程序中的类

时间:2017-11-23 11:01:42

标签: scala intellij-idea akka

嘿,我是scala和akka的新手,我想尝试在intellij中找到的这个示例程序。不幸的是它不起作用。我不知道为什么它不起作用,但我也是intellij的新手,我认为这可能是我加载的libaries的一个问题。无论如何这里是我找到的代码。这是Printer类:

package test
import akka.actor.{Actor, ActorLogging}

class Printer extends Actor with ActorLogging{
  import Printer._

  def receive={
    case Greeting(greeting)=>
   log.info(s"Greeting received (from ${sender()}): $greeting")
 }
}

现在迎来了Greeter课程:

package test
import akka.actor.{Actor, ActorLogging, ActorRef, Props}
class Greeter(message: String, printerActor: ActorRef) extends Actor with 
ActorLogging {
  import Greeter._
  import Printer._
  var greeting = ""
  def receive = {
    case WhoToGreet(who) =>
      greeting = s"$message, $who"
    case Greet =>
  printerActor ! Greeting(greeting)
  }
}  

现在主要是:

package test
import akka.actor.{ActorRef, ActorSystem}
import scala.io.StdIn
object main extends App {
  import Greeter._
  // Create the 'helloAkka' actor system
  val system: ActorSystem = ActorSystem("helloAkka")
    try {
    // Create the printer actor
    val printer: ActorRef = system.actorOf(Printer.props, "printerActor")
    // Create the 'greeter' actors
    val howdyGreeter: ActorRef =
   system.actorOf(Greeter.props("Howdy", printer), "howdyGreeter")
   // Send messages
   howdyGreeter ! WhoToGreet("Akka")
   howdyGreeter ! Greet
    println(">>> Press ENTER to exit <<<")
   StdIn.readLine()
   } finally {
   system.terminate()
      }
   }

我有几个&#34;无法解决&#34;错误。首先,为什么我需要在Printer和Greeter中导入语句(导入Greeter._ / Printer._),它们被标记为无法解析。此外,每个打印机或Greeter都标记为无法解析。 这是我的错误:

C:\Users\olive\IdeaProjects\akkat\src\test\scala\test\Greeter.scala
Error:(4, 10) not found: value Greeter
  import Greeter._
Error:(5, 10) not found: value Printer
  import Printer._
Error:(8, 10) not found: value WhoToGreet
    case WhoToGreet(who) =>
Error:(10, 10) not found: value Greet
    case Greet =>
Error:(11, 22) not found: value Greeting
      printerActor ! Greeting(greeting)
C:\Users\olive\IdeaProjects\akkat\src\test\scala\test\Printer.scala
Error:(6, 10) not found: value Printer
  import Printer._
Error:(9, 10) not found: value Greeting
    case Greeting(greeting)=>
C:\Users\olive\IdeaProjects\akkat\src\test\scala\test\main.scala
Error:(5, 10) not found: value Greeter
  import Greeter._
Error:(10, 44) not found: value Printer
     val printer: ActorRef = system.actorOf(Printer.props, "printerActor")
Error:(13, 20) not found: value Greeter
     system.actorOf(Greeter.props("Howdy", printer), "howdyGreeter")
Error:(15, 20) not found: value WhoToGreet
    howdyGreeter ! WhoToGreet("Akka")
Error:(16, 20) not found: value Greet
    howdyGreeter ! Greet

这些是我加载的库 libaries

1 个答案:

答案 0 :(得分:1)

您必须创建与scala类相关的Companion对象。

离。对于打印机类

object Printer {
   def props: Props = Props[Printer]
   final case class Greeting(greeting: String)
}

对于Greeter:

object Greeter {
   def props(message: String, printerActor: ActorRef): Props = 
   Props(new Greeter(message, printerActor))
   final case class WhoToGreet(who: String)
   case object Greet
}

并且您必须将这些伴随对象导入到scala类中,该类是使用其属性的actor

Compainon Object与scala类名称相同

Companion Object