Guest User

Untitled

a guest
Apr 4th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. package com.tesco.payments.async;
  2.  
  3. import akka.actor.*;
  4. import akka.japi.pf.DeciderBuilder;
  5. import akka.pattern.Backoff;
  6. import akka.pattern.BackoffSupervisor;
  7. import akka.testkit.javadsl.TestKit;
  8. import org.junit.jupiter.api.AfterAll;
  9. import org.junit.jupiter.api.BeforeAll;
  10. import org.junit.jupiter.api.Test;
  11. import scala.Option;
  12. import scala.concurrent.duration.Duration;
  13.  
  14. import java.util.concurrent.TimeUnit;
  15.  
  16. import static akka.actor.SupervisorStrategy.escalate;
  17. import static akka.actor.SupervisorStrategy.restart;
  18. import static java.util.concurrent.TimeUnit.MILLISECONDS;
  19. import static java.util.concurrent.TimeUnit.SECONDS;
  20. import static org.awaitility.Awaitility.await;
  21.  
  22. public class TestSupervisionDocs {
  23.     static int counter = 0;
  24.  
  25.     private static ActorSystem system;
  26.  
  27.     @Test public void supervisedActorIsRestarted() {
  28.         supervised().tell("foo", ActorRef.noSender());
  29.  
  30.         await().until(() -> counter == 2);
  31.     }
  32.  
  33.     private ActorRef supervised() {
  34.         return system.actorOf(supervised(SupervisedActor.props()), "child-supervisor");
  35.     }
  36.  
  37.     private Props supervised(final Props supervised) {
  38.         return BackoffSupervisor.props(Backoff.onStop(
  39.                 supervised, "child-actor",
  40.                 Duration.create(100, MILLISECONDS),
  41.                 Duration.create(1, SECONDS),
  42.                 0.2).withSupervisorStrategy(
  43.                 new OneForOneStrategy(10, Duration.create(1, TimeUnit.MINUTES),
  44.                         DeciderBuilder
  45.                                 .match(RestartMe.class, e -> {
  46.                                     System.out.println("RETRY caught------------------");
  47.                                     return restart();
  48.                                 })
  49.                                 .matchAny(e -> escalate()).build())));
  50.     }
  51.  
  52.     @BeforeAll
  53.     static void beforeClass() {
  54.         system = ActorSystem.apply("TestActorSystem");
  55.     }
  56.  
  57.     @AfterAll
  58.     static void afterAll() {
  59.         TestKit.shutdownActorSystem(system);
  60.     }
  61.  
  62.     static class SupervisedActor extends AbstractActor {
  63.  
  64.         private ActorRef originator;
  65.  
  66.         @Override public Receive createReceive() {
  67.             return receiveBuilder()
  68.                     .match(String.class, m -> {
  69.                         originator = getSender();
  70.                         System.out.println("received. Count=" + counter);
  71.                         if (counter++ < 3) {
  72.                             throw new RestartMe() ;
  73.                         }
  74.                     })
  75.                     .matchAny(m -> System.out.println("matched any: " + m))
  76.                     .build();
  77.         }
  78.  
  79.         @Override public void preRestart(Throwable reason, Option<Object> message) {
  80.             System.out.println("SupervisedActor restarting with message " + message.get() + " of type " + message.get().getClass().getSimpleName());
  81.             getContext().getParent().tell(message.get(), originator);
  82.         }
  83.  
  84.         @Override public void postRestart(Throwable reason) {
  85.             System.out.println("SupervisedActor restarted! " + getSelf().path());
  86.         }
  87.  
  88.         public static Props props() {
  89.             return Props.create(SupervisedActor.class);
  90.         }
  91.     }
  92.  
  93.     private static class RestartMe extends RuntimeException {
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment