FAQ

[play-framework] Re: [Java 2.1.x] How to return the result of one actor to another?

Johan Andren
Aug 8, 2013 at 2:35 pm
If you use forward the sender will be passed along so that if the child
sends a reply to sender it will be the original sender and not parent. See
the akka docs
here: http://doc.akka.io/docs/akka/2.2.0/java/untyped-actors.html#Forward_message

--
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

4 responses

  • Chanan Braunstein at Aug 8, 2013 at 3:56 pm
    This returns back a future, how do I convert it to my object? I tried a bit
    with the OnSuccess method, but cannot get it to work.

    --
    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 Aug 9, 2013 at 1:27 pm
    More specifically my code is:

    return async (
        Akka.asPromise(ask(gradeActor, getGrade, 10000)).map(
          new Function<Object, Result>() {
            @Override
               public Result apply(Object result) throws Throwable {
                  return ok(Json.toJson(result));
               }
          }
       )
    );

    When there was only one Actor in the chain, the json would be the result
    object of course. Now with the second actor and the forward, I am getting:
    {

        - completed: true

    }

    I assume that is a future, so my result is a Future<Future<Result>>? Just
    not sure how to unwrap that future.

    On Thursday, August 8, 2013 11:56:21 AM UTC-4, Chanan Braunstein wrote:

    This returns back a future, how do I convert it to my object? I tried a
    bit with the OnSuccess method, but cannot get it to work.
    --
    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 Aug 9, 2013 at 5:02 pm
    Hi,

    I guess that you have to use flatMap instead of map somewhere:

    http://www.playframework.com/documentation/api/2.1.x/java/play/libs/F.Promise.html#flatMap(play.libs.F.Function)

    On Fri, Aug 9, 2013 at 3:27 PM, Chanan Braunstein wrote:

    More specifically my code is:

    return async (
    Akka.asPromise(ask(gradeActor, getGrade, 10000)).map(
    new Function<Object, Result>() {
    @Override
    public Result apply(Object result) throws Throwable {
    return ok(Json.toJson(result));
    }
    }
    )
    );

    When there was only one Actor in the chain, the json would be the result
    object of course. Now with the second actor and the forward, I am getting:
    {

    - completed: true

    }

    I assume that is a future, so my result is a Future<Future<Result>>? Just
    not sure how to unwrap that future.

    On Thursday, August 8, 2013 11:56:21 AM UTC-4, Chanan Braunstein wrote:

    This returns back a future, how do I convert it to my object? I tried a
    bit with the OnSuccess method, but cannot get it to work.
    --
    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, @guillaumebort <http://twitter.com/guillaumebort> –
    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 Aug 9, 2013 at 5:32 pm
    Thanks Guillaume, got it! (And cleaned it up a tad)

    return async (
      Akka.asPromise(ask(gradeActor, getGrade, 10000))
      .flatMap(unWrapFuture())
      .map(toResult())
      );


    private static Function<Object, Promise<Object>> unWrapFuture() {
      return new Function<Object, Promise<Object>>() {
      @Override
      public Promise<Object> apply(Object arg0) throws Throwable {
      return Akka.asPromise((Future<Object>) arg0);
      }
      };
      }

      private static Function<Object, Result> toResult() {
      return new Function<Object, Result>() {
      @Override
      public Result apply(Object getGrade) throws Throwable {
      return ok(Json.toJson(getGrade));
      }
      };
      }




    On Friday, August 9, 2013 1:01:42 PM UTC-4, Guillaume Bort wrote:

    Hi,

    I guess that you have to use flatMap instead of map somewhere:


    http://www.playframework.com/documentation/api/2.1.x/java/play/libs/F.Promise.html#flatMap(play.libs.F.Function)


    On Fri, Aug 9, 2013 at 3:27 PM, Chanan Braunstein <cha...@gmail.com<javascript:>
    wrote:
    More specifically my code is:

    return async (
    Akka.asPromise(ask(gradeActor, getGrade, 10000)).map(
    new Function<Object, Result>() {
    @Override
    public Result apply(Object result) throws Throwable {
    return ok(Json.toJson(result));
    }
    }
    )
    );

    When there was only one Actor in the chain, the json would be the result
    object of course. Now with the second actor and the forward, I am getting:
    {

    - completed: true

    }

    I assume that is a future, so my result is a Future<Future<Result>>? Just
    not sure how to unwrap that future.

    On Thursday, August 8, 2013 11:56:21 AM UTC-4, Chanan Braunstein wrote:

    This returns back a future, how do I convert it to my object? I tried a
    bit with the OnSuccess method, but cannot get it to work.
    --
    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 <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.



    --
    Guillaume Bort, @guillaumebort <http://twitter.com/guillaumebort> –
    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.

Related Discussions

Discussion Navigation
viewthread | post