In Play Framework 2.2.2, I'd like to return a Promise. However I'm calling a function which needs access to the variables stored in Http.Context.current()
(the current logged in user, the JPA connection...).
Of course, since the Promise is executed in another thread, it doesn't have access to Http.Context.current()
. Can I preserve it in the Promise, or should I restore it manually? Is there another pattern I should use?
Example:
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));
}
});
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…