您的位置:首页 > 理论基础 > 计算机网络

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

2012-04-18 22:52 344 查看

Handlingasynchronousresults

处理异步results

Whyasynchronousresults?

为啥需要异步results

Untilnow,wewereabletocomputetheresulttosendtothewebclientdirectly.Thisisnotalwaysthecase:theresultmaydependofanexpensivecomputationoronalongwebservicecall.

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

BecauseofthewayPlay2.0works,actioncodemustbeasfastaspossible(i.e.nonblocking).Sowhatshouldwereturnasresultifwearenotyetabletocomputeit?Theresponseshouldbeapromiseofaresult!

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

APromise<Result>willeventuallyberedeemedwithavalueoftypeResult.BygivingaPromise<Result>insteadofanormalResult,weareabletocomputetheresultquicklywithoutblockinganything.Playwillthenservethisresultassoonasthepromiseisredeemed.

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

Thewebclientwillbeblockedwhilewaitingfortheresponsebutnothingwillbeblockedontheserver,andserverresourcescanbeusedtoserveotherclients.

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

HowtocreateaPromise<Result>

如何创建Promise<Result>

TocreateaPromise<Result>weneedanotherpromisefirst:thepromisethatwillgiveustheactualvalueweneedtocomputetheresult:

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

Promise<Double>promiseOfPIValue=computePIAsynchronously();

Promise<Result>promiseOfResult=promiseOfPIValue.map(

newFunction<Double,Result>(){

publicResultapply(Doublepi){

returnok("PIvaluecomputed:"+pi);

}

});

Note:WritingfunctionalcompositioninJavaisreallyverboseforatthemoment,butitshouldbebetterwhenJavasupportslambdanotation.

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

Play2.0asynchronousAPImethodsgiveyouaPromise.Thisisthecasewhenyouarecallinganexternalwebserviceusingtheplay.libs.WSAPI,orifyouareusingAkkatoscheduleasynchronoustasksortocommunicatewithActorsusingplay.libs.Akka.

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

AsimplewaytoexecuteablockofcodeasynchronouslyandtogetaPromiseistousetheplay.libs.Akkahelpers:

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

Promise<Integer>promiseOfInt=Akka.future(

newCallable<Integer>(){

publicIntegercall(){

intensiveComputation();

}

});

Note:Here,theintensivecomputationwilljustberunonanotherthread.ItisalsopossibletorunitremotelyonaclusterofbackendserversusingAkkaremote.

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

AsyncResult

异步结果

WhilewewereusingResults.Statusuntilnow,tosendanasynchronousresultweneedanResults.AsyncResultthatwrapstheactualresult:

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

publicstaticResultindex(){

Promise<Integer>promiseOfInt=Akka.future(

newCallable<Integer>(){

publicIntegercall(){

intensiveComputation();

}

}

);

async(

promiseOfInt.map(

newFunction<Integer,Result>(){

publicResultapply(Integeri){

returnok("Gotresult:"+i);

}

}

)

);

}

Note:async()isanhelpermethodbuildinganAsyncResultfromaPromise<Result>.

注意:async()是一个根据Promise<Result>构建AsyncResult的便利方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: