FAQ

[play-framework] [2.1.0 Java] Waiting on a webservice promise

Chanan Braunstein
Apr 15, 2013 at 8:57 pm
Hello,

We have code that looks something like this:

Promise<Boolean> promise =
WS.url("http://localhost:9001/WebService/InsertTestScore")
.setHeader("Content-Type", "application/x-www-form-urlencoded")
.post(post).map(
new Function<WS.Response, Boolean>() {
public Boolean apply(WS.Response response) {
Logger.info("here");
return true;
}
}
);

We know the code is not timing out because we see the "here" being printed
to the log.

If we call promise.get(); we get a TimeoutException. How do we get the
return from the promise?

Chanan.

--
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-framework+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
reply

Search Discussions

8 responses

  • Martin Grotzke at Apr 15, 2013 at 9:41 pm
    You have to map the promise to the Result, s.th. like this:

    return async(promise.map(new Function<Boolean, Result>() {
    public Result apply(Boolean a) {
    return ok("Got " + a);
    }
    });

    Cheers,
    Martin

    On 04/15/2013 10:57 PM, Chanan Braunstein wrote:
    Hello,

    We have code that looks something like this:

    Promise<Boolean> promise =
    WS.url("http://localhost:9001/WebService/InsertTestScore")
    .setHeader("Content-Type", "application/x-www-form-urlencoded")
    .post(post).map(
    new Function<WS.Response, Boolean>() {
    public Boolean apply(WS.Response response) {
    Logger.info("here");
    return true;
    }
    }
    );

    We know the code is not timing out because we see the "here" being
    printed to the log.

    If we call promise.get(); we get a TimeoutException. How do we get the
    return from the promise?

    Chanan.

    --
    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-framework+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    inoio gmbh - http://inoio.de
    Schulterblatt 36, 20357 Hamburg
    Amtsgericht Hamburg, HRB 123031
    Geschäftsführer: Dennis Brakhane, Martin Grotzke, Ole Langbehn
  • Chanan Braunstein at Apr 16, 2013 at 10:45 am
    I do not want to return the promise. We may need to do other processing on
    the result before the action is complete.

    Ok, so the development settings seem to be the issue here, is there a way
    to change that in development?

    On Monday, April 15, 2013 8:02:51 PM UTC-4, Martin Grotzke wrote:
    On 04/16/2013 12:21 AM, Chanan Braunstein wrote:
    Yes, but I don't want to map the promise to the result. That assumes the
    code is inside the controller which in this case it is not, its in
    another class.
    Then return the promise from the method.
    I am confused is to why mapping to a Boolean would cause
    a timeout.
    In dev mode play runs with 1 thread, and this is blocked by the request
    thread.

    Cheers,
    Martin
    --
    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-framework+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Martin Grotzke at Apr 16, 2013 at 11:46 am

    On 04/16/2013 12:45 PM, Chanan Braunstein wrote:
    I do not want to return the promise. We may need to do other processing
    on the result before the action is complete.
    Using promises in "lower" levels of the app is kind of viral. Depending
    on the app characteristics you can actually benefit from it. Of course
    with java the verbosity is annoying.

    So, your dependent processing should also work with promises (and map /
    flatMap).
    In our app we're returning promises from backend calls (search engine,
    database, webservices) and use map/flatMap to process results.
    E.g. the searchengine returns a Promise<List<ProductId>>, the products
    are loaded from the database via s.th. like
    productIdsPromise.flatMap(
    new Function<List<ProductId>, List<Product>>() {..}
    )
    and _in parallel_ (this is the benefit) additional data (user generated
    content) is loaded via a similar call.

    Ok, so the development settings seem to be the issue here, is there a
    way to change that in development?
    I don't know.

    Cheers,
    Martin


    On Monday, April 15, 2013 8:02:51 PM UTC-4, Martin Grotzke wrote:
    On 04/16/2013 12:21 AM, Chanan Braunstein wrote:
    Yes, but I don't want to map the promise to the result. That
    assumes the
    code is inside the controller which in this case it is not, its in
    another class.
    Then return the promise from the method.
    I am confused is to why mapping to a Boolean would cause
    a timeout.
    In dev mode play runs with 1 thread, and this is blocked by the request
    thread.

    Cheers,
    Martin
  • Chanan Braunstein at Apr 16, 2013 at 2:31 pm
    Right, I first mapped the the initial response from WS.

    At some point I need to resolve that promise, and I don't always want to do
    as a Result. It will not always be possible to return an Async Result. I
    understand that when possible it is best, but there cases when there will
    be a need to get to the actual response. Waiting on the response in this
    case should work and not give a timeout. I was going to see if it works
    when I do play start, but if you look at the other thread I started, I
    wasn't able to do that since play start failed.

    --
    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-framework+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Guillaume Bort at Apr 16, 2013 at 2:37 pm
    It gives a timeout because you are creating a deadlock. If you don't want
    to deal with Promise, you shouldn't use them at all and you should make the
    webservice call blocking like:

    WS.url("http://localhost:9001/WebService/InsertTestScore")
    .setHeader("Content-Type", "application/x-www-form-urlencoded")
    .post(post).get()

    On Tue, Apr 16, 2013 at 4:31 PM, Chanan Braunstein wrote:

    Right, I first mapped the the initial response from WS.

    At some point I need to resolve that promise, and I don't always want to
    do as a Result. It will not always be possible to return an Async Result. I
    understand that when possible it is best, but there cases when there will
    be a need to get to the actual response. Waiting on the response in this
    case should work and not give a timeout. I was going to see if it works
    when I do play start, but if you look at the other thread I started, I
    wasn't able to do that since play start failed.

    --
    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-framework+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.



    --
    Guillaume Bort, http://guillaume.bort.fr

    --
    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-framework+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Chanan Braunstein at Apr 16, 2013 at 2:44 pm
    Hi Guilaume,

    My question is why does it block and fail though? It really shouldn't. If
    the promise is being resolved on another thread the promise.get should wait
    till it is done (with the timeout) and continue when it is done. I know the
    WS call finished already before I get the timeout exception, so why is it
    throwing the timeout?

    I am not sure yet what I want to do regarding a blocking call/non blocking.
    The piece of code I have there is just trying to figure out how things work
    and not code that is final, I cannot get any final code if I don't
    understand how things should work and why its failing.

    --
    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-framework+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Chanan Braunstein at Apr 16, 2013 at 6:14 pm
    When you say till the end, you mean returning an AsyncResult back to the
    framework?

    --
    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-framework+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Guillaume Bort at Apr 16, 2013 at 6:29 pm
    Yes I guess. It's the most common case with Play. Now you can do something
    else like executing a side-effect function when the Promise is redeemed. It
    could be write to a file, mutate some global state, anything you want.
    On Apr 16, 2013 8:14 PM, "Chanan Braunstein" wrote:

    When you say till the end, you mean returning an AsyncResult back to the
    framework?

    --
    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-framework+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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-framework+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post