Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you use await in a controller it does bytecode enhancement to break a single method into two threads. This is pretty cool, but definitely one of the 'black magic' tricks of Play1. But, this is one place where Play often acts weird and requires a restart (or as you found, some code changing) - the other place it can act strange is when you change a Model class. </p> <p><a href="http://www.playframework.com/documentation/1.2.5/asynchronous#SuspendingHTTPrequests" rel="nofollow">http://www.playframework.com/documentation/1.2.5/asynchronous#SuspendingHTTPrequests</a></p> <blockquote> <p>To make it easier to deal with asynchronous code we have introduced continuations. Continuations allow your code to be suspended and resumed transparently. So you write your code in a very imperative way, as:</p> <p>public static void computeSomething() { Promise delayedResult = veryLongComputation(…); String result = await(delayedResult); render(result); } </p> <p>In fact here, your code will be executed in 2 steps, in 2 different hreads. But as you see it, it’s very transparent for your application code.</p> <p>Using await(…) and continuations, you could write a loop:</p> </blockquote> <pre><code> public static void loopWithoutBlocking() { for(int i=0; i&lt;=10; i++) { Logger.info(i); await("1s"); } renderText("Loop finished"); } </code></pre> <blockquote> <p>And using only 1 thread (which is the default in development mode) to process requests, Play is able to run concurrently these loops for several requests at the same time.</p> </blockquote> <hr> <p>To respond to your comment: </p> <pre><code> public static void generatePDF(Long reportId) { Promise&lt;InputStream&gt; pdf = new ReportAsPDFJob(report).now(); InputStream pdfStream = await(pdf); renderBinary(pdfStream); </code></pre> <p>and ReportAsPDFJob is simply a play Job class with doJobWithResult overridden - so it returns the object. See <a href="http://www.playframework.com/documentation/1.2.5/jobs" rel="nofollow">http://www.playframework.com/documentation/1.2.5/jobs</a> for more on jobs. </p> <p>Calling <code>job.now()</code> returns a future/promise, which you can use like this: <code>await(job.now())</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload