为什么80%的码农都做不了架构师?>>>   

Handling asynchronous results

处理异步results

Why asynchronous results?

为啥需要异步results

Until now, we were able to compute the result to send to the web client directly. This is not always the case: the result may depend of an expensive computation or on a long web service call.

到目前为止,我们可以直接计算出发送到web客户端的结果。而不会总是这样:结果需要经过耗时的计算或web service调用。

Because of the way Play 2.0 works, action code must be as fast as possible (i.e. non blocking). So what should we return as result if we are not yet able to compute it? The response should be a promise of a result!

play2.0要求action代码必须尽可能的快(例如非阻塞),如果我们一时还不能计算它,那么我们应该返回什么?答案是一个结果的Promise(承诺)!

Promise<Result> will eventually be redeemed with a value of type Result. By giving a Promise<Result> instead of a normal Result, we are able to compute the result quickly without blocking anything. Play will then serve this result as soon as the promise is redeemed.

一个 Promise<Result> 实际上最终会返回一个 Result值。用 Promise<Result> 代替Result,我们就能够无阻塞的快速计算结果。一旦promise完成了,play就会返回这个结果。

The web client will be blocked while waiting for the response but nothing will be blocked on the server, and server resources can be used to serve other clients.

web客户端在等待应答的时候会被阻塞住,但是服务器端不会阻塞,服务器还能给其他客户端提供服务.

How to create a Promise<Result>

如何创建 Promise<Result>

To create a Promise<Result> we need another promise first: the promise that will give us the actual value we need to compute the result:

为了创建一个 Promise<Result> ,我们首先需要另外一个promise:这个promise将会返回一个需要计算结果的实际值:

Promise<Double> promiseOfPIValue = computePIAsynchronously();

Promise<Result> promiseOfResult = promiseOfPIValue.map(

new Function<Double,Result>() {

public Result apply(Double pi) {

return ok("PI value computed: " + pi);

}

} );

Note: Writing functional composition in Java is really verbose for at the moment, but it should be better when Java supports lambda notation.

注意:目前在java里写函数组合非常的罗嗦,但是等java支持lamba表达式的时候情况会好一些。

Play 2.0 asynchronous API methods give you a Promise. This is the case when you are calling an external web service using the play.libs.WS API, or if you are using Akka to schedule asynchronous tasks or to communicate with Actors usingplay.libs.Akka.

当你用 play.libs.WS API调用外部的webservice或者用AKKA调度异步任务或使用play.libs.Akka进行异步通讯时,Play 2.0异步API方法就会返回一个 Promise.

A simple way to execute a block of code asynchronously and to get a Promise is to use the play.libs.Akka helpers:

异步的执行一个代码块的简单方式是使用 play.libs.Akka 便利方法:

Promise<Integer> promiseOfInt = Akka.future(

    new Callable<Integer>() {

        public Integer call() {

            intensiveComputation();

        }

   } );

Note: Here, the intensive computation will just be run on another thread. It is also possible to run it remotely on a cluster of backend servers using Akka remote.

注意:这里,密集的计算会在其他线程里执行。也有可能在后端服务器集群上远程执行.

AsyncResult

异步结果

While we were using Results.Status until now, to send an asynchronous result we need an Results.AsyncResult that wraps the actual result:

当我们使用 Results.Status 时,为了发送一个异步result,我们需要一个包含了实际的result的 Results.AsyncResult :

public static Result index() {

Promise<Integer> promiseOfInt = Akka.future(

new Callable<Integer>() {

public Integer call() { 

intensiveComputation();

}

}

);

async(

promiseOfInt.map(

new Function<Integer,Result>() {

public Result apply(Integer i) {

return ok("Got result: " + i);

}

}

)

);

}

Note: async() is an helper method building an AsyncResult from aPromise<Result>.

注意: async() 是一个根据Promise<Result>构建 AsyncResult 的便利方法。

转载于:https://my.oschina.net/gai/blog/54029

