Scala Lift-运行系统命令并等待完成
我当前使用的类位于以下位置: 一切都很好,但我需要代码等待命令完成,然后返回true/false类型响应,这样我就可以继续处理结果 我将它与OpenOffice结合使用来转换一些文档,因此在使用生成的文件之前等待文档转换完成是至关重要的 感谢您的帮助,非常感谢:)Scala Lift-运行系统命令并等待完成,scala,cmd,lift,Scala,Cmd,Lift,我当前使用的类位于以下位置: 一切都很好,但我需要代码等待命令完成,然后返回true/false类型响应,这样我就可以继续处理结果 我将它与OpenOffice结合使用来转换一些文档,因此在使用生成的文件之前等待文档转换完成是至关重要的 感谢您的帮助,非常感谢:) 代码应该已经等待命令完成,因为receiveWithin是一种阻塞方法。您只需在sleep 10和println之前和之后运行示例main函数,就可以自己进行测试 从run函数中获取布尔值结果应该非常简单,您只需对run函数进行一个
receiveWithin
是一种阻塞方法。您只需在sleep 10
和println
之前和之后运行示例main
函数,就可以自己进行测试李>
布尔值
结果应该非常简单,您只需对run函数进行一个小的修改运行功能:
def run(command:String) : Boolean = {
println("gonna runa a command: " + Thread.currentThread)
val args = command.split(" ")
val processBuilder = new ProcessBuilder(args: _* )
processBuilder.redirectErrorStream(true)
val proc = processBuilder.start()
//Send the proc to the actor, to extract the console output.
reader ! proc
//Receive the console output from the actor.
//+========== Begin Modified Section ==============+
//Here, we'll store the result instead of printing it, storing a None
//if there was a timeout
var commandResult : Option[String] = None
receiveWithin(WAIT_TIME) {
case TIMEOUT => commandResult = None
case result:String => commandResult = Some(result)
}
//Here we interpret the result to return our boolean value
commandResult match {
case None => false
case Some(s) => //... You'll have to transform the result to a true/false
//however is most applicable to your use case
}
}
代码应该已经等待命令完成,因为receiveWithin
是一种阻塞方法。您只需在sleep 10
和println
之前和之后运行示例main
函数,就可以自己进行测试李>
从run函数中获取布尔值
结果应该非常简单,您只需对run函数进行一个小的修改
新的运行功能:
def run(command:String) : Boolean = {
println("gonna runa a command: " + Thread.currentThread)
val args = command.split(" ")
val processBuilder = new ProcessBuilder(args: _* )
processBuilder.redirectErrorStream(true)
val proc = processBuilder.start()
//Send the proc to the actor, to extract the console output.
reader ! proc
//Receive the console output from the actor.
//+========== Begin Modified Section ==============+
//Here, we'll store the result instead of printing it, storing a None
//if there was a timeout
var commandResult : Option[String] = None
receiveWithin(WAIT_TIME) {
case TIMEOUT => commandResult = None
case result:String => commandResult = Some(result)
}
//Here we interpret the result to return our boolean value
commandResult match {
case None => false
case Some(s) => //... You'll have to transform the result to a true/false
//however is most applicable to your use case
}
}
哇,非常感谢你抽出时间来帮助我!非常感谢:)新年快乐哇,非常感谢你抽出时间来帮助我!非常感谢:)新年快乐