简体   繁体   English

播放2.1 Scala连锁多个期货和承诺

[英]Play 2.1 Scala chain multiple futures and promises

I have an existing java class for processing data. 我有一个用于处理数据的现有java类。 How could I use the ObjectNode from first promise and so more processing in my Scala Async action? 我如何使用第一个承诺中的ObjectNode以及我的Scala异步操作中的更多处理?

public class JavaClass extends Controller {
  public static Promise<ObjectNode> intensiveComputation(String imageId) {
  }
}


def index = Action {
  val futureInt = scala.concurrent.Future { JavaClass.intensiveComputation() }
  Async {
    futureInt.map(promise => 
      var objJs = promise.GetObjectNodeFromPromise()
      (objJs \ Config.RESP_STATUS_PARAM).as[String] match {
        // I WANT TO READ ObjectNode from promise and do more works here
      }
      Ok(Json.toJson(Map("status" -> "ok")))
  }
}

EDIT 1 编辑1

I tried with @hbf code, however, I got compiling error one this line. 我尝试使用@hbf代码,然而,我在这一行编译了错误。

[error]  found   : org.codehaus.jackson.node.ObjectNode => play.api.mvc.SimpleResult[play.api.libs.json.JsValue]
[error]  required: play.libs.F.Function[org.codehaus.jackson.node.ObjectNode,?]
[error]                 var result = futureObj map { objJs: ObjectNode =>

If I remove ObjectNode from objJs, I got this error. 如果我从objJs中删除ObjectNode,我收到此错误。

[error]  missing parameter type
[error]                 var result = futureObj map { objJs =>

New Code 新规范

def index = Action {
  val futureInt = JavaClass.intensiveComputation()
  Async {
    var result = futureObj map { objJs: ObjectNode =>
      Ok(Json.toJson(Map("status" -> "ok")))
    }
    result
}

I assume you are following the Play documentation guide , right? 我假设您正在关注Play文档指南 ,对吧?

First of all, notice that Play is now (from version 2.1 on) using Scala futures and due to this, the nomenclature changed: your method intensiveComputation() returns a (Scala) Future<ObjectNode> (while in pre-2.1, this was called Promise<ObjectNode> ). 首先,请注意Play现在(使用版本2.1)使用Scala期货,因此,命名法改变了:您的方法intensiveComputation()返回(Scala) Future<ObjectNode> (在2.1之前,这是叫做Promise<ObjectNode>

public class JavaClass extends Controller {
  public static Future<ObjectNode> intensiveComputation(String imageId) {
    /* ... */
  }
}

Also, note that in the Play documentation example , intensiveComputation() returns the value (ie, ObjectNode ) directly, while your version returns a future holding the value (ie, Future<ObjectNode> ). 另请注意,在Play文档示例中intensiveComputation()返回值(即ObjectNode ),而您的版本返回保存该值的未来(即Future<ObjectNode> )。

Second, in your futureInt.map , the closure receives the value of the future and not the future itself. 其次,在futureInt.map ,闭包接收未来的价值而不是未来的价值。 So try something like: 所以尝试类似的东西:

def index = Action {
  val futureInt = JavaClass.intensiveComputation() // I's already a future!
  Async {
    futureInt.map(objJs => // `objJs` is the result of `intensiveComputation`
      // Extract from `objJs` whatever you need ...
      // ... and make the `Ok` call here (and not outside)
      Ok(Json.toJson(Map("status" -> "ok")))
    )
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM