【问题标题】:Could not execute this Akka actor based test code无法执行这个基于 Akka actor 的测试代码
【发布时间】:2014-11-06 18:02:58
【问题描述】:

我是 Scala Akka 演员的新手。根据他们网站上的 akka 教程示例,我编写了一个类似的示例,只在它们开始时打印消息。但是代码没有在我的 Eclipse IDE 中执行。

import akka.actor.Actor
import akka.actor.ActorRef
import scala.collection.mutable.ListBuffer
import akka.actor.Props
import akka.routing.RoundRobinRouter
import akka.actor.ActorSystem

object AkaObj extends App{


process()

sealed trait PiMessage
case class Work(value:String) extends PiMessage
case class printComp(valu:String)extends PiMessage
case object Start extends PiMessage 
case class ListObj(cont:Seq[String]) extends PiMessage


class Worker extends Actor{

  def receive()={

    case Work(value:String)=>
      println(value)
      sender ! printComp(value)
  }
}

class Master(listener: ActorRef) extends Actor{

  val nrOfWorkers = 10
  var counter = 0
  var lis = ListBuffer[String]()

  val workerRouter = context.actorOf(
  Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)), name = "workerRouter")

  def receive()={
    //handle message
    case Start=>workerRouter ! Work("Start now " + System.currentTimeMillis())
    case printComp(value:String)=> { 
      counter+=1
      lis.append(counter+"-"+value)
      if(counter >=10)
          listener ! ListObj(lis.toSeq)

      // Stops this actor and all its supervised children
      context.stop(self)
      }
  }
 }

class Listener extends Actor{

  def receive()={

    case ListObj(cont:Seq[String])=>
      println("Completed " +cont)
      context.system.shutdown()
  }
}

def process(){
  // Create an Akka system
    val system = ActorSystem("PiSystem")

  // create the result listener, which will print the result and shutdown the system
    val listener = system.actorOf(Props[Listener], name = "listener")

  // create the master
    val master = system.actorOf(Props(new Master(listener)),
      name = "master")

  // start the calculation
    master ! Start
}

}

问题:我收到的错误消息是:“错误:无法找到或加载主类 AkaObj” 我认为它甚至没有编译。当我运行项目时,这就是我得到的信息。

【问题讨论】:

  • 您需要更明确地说明您遇到的问题。根据您的描述,这有点模糊。
  • 我已经更新了这个问题。我无法运行该项目。
  • 我建议您从 Activator 中的入门教程之一开始:akka.io/downloads

标签: scala akka actor


【解决方案1】:

实际上,在解决依赖关系后,我设法运行了您的代码。所以问题出在您的环境配置中。我使用 Intellij Idea IDE。

试试这个Eclipse Error: Could not find or load main class

【讨论】:

  • 我在 Windows 的 Scala cmd 中测试了代码。我仍然得到同样的错误。所以我认为问题不在于 Eclipse 或依赖项。
【解决方案2】:

实际问题是object AkaObj extends App的使用。在编写 scala 时,我注意到使用 scala.App 仅限于简单程序(至少在 IDE 中)。一旦您开始在 scala.App 中定义对象和类,Eclipse 就会开始抱怨找不到程序的入口点。

尝试按以下方式解决此问题:

import akka.actor.Actor
import akka.actor.ActorRef
import scala.collection.mutable.ListBuffer
import akka.actor.Props
import akka.routing.RoundRobinRouter
import akka.actor.ActorSystem

object AkaObj {

  def main(args:Array[String]) {
    process()
  }

  def process() {
    // Create an Akka system
      val system = ActorSystem("PiSystem")

    // create the result listener, which will print the result and shutdown the system
      val listener = system.actorOf(Props[Listener], name = "listener")

    // create the master
      val master = system.actorOf(Props(new Master(listener)),
        name = "master")

    // start the calculation
      master ! Start
  }

  sealed trait PiMessage
  case class Work(value:String) extends PiMessage
  case class printComp(valu:String)extends PiMessage
  case object Start extends PiMessage 
  case class ListObj(cont:Seq[String]) extends PiMessage


  class Worker extends Actor{

    def receive()={

      case Work(value:String)=>
        println(value)
        sender ! printComp(value)
    }
  }

  class Master(listener: ActorRef) extends Actor{

    val nrOfWorkers = 10
    var counter = 0
    var lis = ListBuffer[String]()

    val workerRouter = context.actorOf(
    Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)), name = "workerRouter")

    def receive()={
      //handle message
      case Start=>workerRouter ! Work("Start now " + System.currentTimeMillis())
      case printComp(value:String)=> { 
        counter+=1
        lis.append(counter+"-"+value)
        if(counter >=10)
            listener ! ListObj(lis.toSeq)

        // Stops this actor and all its supervised children
        context.stop(self)
        }
    }
  }

  class Listener extends Actor{

    def receive()={

      case ListObj(cont:Seq[String])=>
        println("Completed " +cont)
        context.system.shutdown()
    }
  } 
}

在大多数情况下,使用def main(args:Array[String]) 可以解决问题。

【讨论】:

    猜你喜欢
    • 2020-10-04
    • 2013-11-27
    • 2019-02-12
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 2013-05-23
    相关资源
    最近更新 更多