我正在为一位家长创建一名儿童演员。我的子演员执行一些业务逻辑并将值返回给 scala Future。当我向Future
我的父母发送消息时,我无法收到我未来的消息。以下是我的代码:
儿童演员
public class FetchDevicesIds extends AbstractActor {
private final LoggingAdapter LOG = Logging.getLogger(context().system(), this);
private final ActorRef parent = context().parent();
@Override
public PartialFunction<Object, BoxedUnit> receive() {
return ReceiveBuilder.
match(String.class, msg -> {
final ExecutionContext ec = context().dispatcher();
Future<DevicesIds> future = Futures.future(() -> new DevicesIds(new ArrayList<>()), ec);
future.onFailure(futureFailureHandler(), ec);
System.out.println("************************************ : "+parent);
pipe(future, ec).to(parent);
}).
matchAny(msg -> LOG.info("unknown message: "+ msg)).
build();
}
private OnFailure futureFailureHandler(){
return new OnFailure() {
@Override
public void onFailure(Throwable failure) throws Throwable {
if(failure.getCause() instanceof DevicesNotFound){
self().tell("-----------------", ActorRef.noSender());
}
}
};
}}
父演员
public class NotificationSupervisor extends AbstractActor {
private final LoggingAdapter LOG = Logging.getLogger(context().system(), this);
private final ActorContext context = context();
@Override
public PartialFunction<Object, BoxedUnit> receive() {
return ReceiveBuilder.
match(String.class, msg -> {
ActorRef fetchDeviceIds = context.actorOf(Props.create(FetchDevicesIds.class), "fetch-devices-ids");
fetchDeviceIds.tell("fetch-ids", self());
}).
match(DevicesIds.class, ids -> System.out.println("&&&&&&&&&&&&& I GOT IT")).
matchAny(msg -> LOG.info("unknown message: "+ msg)).
build();
}
日志
[INFO] [08/21/2016 13:04:10.776] [ActorLifeCycleTest-akka.actor.default-dispatcher-4] [akka://ActorLifeCycleTest/user/notification-supervisor]
Message [java.lang.Integer] from Actor[akka://ActorLifeCycleTest/deadLetters] to TestActor[akka://ActorLifeCycleTest/user/notification-supervisor] was not delivered. [1] dead letters encountered.
This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'
更新
我正在尝试发送tell
给父母而不是未来,但父母仍然没有收到消息。以下是我的更改:
parent.tell(23, ActorRef.noSender()); //replace pipe(future, ec).to(parent);
预期,父matchAny(msg -> {System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");LOG.info("unknown message: "+ msg);})
案例处理此消息。但什么也没有发生。
更新 2
根据我的调查,当我注释掉future.onFailure(futureFailureHandler(), ec);
语句时,parent.tell(23, ActorRef.noSender());
执行成功。仍然不明白为什么会发生这种情况。
我的要求是,将未来的消息发送给父actor,并在akkaactor系统中处理未来的容错故障。