我有一些与 akka 交互的旧 playframework 2.2 java webservice,现在我应该将它们移植到 playframework 2.3。
但是,异步已被弃用,即使在阅读了有关异步移植的文档(http://www.playframework.com/documentation/2.3.x/JavaAsync)之后,我也无法理解如何将其应用于我的案例(下面的代码):
- 在开始构建我的回复 (ok()) 之前,我必须等待超时/akka 服务器回复,否则我将阻塞线程。
我也应该让演员选择异步。
我也应该使akka服务器回复解析/回复构造异步
我环顾四周,即使在类型安全模板中也找不到这种交互的示例。
我怎么能那样做?
/* playframework 2.2 code */
public class Resolve extends Controller {
private final static String RESOLVER_ACTOR = play.Play.application().configuration().getString("actor.resolve");
@CorsRest
@VerboseRest
@RequireAuthentication
@BodyParser.Of(BodyParser.Json.class)
public static Result getJsonTree() {
JsonNode json = request().body().asJson();
ProtoBufMessages.ResolveRequest msg;
ResolveRequestInput input;
try {
input = new ResolveRequestInput(json);
} catch (rest.exceptions.MalformedInputException mie) {
return badRequest(mie.getMessage());
}
msg = ((ProtoBufMessages.ResolveRequest)input.getMessage());
ActorSelection resolver = Akka.system().actorSelection(RESOLVER_ACTOR);
Timeout tim = new Timeout(Duration.create(4, "seconds"));
Future<Object> fut = Patterns.ask(resolver, input.getMessage(), tim);
return async (
F.Promise.wrap(fut).map(
new F.Function<Object, Result>() {
public Result apply(Object response) {
ProtoBufMessages.ResolveReply rsp = ((ProtoBufMessages.ResolveReply)response);
ResolveOutput output = new ResolveOutput(rsp);
return ok(output.getJsonReply());
}
}
)
);
}
}