2016-08-21 7 views
0

1つの親に対して1つの子アクターを作成しています。私の子供の俳優は、いくつかのビジネスロジックを実行し、scala Futureに価値を返します。親にFutureメッセージを送信すると、私は将来のメッセージをキャッチできません。以下は、私のコードです:Akka-Java:パイプパターンを使用して親の将来のメッセージを受け取ることができません

子役

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を将来の代わりに親に送信しようとしていますが、まだメッセージを受け取っていません。 Follwoingが私の変化である:

parent.tell(23, ActorRef.noSender()); //replace pipe(future, ec).to(parent); 

expectd、親matchAny(msg -> {System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");LOG.info("unknown message: "+ msg);})場合は、このメッセージを処理します。しかし、何も起こりません。私の調査によると

アップデート2

、私はfuture.onFailure(futureFailureHandler(), ec);文をコメントアウトするとき、parent.tell(23, ActorRef.noSender());が正常に実行されます。それでもなぜこれが起こっているのか分からない。

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

答えて

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(); 
}} 

我々が直接import static akka.pattern.PatternsCS.pipe;パターンを使用してJavaアッカ8 CompletableFutureを使用することができます。以下のコードです。ハンドルJava 8 CompletableFutureのためのAkka PatternsCS

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

関連する問題