How to create a Promise<Result> play2.0 framework - Java
Asked Answered
P

4

8

How to create a Promise<Result> in Play 2.0 framework - Java

I see this link http://www.playframework.org/documentation/2.0.4/JavaAsync. it doesn't contain that much details. do you known any tutorial than refer to me. pls.

Pascasia answered 3/10, 2012 at 12:31 Comment(0)
G
7

James Roper (a Play Framework developer) has a good example for using Promise in Play with Java: https://github.com/jroper/play-promise-presentation/blob/master/src/main/java/controllers/Application.java

Glasshouse answered 3/10, 2012 at 17:59 Comment(1)
I've never been able to get this kind of code working, where is async() defined?Elapse
C
8

This is new way of creating Promise in Play 2.2

Promise<Boolean> myPromise = Promise.promise(new Function0<Boolean>() {
    public Boolean apply() throws Throwable {
        // TODO - Add Implementation here.
        return Boolean.TRUE;
    }

});
Chess answered 25/11, 2013 at 4:25 Comment(1)
This almost worked for me, I found I needed to use F.Function0 and not Function0.Autograft
G
7

James Roper (a Play Framework developer) has a good example for using Promise in Play with Java: https://github.com/jroper/play-promise-presentation/blob/master/src/main/java/controllers/Application.java

Glasshouse answered 3/10, 2012 at 17:59 Comment(1)
I've never been able to get this kind of code working, where is async() defined?Elapse
P
4

As explained in the doc that you mention, use an Akka.future:

Promise<Result> promiseOfResult = Akka.future(
    new Callable<Result>() {
      public Result call() {
        return ok("This is a promise result !");
      }
    }
  );
Pears answered 3/10, 2012 at 12:52 Comment(2)
I've just tried to use it in play 2.2 and I've noticed method Akka.future is deprecated.Sternwheeler
Every time I blink something in Play gets deprecated.Cancellation
E
1
public F.Promise<Result> asyncFoo() {

          F.Promise<Integer> promise = F.Promise.promise(() -> longRunningCalculation());

          return promise.map((Integer i) -> ok("The calculation result is: " + i));

}

https://www.typesafe.com/blog/play-framework-with-java-8

Ejaculate answered 26/10, 2015 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.