2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.pattern;
|
2012-12-08 11:27:07 +01:00
|
|
|
|
2012-12-08 15:13:58 +01:00
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ActorRefFactory;
|
|
|
|
|
import akka.actor.Props;
|
2017-02-04 11:51:30 +05:00
|
|
|
import akka.actor.AbstractActor;
|
2012-12-08 15:13:58 +01:00
|
|
|
import akka.util.Timeout;
|
2017-03-14 15:51:44 +05:00
|
|
|
import scala.concurrent.duration.FiniteDuration;
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.CompletionStage;
|
2012-12-08 11:27:07 +01:00
|
|
|
|
|
|
|
|
public class SupervisedAskSpec {
|
|
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
public Object execute(Class<? extends AbstractActor> someActor,
|
2012-12-08 15:13:58 +01:00
|
|
|
Object message, Timeout timeout, ActorRefFactory actorSystem)
|
|
|
|
|
throws Exception {
|
2012-12-08 11:27:07 +01:00
|
|
|
// example usage
|
|
|
|
|
try {
|
|
|
|
|
ActorRef supervisorCreator = SupervisedAsk
|
|
|
|
|
.createSupervisorCreator(actorSystem);
|
2017-03-14 15:51:44 +05:00
|
|
|
CompletionStage<Object> finished = SupervisedAsk.askOf(supervisorCreator,
|
2013-04-14 22:56:41 +02:00
|
|
|
Props.create(someActor), message, timeout);
|
2017-03-14 15:51:44 +05:00
|
|
|
FiniteDuration d = timeout.duration();
|
|
|
|
|
return finished.toCompletableFuture().get(d.length(), d.unit());
|
2012-12-08 11:27:07 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
// exception propagated by supervision
|
2012-12-08 15:13:58 +01:00
|
|
|
throw e;
|
2012-12-08 11:27:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|