[Play 2.5] Get actual result from CompletableFuture<Result>

2,343 views
Skip to first unread message

adakbar

unread,
Jun 2, 2016, 2:06:14 PM6/2/16
to play-framework
While reading code from official doc on accessing webservices, I tried to access local service like so.

public Result index() {

    WSRequest request = ws.url("http://localhost/fu.php");

    CompletionStage<Result> responsePromise = request.get()
        .thenApply(response -> ok("RESULT " + response));

    System.out.println(responsePromise);

    try {
        Thread.sleep(2000);
    } catch(InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
    
    System.out.println(responsePromise);

    return responsePromise;
}


Notice that 2 System.out.println() that printed in cmd.

java.util.concurrent.CompletableFuture@70becf94[Not completed]
java.util.concurrent.CompletableFuture@70becf94[Completed normally]

I thought when completed normally, I'll get actual result, but I got compilation error instead, by the way, that output actually come when I change method type to CompletableFuture<Result> not Result like in my code example above

java.util.concurrent.CompletionStage<play.mvc.Result> cannot be converted to play.mvc.Result

Then I'm thinking, in CompletableFuture doc there is method called get() in responsePromise but when use it, it says there's no method.
So the question is, how I get actual result from this? I'm sorry if this is a trivial question, my understanding in promise and future, and also in Play still bad.

Thanks in advance.

Greg Methvin

unread,
Jun 2, 2016, 3:13:00 PM6/2/16
to play-framework
If you're using a CompletionStage, you should make your method return CompletionStage<Result>, return the CompletionStage. CompletionStage is just the interface that CompletableFuture implements. You can use responsePromise.toCompletableFuture() to convert it, but typically you shouldn't have to do that.

Also, it's a bad idea to wait (call get()) on the CompletableFuture, since that's unnecessarily blocking the thread. If you return the CompletionStage directly from your action method, Play will send your response when it's available, without blocking.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/e0646a6f-f220-4032-9e57-308e80c7b2bc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Senior Software Engineer

adakbar

unread,
Jun 2, 2016, 3:55:10 PM6/2/16
to play-framework
 If you return the CompletionStage directly from your action method, Play will send your response when it's available, without blocking.

This is the part that I don't really understand, looking at output that Completed normally
I can actually use the actual result from the url call (in this case it return a string), right?
and when action is complete in browser printed like this.

RESULT play.libs.ws.ahc.AhcWSResponse@4c59194

Not the actual string that I should get.

rta...@twitter.com

unread,
Jun 2, 2016, 6:31:47 PM6/2/16
to play-framework
Instead of:

public Result index() {/* ... */ }

Your return type is CompletionStage<Result>:

public CompletionStage<Result> index() { /* ... */ }

The use of async results is documented here: https://www.playframework.com/documentation/2.5.x/JavaAsync

Greg Methvin

unread,
Jun 2, 2016, 6:57:00 PM6/2/16
to play-framework
On Thu, Jun 2, 2016 at 12:55 PM, adakbar <adamak...@gmail.com> wrote:
 If you return the CompletionStage directly from your action method, Play will send your response when it's available, without blocking.

This is the part that I don't really understand, looking at output that Completed normally
I can actually use the actual result from the url call (in this case it return a string), right?
and when action is complete in browser printed like this.

RESULT play.libs.ws.ahc.AhcWSResponse@4c59194

Not the actual string that I should get.

No, it doesn't return a String. It returns a WSResponse, which is a representation of the HTTP response. Do you mean to get the body of the response?

adakbar

unread,
Jun 2, 2016, 10:26:49 PM6/2/16
to play-framework
Yes, I mean the body of response.
Reply all
Reply to author
Forward
0 new messages