@Test public void shouldRestartWaiterAndResendPrepareCoffeeToBaristaOnFailure() { new JavaTestKit(system) {{ createActor(CoffeeHouse.class, "resend-prepare-coffee", () -> new CoffeeHouse(Integer.MAX_VALUE) { @Override protected ActorRef createBarista() { return getRef(); } @Override protected ActorRef createWaiter() { //stubbing out the waiter actor to always throw exception return context().actorOf(Props.create(AbstractActor.class, () -> new AbstractActor() { @Override public Receive createReceive() { return receiveBuilder().matchAny(o -> { throw new Waiter.FrustratedException(new Coffee.Akkaccino(), system.deadLetters()); }).build(); } }), "waiter"); } }); ActorRef waiter = expectActor(this, "/user/resend-prepare-coffee/waiter"); waiter.tell("Blow up", ActorRef.noSender()); expectMsgEquals(new Barista.PrepareCoffee(new Coffee.Akkaccino(), system.deadLetters())); }}; }
@Test public void shouldRestartWaiterAndResendPrepareCoffeeToBaristaOnFailure() { new JavaTestKit(system) {{ createActor(CoffeeHouse.class, "resend-prepare-coffee", () -> new CoffeeHouse(Integer.MAX_VALUE) { @Override protected ActorRef createBarista() { return getRef(); } @Override protected ActorRef createWaiter() { //stubbing out the waiter actor to always throw exception return context().actorOf(Props.create(AbstractActor.class, () -> new AbstractActor() { @Override public Receive createReceive() { return receiveBuilder().matchAny(o -> { //throw new Waiter.FrustratedException(new Coffee.Akkaccino(), system.deadLetters()); }).build(); } }), "waiter"); } }); ActorRef waiter = expectActor(this, "/user/resend-prepare-coffee/waiter"); waiter.tell("Blow up", ActorRef.noSender()); expectMsgEquals(new Barista.PrepareCoffee(new Coffee.Akkaccino(), system.deadLetters())); }}; }
@Test public void shouldRestartWaiterAndResendPrepareCoffeeToBaristaOnFailure() { new JavaTestKit(system) {{ createActor(CoffeeHouse.class, "resend-prepare-coffee", () -> new CoffeeHouse(Integer.MAX_VALUE) { @Override protected ActorRef createBarista() { return getRef(); } @Override protected ActorRef createWaiter() { //stubbing out the waiter actor to always throw exception return context().actorOf(Props.create(AbstractActor.class, () -> new AbstractActor() {{ receive( ReceiveBuilder.matchAny(o -> { throw new Waiter.FrustratedException(new Coffee.Akkaccino(), system.deadLetters()); }).build()); }}), "waiter"); } }); ActorRef waiter = expectActor(this, "/user/resend-prepare-coffee/waiter"); waiter.tell("Blow up", ActorRef.noSender()); expectMsgEquals(new Barista.PrepareCoffee(new Coffee.Akkaccino(), system.deadLetters())); }}; }
private static void RegisterActors(Binder binder) { Logger.debug("Actor Scanner Started..."); final Map<String, ActorHolder> map = new HashMap<>(); final ConfigurationBuilder configBuilder = build(); final Reflections reflections = new Reflections(configBuilder.setScanners(new SubTypesScanner())); final Set<Class<? extends UntypedActor>> actors = reflections.getSubTypesOf(UntypedActor.class); final Set<Class<? extends AbstractActor>> abstractActors = reflections.getSubTypesOf(AbstractActor.class); loopOnActors(map, actors); loopOnAbstractActors(map, abstractActors); if(!map.isEmpty()) Logger.debug("Registering actors: "); for(final String key : map.keySet()) { final ActorHolder actorHolder = map.get(key); final Class<? extends Actor> actor = actorHolder.getActor(); if(actorHolder.isSingleton()) { Logger.debug("Binding class " + actor.getSimpleName() + " to name: " + key + " Singleton Scoped."); binder.bind(ActorRef.class).annotatedWith(Names.named(key)).toProvider(new ActorRefProvider(actor, key, true)).in(Singleton.class); } else { Logger.debug("Binding class " + actor.getSimpleName() + " to name: " + key + " Request Scoped."); binder.bind(ActorRef.class).annotatedWith(Names.named(key)).toProvider(new ActorRefProvider(actor, key, false)); PropsContext.put(key, actorHolder); } } }
private static void loopOnAbstractActors(Map<String, ActorHolder> map, Set<Class<? extends AbstractActor>> actors) { for(final Class<? extends Actor> actor : actors) { if(ignore.contains(actor.getSimpleName())) continue; final String named = getNamed(actor); final boolean isSingleton = isSingleton(actor); final ActorHolder actorHolder = new ActorHolder(actor, isSingleton); if(named != null) { map.put(named, actorHolder); } else { if(map.containsKey(actor.getSimpleName())){ map.put(actor.getName(), actorHolder); final ActorHolder tempHolder = map.remove(actor.getSimpleName()); map.put(tempHolder.getActor().getName(), tempHolder); } else map.put(actor.getSimpleName(), actorHolder); } } }
public AbstractActor.Receive createReceive() { return receiveBuilder() .match(BackendActor.Identifiers.class, identifiers -> { long totalTime = 0l; for (long id : identifiers.ids) { long simulatedDatabaseCallTime = id; Thread.sleep(simulatedDatabaseCallTime); totalTime += simulatedDatabaseCallTime; } getSender().tell("DB CALL TIME = " + totalTime + "ms\n", getSender()); }).build(); }
static <T> ActorRef spawn(String actorName, ActorSystem system, Class<? extends AbstractActor> actorClass, Object... constructorParams) { Props props = Props.create(actorClass, constructorParams); return system.actorOf(props, actorName); }