2018-03-13 23:45:55 +09:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2018-2019 Lightbend Inc. <https://www.lightbend.com>
|
2018-03-13 23:45:55 +09:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package jdocs.pattern;
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ActorRefFactory;
|
|
|
|
|
import akka.actor.Props;
|
|
|
|
|
import akka.actor.AbstractActor;
|
|
|
|
|
import akka.util.Timeout;
|
|
|
|
|
import scala.concurrent.duration.FiniteDuration;
|
|
|
|
|
|
2018-12-06 22:40:43 +08:00
|
|
|
import java.time.Duration;
|
2018-03-13 23:45:55 +09:00
|
|
|
import java.util.concurrent.CompletionStage;
|
2018-12-06 22:40:43 +08:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2018-03-13 23:45:55 +09:00
|
|
|
|
|
|
|
|
public class SupervisedAskSpec {
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
public Object execute(
|
|
|
|
|
Class<? extends AbstractActor> someActor,
|
|
|
|
|
Object message,
|
|
|
|
|
Duration timeout,
|
|
|
|
|
ActorRefFactory actorSystem)
|
2018-03-13 23:45:55 +09:00
|
|
|
throws Exception {
|
|
|
|
|
// example usage
|
|
|
|
|
try {
|
2019-01-12 04:00:53 +08:00
|
|
|
ActorRef supervisorCreator = SupervisedAsk.createSupervisorCreator(actorSystem);
|
|
|
|
|
CompletionStage<Object> finished =
|
|
|
|
|
SupervisedAsk.askOf(supervisorCreator, Props.create(someActor), message, timeout);
|
2018-12-06 22:40:43 +08:00
|
|
|
return finished.toCompletableFuture().get(timeout.toMillis(), TimeUnit.MILLISECONDS);
|
2018-03-13 23:45:55 +09:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
// exception propagated by supervision
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|