“系统”定义失败

时间:2014-08-12 17:01:17

标签: scala akka

我不知道什么可以解决我在下面遇到的错误。我试图改变我的对象的定义,甚至把它设置为全局,但是没有成功,你可能会理解。

以下是代码:

import akka.actor._
import akka.actor.Actor
import akka.actor.Props
import akka.actor.ScalaActorRef
import akka.pattern.gracefulStop
import akka.util._
import java.util.concurrent._
import scala.Array._
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global

sealed trait MSG
case class start(
    target: ActorRef
) extends MSG
case class msgTest(
    content: Int
) extends MSG
case class result(
    content: String
) extends MSG

class tip extends Actor {
    def receive = {
        case start(target) =>
            system.scheduler.scheduleOnce(
                1000 milliseconds,
                target,
                msgTest(3)
            )
            system.scheduler.scheduleOnce(
                1000 milliseconds,
                target,
                msgTest(2)
            )
            system.scheduler.scheduleOnce(
                1000 milliseconds,
                target,
                msgTest(1)
            )
        case result(content) =>
            println ("received | content: " + content)
        case _       =>
            println ("no matching found")
    }

}

class transformationNode extends Actor {
    def receive = {
        case msgTest(content) =>
            if (content == 1) {
                sender ! result("mode 1")
            } else if (2 == content) {
                sender ! result("mode 2")
            } else if (3 == content) {
                sender ! result("mode 3")
            } else {
                println ("wut?")
                sender ! result("mode unknown")
            }
        case _      =>
            println ("wut?")
            sender ! "message unknown"
    }
}

object main extends App {
    val system = akka.actor.ActorSystem("mySystem")
    val tip = system.actorOf(Props[tip], name = "tip")
    val transformationNode = system.actorOf(Props[transformationNode], name = "transformationNode")

    tip ! start(transformationNode)
}

然后是错误:

[info] Compiling 1 Scala source to /home/plard/wrkspc/prjt3/target/scala-2.11/classes...
[error] /home/plard/wrkspc/prjt3/src/main/scala/proj.scala:13: expected class or object definition
[error] def system = akka.actor.ActorSystem("mySystem")
[error] ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 2 s, completed Aug 12, 2014 6:49:12 PM

1 个答案:

答案 0 :(得分:1)

您使用的是“system”而不是“context.system”。

此外,我根据常见的(至少对我而言)编码风格修改了源代码:

import akka.actor._
import akka.actor.Actor
import akka.actor.Props
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global

sealed trait Msg

case class Start(
                  target: ActorRef
                  ) extends Msg

case class MsgTest(
                    content: Int
                    ) extends Msg

case class Result(
                   content: String
                   ) extends Msg

class Tip extends Actor {

  def receive = {
    case Start(target) =>
      (1 to 3) foreach {
        i =>
          context.system.scheduler.scheduleOnce(
            1000 milliseconds,
            target,
            MsgTest(i)
          )
      }

    case Result(content) =>
      println (s"received | content: $content")

    case _       =>
      println ("no matching found")
  }
}

class TransformationNode extends Actor {

  def receive = {
    case MsgTest(content) if (1 to 3).contains(content) =>
        sender ! Result(s"mode $content")

    case MsgTest(content) =>
        println ("wut?")
        sender ! Result(s"mode $content unknown")

    case _      =>
      println ("wut?")
      sender ! "message unknown"
  }
}

object Main extends App {
  val system = akka.actor.ActorSystem("mySystem")
  val tip = system.actorOf(Props[Tip], name = "tip")
  val transformationNode = system.actorOf(Props[TransformationNode], name = "transformationNode")

  tip ! Start(transformationNode)
}