Play framework async I/O gotchas
public class AsyncTest extends Controller
{
public static void gotchas()
{
params.put("foo", "bar"); // Check if changes to params survive the request being suspended
renderArgs.put("foo", "bar"); // Check if changes to renderArgs survive the request being suspended
final String foo = "bar"; // Must be declared final to use in the callback
F.Promise<WS.HttpResponse> remoteCall1 = WS.url("http://www.remote-service.com/data/1").getAsync();
F.Promise<WS.HttpResponse> remoteCall2 = WS.url("http://www.remote-service.com/data/2").getAsync();
F.Promise<WS.HttpResponse> remoteCall3 = WS.url("http://www.remote-service.com/data/3").getAsync();
F.Promise<List<WS.HttpResponse>> promises = F.Promise.waitAll(remoteCall1, remoteCall2, remoteCall3);
await(promises, new F.Action<List<WS.HttpResponse>>() // request gets suspended here
{
public void invoke(List<WS.HttpResponse> httpResponses)
{
System.out.println(params.get("foo")); // prints "bar"
System.out.println(renderArgs.get("foo")); // prints null
System.out.println(foo); // prints "bar"
render(httpResponses);
}
});
System.out.println("End"); // never called
}
}