public void testSchedule( final JavaTestKit probe, Props props, FiniteDuration startDuration, FiniteDuration afterRestartDuration) { Iterable<akka.testkit.EventFilter> filter = Arrays.asList( new akka.testkit.EventFilter[] { (akka.testkit.EventFilter) new ErrorFilter(ArithmeticException.class) }); try { system.eventStream().publish(new Mute(filter)); final ActorRef actor = system.actorOf(props); new Within(startDuration) { protected void run() { probe.expectMsgEquals("tick"); probe.expectMsgEquals("tick"); probe.expectMsgEquals("tick"); } }; actor.tell("restart", getRef()); new Within(afterRestartDuration) { protected void run() { probe.expectMsgEquals("tick"); probe.expectMsgEquals("tick"); } }; system.stop(actor); } finally { system.eventStream().publish(new UnMute(filter)); } }
@Override public void onReceive(Object message) throws Exception { if (message.equals("tick")) { // send another periodic tick after the specified delay getContext() .system() .scheduler() .scheduleOnce( Duration.create(1000, TimeUnit.MILLISECONDS), getSelf(), "tick", getContext().dispatcher()); // do something useful here // #schedule-receive target.tell(message, getSelf()); // #schedule-receive } // #schedule-receive else if (message.equals("restart")) { throw new ArithmeticException(); } // #schedule-receive else { unhandled(message); } }
@Override public void onReceive(Object message) throws Exception { if (message.equals("tick")) { // do something useful here // #schedule-constructor target.tell(message, getSelf()); // #schedule-constructor } // #schedule-constructor else if (message.equals("restart")) { throw new ArithmeticException(); } // #schedule-constructor else { unhandled(message); } }