Skip to content

Instantly share code, notes, and snippets.

@netmarkjp
Created October 28, 2012 08:19
Show Gist options
  • Save netmarkjp/3968044 to your computer and use it in GitHub Desktop.
Save netmarkjp/3968044 to your computer and use it in GitHub Desktop.
PlayFramework 2.0.4 WS.get() sync/async
package controllers;
import java.util.Date;
import org.codehaus.jackson.node.ObjectNode;
import play.libs.F.Promise;
import play.libs.Json;
import play.libs.WS;
import play.mvc.Controller;
import play.mvc.Result;
/*
* write below in conf/application.conf
* play.akka.default-dispatcher.core-pool-size-max = 512
* play.akka.actor.retrieveBodyParserTimeout = 30s
* ws.timeout = 30000
*/
public class Application extends Controller {
public static Result delay() {
ObjectNode result = Json.newObject();
result.put("startAt", new Date().toLocaleString());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
result.put("endAt", new Date().toLocaleString());
return ok(result);
}
public static Result sync() {
String url = "http://localhost:9000/delay";
Promise<WS.Response> response = play.libs.WS.url(url).get();
return ok(response.get().asJson());
}
public static Result async() {
String url = "http://localhost:9000/delay";
return async(play.libs.WS
.url(url)
.get()
.map(new play.libs.F.Function<play.libs.WS.Response, play.mvc.Result>() {
public play.mvc.Result apply(play.libs.WS.Response response) {
return ok(response.asJson());
}
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment