我正在尝试使用Akka来处理轮询的REST调用。目前我让演员重新安排自己,直到检索到正确的答案。这可行,但最终结果永远不会返回。我该怎么做呢?如果这不是正确的方法应该怎么做呢?
我正在使用Java,因此任何代码都可以使用Java而不是Scala。
启动演员链:
Props props = Props.create(SpringExtProvider.get(myActorSystem).props("RefreshAccountActor").actorClass(), aggregatorSourceSessionManager.getSession().getSessionToken(), userSession.getSessionToken());
ActorRef ref = myActorSystem.actorOf(props, "ref");
FiniteDuration duration = FiniteDuration.create(180, TimeUnit.SECONDS);
Future<Object> siteAccountInfoFuture = Patterns.ask(ref, AccountMessages.SiteRefreshInfoMessage.toSiteRefreshInfoMessage(siteAccountInfo.getSiteRefreshInfo()), Timeout.durationToTimeout(duration));
siteAccountInfoFuture.onSuccess(new OnSuccess<Object>() {
public void onSuccess(Object result) {
// Update DB?
logger.info("Refresh successful");
//Do something with result
}
}, myActorSystem.dispatcher());
RefreshAccountActor:
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof SiteRefreshInfoMessage) {
SiteRefreshInfoMessage siteRefreshInfoMessage = (SiteRefreshInfoMessage) message;
if (!siteRefreshInfoMessage.getStatus().equals("COMPLETE")) {
// siteRefreshRetry is populated here
// Schedule another attempt
getContext().system().scheduler().scheduleOnce(
Duration.create(2500, TimeUnit.MILLISECONDS),
getSelf(), siteRefreshRetry,
getContext().dispatcher(), null),
getSelf();
} else {
// Refresh done
// This doesn't seem to get through
getSender().tell(siteRefreshInfoMessage, getSelf());
}
} else if (message instanceof SiteRefreshRetry) {
// Do REST call here to populate siteRefreshInfoMessage
// Now process message
onReceive(siteRefreshInfoMessage);
}
}
答案 0 :(得分:1)
我发现你的代码有两个问题。一,你不应该直接打电话给onReceive
。在从邮箱中出列消息后,由actor框架调用。如果两个处理需要共享逻辑,则创建一个可以调用的单独方法。其次,在安排任务时,您会丢失原始邮件的sender
。最后一个arg,您设置为null
应该设置为getSender()
。如果您这样做,那么sender
消息处理逻辑上将出现正确的SiteRefreshRetry
,然后可以对其进行响应。