Mark_H
Mark_H

Reputation: 790

Play framework 2, how to use execution context in Java?

The official documents only describe how to use it in scala. http://www.playframework.com/documentation/2.1.0/ThreadPools.

Future {
  // Some blocking or expensive code here
}(Contexts.myExecutionContext)

I can get the excutionContext like:

ExecutionContext myExecutionContext = Akka.system().dispatchers().lookup("my-context");

But how to add it in the code blow?

return async(
    future(new Callable<String>() {
        public String call() {
              return doSth();
        }).map(new F.Function<String,Result>() {
           public Result apply(String i) {
              return ok(i);
           }
        })  

Upvotes: 4

Views: 2598

Answers (1)

Mark_H
Mark_H

Reputation: 790

I think the answer should be:

    ExecutionContext myExecutionContext = Akka.system().dispatchers().lookup("my-context");
    return async(
            Akka.asPromise(Futures.future(new Callable<String>() {
              public String call() {
                return doSth();
              }   
            }, myExecutionContext)).map(new F.Function<String,Result>() {
              public Result apply(String i) {
                return ok(i);
              }
            })
    );

Upvotes: 5

Related Questions