Java: Async IO

30 views
Skip to first unread message

Navin Math

unread,
Jun 2, 2015, 7:03:56 AM6/2/15
to play-fr...@googlegroups.com

Hi
I wanted to understand the how to do non-blocking I/O.
I have login API:

public static Promise<Result> login() {
Logger.debug("Application.login()-------------- >");
Promise<String> promiseOfJson = Promise.promise(
new Function0<String>() {
@Override
public String apply() throws Throwable {
return loginService();
}
}
);
return after(promiseOfJson);
}

loginService() {

// Now this function calls other server to authenticate the credentials.. username and password, 
// the function call loginAuthenticateServer
Promise<JsonNode> promise = loginAuthenticateServer();

// now I need to wait here until I get the data

My Questions are:
DO I need to call wait() or get() on the promise
Is it correct way of doing Async IO
 
}

public static Promise<JsonNode> loginAuthenticateServer(String serverUrl, JsonNode requestBodyJson) {
Promise<WSResponse> response = WS.url(serverUrl)
.setContentType(RequestConstants.REQUEST_CONTENT_TYPE_JSON)
.post(requestBodyJson);
return response.map(new Function<WSResponse, JsonNode>() {

@Override
public JsonNode apply(WSResponse wsResponse) throws Throwable {
return wsResponse.asJson();
}
});
}
Reply all
Reply to author
Forward
0 new messages