在Play Framework 2.2.2中,我想返回一个Promise。不过,我正在调用一个需要访问存储在Http.Context.current()
(当前登录用户,JPA连接...)中的变量的函数。如何在播放Promise中使用Http.Context.current()?
当然,由于Promise在另一个线程中执行,因此它无权访问Http.Context.current()
。我可以保留它在承诺,还是我应该手动恢复它?我应该使用另一种模式吗?
例子:
public static Promise<Result> getAvailableServices() {
return new Promise.promise(new Function0<Result>(){
@Override
public Result apply() throws Throwable {
// Long operation
List<Services> data = buildResult();
// Render the template
// (The header of the template requires access to
// Http.Context.current().args.get("usermodel"))
return Results.ok(services_template.render(services));
}
});
}
此问题可能被认为是http://stackoverflow.com/questions/17886630/play-2-1-1-java-can-i-access-http-context-current-from-an-任意异步 – Adrien
我们可以将另一个参数传递给Promise.promise(),ExecutionContext。 [HttpExecutionContext](http://www.playframework.com/documentation/2.2.1/api/scala/index.html#play.core.j.HttpExecutionContext)是答案的一部分吗? – Adrien