0

我正在为一位家长创建一名儿童演员。我的子演员执行一些业务逻辑并将值返回给 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系统中处理未来的容错故障。

4

1 回答 1

0

我仍然没有得到,上面的代码有什么确切的问题以及我如何解决这个问题。但是找到了另一种akka pipe使用java. 以下是代码:

public class FetchDevicesIds extends AbstractActor {

private final LoggingAdapter LOG = Logging.getLogger(context().system(), this);
private DeviceService deviceService = new DeviceServiceImpl();

@Override
public PartialFunction<Object, BoxedUnit> receive() {
    return ReceiveBuilder.
            match(String.class, msg -> {
                final ExecutionContext ec = context().system().dispatcher();
                CompletableFuture<DevicesIds> devicesIds = deviceService.getAllDevicesIds();
                pipe(devicesIds, ec).to(context().parent());
            }).
            matchAny(msg -> LOG.info("unknown message: "+ msg)).
            build();
}}

我们可以CompletableFuture使用模式直接将 Java 8 与 akka 一起使用import static akka.pattern.PatternsCS.pipe;。AkkaPatternsCS用于处理 Java 8 CompletableFuture

注意:在消息处理程序中使用context().parent()而不是在 Actor 中创建父实例,例如private final ActorRef parent = context().parent();. 我仍然不明白为什么会发生这种情况,但是有时使用父实例变量,该pipe模式不起作用。

于 2016-08-21T10:18:29.423 回答