Can you give me some links with good examples how to use continuations in play framework?(beside source of pla开发者_如何学Cy framework, their 'samples-and-tests' and on-site doc, already were there)
Any documentation and theory in "for dummies" format are also appreciated.
Continuations works mainly by using the await()
method that is made available through your Controller. The await method can accept two different types of parameter (there are actually 6 overloads of the method, but they are simple variations on the 2 themes).
The first is calling await with a timeout. This can be in milliseconds, or can be specified using a String literal expressing the time, e.g. 1s
for 1 second etc.
The second is calling await with a Future
object, and most commonly using Play's implementation of the java Future called Promise (in the libs.F). A Promise returns when the promise is fulfilled, in that the event that is invoked as part of the Promise is completed. However, a Promise can be more than a single event, it can be multiple events. There are even options to say waitAny, so that that it only waits for one of many events to return.
So, both approaches basically result in an event happening at some point in the future. The first is predetermined, the second depends on how long it takes for the Promise to be fulfilled.
Play continuations is a way to make the coding of this event structure easier. You can enter some code that says
// do some logic
await(timeout or promise);
// continue the execution
Behind the scenes, the HTTP thread is released, so that Play can process more concurrent requests more efficiently. And when the timeout or promise is fulfilled, the method continues to execute, without you having to code any specific handling for the thread of execution starting up again.
Taking the code from the Play site for continuations, it says
public static void loopWithoutBlocking() {
for(int i=0; i<=10; i++) {
Logger.info(i);
await("1s");
}
renderText("Loop finished");
}
This actually ends the thread of execution 10 times, and start a new thread after a 1 second wait. This whole thing is completely transparent from a programmers perspective, and allows you to build applications intuitively without worrying about creating non-blocking applications, as this is all magically handled by Play!
精彩评论