0

1 つの親に対して 1 つの子アクターを作成しています。私の子アクターはいくつかのビジネス ロジックを実行し、値を 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());正常に実行されます。なぜこれが起こるのかまだわかりません。

私の要件は、将来のメッセージを親アクターに送信し、akka アクター システムのフォールト トレランスのために将来の障害を処理することです。

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パターンを使用して akka でJava 8 を直接使用できますimport static akka.pattern.PatternsCS.pipe;。Akka PatternsCSfor handle Java 8 CompletableFuture.

注:context().parent()のようなアクターで親インスタンスを作成する代わりに、メッセージ ハンドラーで使用しますprivate final ActorRef parent = context().parent();。なぜこれが起こるのかはまだわかりませんが、親インスタンス変数を使用すると、pipeパターンが機能しません。

于 2016-08-21T10:18:29.423 に答える