play2.0文档-面向java开发者(7)异步HTTP编程相关推荐

  1. JDK文档是Java程序开发不可缺少的编程词典

    Java 构建商业应用程序或者某种物联网设备,构建了Android 操作系统,它为智能手机行业做出了杰出的贡献.借助 Android 操作系统,手机厂商才得以在早期与黑莓抗衡,之后又能够打破苹果的主导 ...

  2. 如何写Java文档注释(Java Doc Comments)

    本文翻译自How to Write Doc Comments for the Javadoc Tool,但是精简了一些私以为不重要的东西 本文不讨论如何使用javadoc工具自动生成文档的方法,而是主 ...

  3. AKKA文档(java版)——准备开始

     http://ifeve.com/akka-doc-java-getting-started/ AKKA文档(java版)--准备开始 原文:http://doc.akka.io/docs/ak ...

  4. 支付宝商户代扣2.0文档

    https://docs.alipay.com/pre-open/20170601105911096277/wob270 支付宝商户代扣2.0文档

  5. Selenium用户指南 - 第一章 Selenium 2.0 文档修订注解

    From: http://blog.csdn.net/planisnothing/article/details/7234995 Selenium 2.0 文档修订注解 你好,欢迎!文档团队欢迎你,并 ...

  6. html如何查看字体样式,css2.0文档查阅及字体样式

    css2.0文档查阅下载     网址:http://soft.hao123.com/soft/appid/9517.html 引入外部样式 这个是正常. 这个是粗体. 这个是粗体 倾斜. varia ...

  7. Beautiful Soup 4.4.0 文档

    Beautiful Soup 4.4.0 文档 文章目录 Beautiful Soup 4.4.0 文档 @[toc] 快速开始 安装 Beautiful Soup 安装完成后的问题 安装解析器 如何 ...

  8. Beautiful Soup 4.4.0 文档 — beautifulsoup 4.4.0q 文档

    Beautiful Soup 4.4.0 文档¶ Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的 ...

  9. 《设备管理系统》设计文档【Java+Eclipse】

    <设备管理系统>设计文档[Java+Eclipse] 文章目录 <设备管理系统>设计文档[Java+Eclipse] 一.项目介绍 二.Login设计 1.账户登录(登录和退出 ...

最新文章

  1. android 图片过滤,android 图片转为bitmap,黑白镜过滤
  2. 【每日DP】day6 P1541 乌龟棋(四维DP)难度⭐⭐⭐
  3. python 可变 与 不可变类型
  4. 使用DBUA单一实例如何升级至Oracle数据库12c
  5. 【随机过程】随机过程之泊松过程的推广
  6. 分布式测试工具Beetle.DT的部署并进行HTTP,SQL,TCP压测
  7. Hibernate二级/查询缓存的陷阱
  8. python中import sys_python import sys出错怎么办
  9. LintCode MySQL 1932/1933. 挂科最多的同学 I / II
  10. CreateCompatibleBitmap
  11. 嵌入式电路设计(dsp电路设计)
  12. elasticsearch的javaAPI之index
  13. 2018春招-赛码网-例题-水仙花数
  14. 女员工有问题,责任在于头目不管事
  15. Oracle 数据库 各种索引类型 简单介绍
  16. 听说MACD是技术指标之王?我们用Python来验验成色
  17. java fact函数_Fact 定义一个类Fact表示无符号有理数(分数) 联合开发网 - pudn.com...
  18. 这台计算机没有连接到网络怎么办,通过路由器连接电脑连不上网怎么办_台式连接路由器没有网解决方法-系统城...
  19. 校园二手交易平台毕设 校园二手商城毕业设计 毕设二手校园商品交易 二手商城系统 java二手交易市场 springboot商城 ssm二手交易平台下载 源代码+数据库+调试运行+讲解代码
  20. 一个案例告诉你如何使用 Kyligence + Spark 进行大数据机器学习

热门文章

  1. Enum类的非一般用法汇总(工作中遇到时持续更新)
  2. 每日英语:Would We Be Healthier With A Vegan Diet?
  3. HDU-1162-Eddy's picture
  4. web服务器安全配置(附安全工具下载)
  5. 动态库的编写和调用 - Delphi
  6. 设置maxJsonLength,解决ajax通过POST方式调用.net的webService时,数据过长时服务器返回500错误的问题
  7. 解决修改css或js文件,浏览器缓存更新问题。
  8. Python 使用sys.exc_info自己捕获异常详细信息
  9. 支付宝 APP支付(证书)方式遇到的问题和解决办法
  10. 企业应用程序部署在iOS 7.1上不起作用