Akka型错配;找到[错误]:需要单位[错误]:scala.sys.process.ProcessLogger

时间:2013-10-31 03:55:00

标签: scala process akka

我尝试编写示例代码来组合akka和actor。但是在编译代码时我收到了错误消息 代码非常简单,如下所示 那么,我有什么问题?

[error] /home/qos/workspaces/actors/actors.scala:20: type mismatch;
[error]  found   : Unit
[error]  required: scala.sys.process.ProcessLogger
[error]         execute(cmd)
[error]                ^
[error] one error found
[error] (compile:compile) Compilation failed

代码是

import scala.sys.process._ 
import akka.actor._ 

object TryActor { 

  def main(args: Array[String]) { 
    val akkaSystem = ActorSystem("akkaSystem") 
    val worker = akkaSystem.actorOf(Props[Worker], name = "work0") 
    worker ! Command("ls") 
  } 

  case class Command(cmd: String) 

  class Worker extends Actor { 

    def receive = { 
      case Command(cmd) => { 
        println(cmd) 
        "echo recieve message from someone" ! 
        execute(cmd.toString) 
      } 
    } 

    def execute(cmd: String) { 
      val process = Process(cmd.toString) 
      process ! ProcessLogger(_ => {}) 
    } 
  } 

}

1 个答案:

答案 0 :(得分:5)

它将execute(cmd.toString)解释为!的参数,因为换行符不一定结束语句。要解决此问题,请不要使用postfix语法,因为某个原因而弃用了该语法:

def receive = { 
  case Command(cmd) => { 
    println(cmd) 
    "echo recieve message from someone".! 
    execute(cmd.toString) 
  } 
}