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

12,598

Solution 1

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

Solution 2

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

});

Solution 3

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 !");
      }
    }
  );

Solution 4

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

Share:
12,598
bdeveloper01
Author by

bdeveloper01

Hi! I am a Software Engineer. Thanks

Updated on July 29, 2022

Comments