0

I am creating one child actor for one parent. My child actor perform some business logic and return value to scala Future. When i send Future message to my parent, i am unable to catch my future message. Following is my code:

Child Actor

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

Parent Actor

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

Logs

[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'

Update

I am trying to send tell to parent instead of future, but still parent not getting the message. Follwoing is my changes :

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

expectd, parent matchAny(msg -> {System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");LOG.info("unknown message: "+ msg);}) case handle this message. But nothing happens.

Update 2

According to my investigation, when i comment out future.onFailure(futureFailureHandler(), ec); statement, the parent.tell(23, ActorRef.noSender()); execute successfully. Still not getting why this happens.

My requirements are, send future message to parent actor and handle future failure for fault-tolerance in akka actor system.

Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126

1 Answers1

0

I am still not getting, what exact problem with above code and how i can resolve this problem. But Found another alternative way for perform akka pipe pattern with java. Following is the code:

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

We can directly use Java 8 CompletableFuture with akka using import static akka.pattern.PatternsCS.pipe; pattern. Akka PatternsCS for handle Java 8 CompletableFuture.

Note: use context().parent() in message handler instead for creating a parent instance in actor like private final ActorRef parent = context().parent();. I am still not getting why this happens, But some time with parent instance variable, the pipe pattern doesn't work.

Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126