Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Akka -- type mismatch; [error] found : Unit [error] required: scala.sys.process.ProcessLogger

I try to write example code to combine akka and actor. But I got the error message when compile the code.
The code is really simple as showed below.
So, What have I got wrong?

[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

The code is

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(_ => {}) 
    } 
  } 

}
like image 756
hellojinjie Avatar asked Oct 31 '25 13:10

hellojinjie


1 Answers

It's interpreting execute(cmd.toString) as the argument to !, because newlines don't necessarily end statements. To fix this, don't use postfix syntax, which is deprecated for a reason:

def receive = { 
  case Command(cmd) => { 
    println(cmd) 
    "echo recieve message from someone".! 
    execute(cmd.toString) 
  } 
}
like image 162
Hugh Avatar answered Nov 03 '25 19:11

Hugh