DOC: Better use of pipeTo in FaultHandlingDocSample
This commit is contained in:
parent
ebec797322
commit
c1178c9b1a
2 changed files with 38 additions and 17 deletions
|
|
@ -35,7 +35,7 @@ public class FaultHandlingDocSample {
|
||||||
/**
|
/**
|
||||||
* Runs the sample
|
* Runs the sample
|
||||||
*/
|
*/
|
||||||
public static void main(String... args) {
|
public static void main(String[] args) {
|
||||||
Config config = ConfigFactory.parseString("akka.loglevel = DEBUG \n" + "akka.actor.debug.lifecycle = on");
|
Config config = ConfigFactory.parseString("akka.loglevel = DEBUG \n" + "akka.actor.debug.lifecycle = on");
|
||||||
|
|
||||||
ActorSystem system = ActorSystem.create("FaultToleranceSample", config);
|
ActorSystem system = ActorSystem.create("FaultToleranceSample", config);
|
||||||
|
|
@ -62,11 +62,11 @@ public class FaultHandlingDocSample {
|
||||||
|
|
||||||
public void onReceive(Object msg) {
|
public void onReceive(Object msg) {
|
||||||
log.debug("received message {}", msg);
|
log.debug("received message {}", msg);
|
||||||
if (msg instanceof CurrentCount) {
|
if (msg instanceof Progress) {
|
||||||
CurrentCount current = (CurrentCount) msg;
|
Progress progress = (Progress) msg;
|
||||||
log.info("Current count for [{}] is [{}]", current.key, current.count);
|
log.info("Current progress: {} %", progress.percent);
|
||||||
if (current.count > 50) {
|
if (progress.percent >= 100.0) {
|
||||||
log.info("That's enough, shutting down");
|
log.info("That's all, shutting down");
|
||||||
getContext().system().shutdown();
|
getContext().system().shutdown();
|
||||||
}
|
}
|
||||||
} else if (msg == Actors.receiveTimeout()) {
|
} else if (msg == Actors.receiveTimeout()) {
|
||||||
|
|
@ -83,13 +83,25 @@ public class FaultHandlingDocSample {
|
||||||
public interface WorkerApi {
|
public interface WorkerApi {
|
||||||
public static final Object Start = "Start";
|
public static final Object Start = "Start";
|
||||||
public static final Object Do = "Do";
|
public static final Object Do = "Do";
|
||||||
|
|
||||||
|
public static class Progress {
|
||||||
|
public final double percent;
|
||||||
|
|
||||||
|
public Progress(double percent) {
|
||||||
|
this.percent = percent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return String.format("%s(%s)", getClass().getSimpleName(), percent);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//#messages
|
//#messages
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Worker performs some work when it receives the Start message. It will
|
* Worker performs some work when it receives the Start message. It will
|
||||||
* continuously notify the sender of the Start message of current progress.
|
* continuously notify the sender of the Start message of current Progress.
|
||||||
* The Worker supervise the CounterService.
|
* The Worker supervise the CounterService.
|
||||||
*/
|
*/
|
||||||
public static class Worker extends UntypedActor {
|
public static class Worker extends UntypedActor {
|
||||||
|
|
@ -99,6 +111,7 @@ public class FaultHandlingDocSample {
|
||||||
// The sender of the initial Start message will continuously be notified about progress
|
// The sender of the initial Start message will continuously be notified about progress
|
||||||
ActorRef progressListener;
|
ActorRef progressListener;
|
||||||
final ActorRef counterService = getContext().actorOf(new Props(CounterService.class), "counter");
|
final ActorRef counterService = getContext().actorOf(new Props(CounterService.class), "counter");
|
||||||
|
final int totalCount = 51;
|
||||||
|
|
||||||
// Stop the CounterService child if it throws ServiceUnavailable
|
// Stop the CounterService child if it throws ServiceUnavailable
|
||||||
private static SupervisorStrategy strategy = new OneForOneStrategy(-1, Duration.Inf(),
|
private static SupervisorStrategy strategy = new OneForOneStrategy(-1, Duration.Inf(),
|
||||||
|
|
@ -128,8 +141,12 @@ public class FaultHandlingDocSample {
|
||||||
counterService.tell(new Increment(1), getSelf());
|
counterService.tell(new Increment(1), getSelf());
|
||||||
counterService.tell(new Increment(1), getSelf());
|
counterService.tell(new Increment(1), getSelf());
|
||||||
|
|
||||||
// Send current count to the initial sender
|
// Send current progress to the initial sender
|
||||||
pipeTo(ask(counterService, GetCurrentCount, askTimeout), progressListener);
|
pipeTo(ask(counterService, GetCurrentCount, askTimeout).map(new Function<CurrentCount, Progress>() {
|
||||||
|
public Progress apply(CurrentCount c) {
|
||||||
|
return new Progress(100.0 * c.count / totalCount);
|
||||||
|
}
|
||||||
|
}), progressListener);
|
||||||
} else {
|
} else {
|
||||||
unhandled(msg);
|
unhandled(msg);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,15 +43,15 @@ object FaultHandlingDocSample extends App {
|
||||||
* work has been done.
|
* work has been done.
|
||||||
*/
|
*/
|
||||||
class Listener extends Actor with ActorLogging {
|
class Listener extends Actor with ActorLogging {
|
||||||
import CounterService._
|
import Worker._
|
||||||
// If we don't get any progress within 15 seconds then the service is unavailable
|
// If we don't get any progress within 15 seconds then the service is unavailable
|
||||||
context.setReceiveTimeout(15 seconds)
|
context.setReceiveTimeout(15 seconds)
|
||||||
|
|
||||||
def receive = {
|
def receive = {
|
||||||
case CurrentCount(key, count) ⇒
|
case Progress(percent) ⇒
|
||||||
log.info("Current count for [{}] is [{}]", key, count)
|
log.info("Current progress: {} %", percent)
|
||||||
if (count > 50) {
|
if (percent >= 100.0) {
|
||||||
log.info("That's enough, shutting down")
|
log.info("That's all, shutting down")
|
||||||
context.system.shutdown()
|
context.system.shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -66,13 +66,14 @@ class Listener extends Actor with ActorLogging {
|
||||||
object Worker {
|
object Worker {
|
||||||
case object Start
|
case object Start
|
||||||
case object Do
|
case object Do
|
||||||
|
case class Progress(percent: Double)
|
||||||
}
|
}
|
||||||
//#messages
|
//#messages
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Worker performs some work when it receives the `Start` message.
|
* Worker performs some work when it receives the `Start` message.
|
||||||
* It will continuously notify the sender of the `Start` message
|
* It will continuously notify the sender of the `Start` message
|
||||||
* of current progress. The `Worker` supervise the `CounterService`.
|
* of current ``Progress``. The `Worker` supervise the `CounterService`.
|
||||||
*/
|
*/
|
||||||
class Worker extends Actor with ActorLogging {
|
class Worker extends Actor with ActorLogging {
|
||||||
import Worker._
|
import Worker._
|
||||||
|
|
@ -87,6 +88,7 @@ class Worker extends Actor with ActorLogging {
|
||||||
// The sender of the initial Start message will continuously be notified about progress
|
// The sender of the initial Start message will continuously be notified about progress
|
||||||
var progressListener: Option[ActorRef] = None
|
var progressListener: Option[ActorRef] = None
|
||||||
val counterService = context.actorOf(Props[CounterService], name = "counter")
|
val counterService = context.actorOf(Props[CounterService], name = "counter")
|
||||||
|
val totalCount = 51
|
||||||
|
|
||||||
def receive = LoggingReceive {
|
def receive = LoggingReceive {
|
||||||
case Start if progressListener.isEmpty ⇒
|
case Start if progressListener.isEmpty ⇒
|
||||||
|
|
@ -98,8 +100,10 @@ class Worker extends Actor with ActorLogging {
|
||||||
counterService ! Increment(1)
|
counterService ! Increment(1)
|
||||||
counterService ! Increment(1)
|
counterService ! Increment(1)
|
||||||
|
|
||||||
// Send current count to the initial sender
|
// Send current progress to the initial sender
|
||||||
counterService ? GetCurrentCount pipeTo progressListener.get
|
counterService ? GetCurrentCount map {
|
||||||
|
case CurrentCount(_, count) ⇒ Progress(100.0 * count / totalCount)
|
||||||
|
} pipeTo progressListener.get
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue