Changes according to review. #2513
This commit is contained in:
parent
99ad1e0eeb
commit
c63234ca4c
5 changed files with 50 additions and 50 deletions
|
|
@ -8,26 +8,23 @@ import akka.actor.*;
|
||||||
import akka.testkit.*;
|
import akka.testkit.*;
|
||||||
import akka.testkit.TestEvent.Mute;
|
import akka.testkit.TestEvent.Mute;
|
||||||
import akka.testkit.TestEvent.UnMute;
|
import akka.testkit.TestEvent.UnMute;
|
||||||
import org.junit.After;
|
import org.junit.*;
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
|
||||||
import scala.concurrent.util.Duration;
|
import scala.concurrent.util.Duration;
|
||||||
import scala.concurrent.util.FiniteDuration;
|
import scala.concurrent.util.FiniteDuration;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class SchedulerPatternTestBase {
|
public class SchedulerPatternTest {
|
||||||
|
|
||||||
ActorSystem system;
|
static ActorSystem system;
|
||||||
|
|
||||||
@Before
|
@BeforeClass
|
||||||
public void setUp() {
|
public static void setUp() {
|
||||||
system = ActorSystem.create("SchedulerPatternTest", AkkaSpec.testConf());
|
system = ActorSystem.create("SchedulerPatternTest", AkkaSpec.testConf());
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@AfterClass
|
||||||
public void tearDown() {
|
public static void tearDown() {
|
||||||
system.shutdown();
|
system.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -38,9 +35,10 @@ public class SchedulerPatternTestBase {
|
||||||
private final Cancellable tick = getContext().system().scheduler().schedule(
|
private final Cancellable tick = getContext().system().scheduler().schedule(
|
||||||
Duration.create(500, TimeUnit.MILLISECONDS),
|
Duration.create(500, TimeUnit.MILLISECONDS),
|
||||||
Duration.create(1000, TimeUnit.MILLISECONDS),
|
Duration.create(1000, TimeUnit.MILLISECONDS),
|
||||||
getSelf(), "tick", getContext().system().dispatcher());
|
getSelf(), "tick", getContext().dispatcher());
|
||||||
//#schedule-constructor
|
//#schedule-constructor
|
||||||
ActorRef target;
|
// this variable and constructor is declared here to not show up in the docs
|
||||||
|
final ActorRef target;
|
||||||
public ScheduleInConstructor(ActorRef target) {
|
public ScheduleInConstructor(ActorRef target) {
|
||||||
this.target = target;
|
this.target = target;
|
||||||
}
|
}
|
||||||
|
|
@ -75,7 +73,8 @@ public class SchedulerPatternTestBase {
|
||||||
//#schedule-receive
|
//#schedule-receive
|
||||||
public class ScheduleInReceive extends UntypedActor {
|
public class ScheduleInReceive extends UntypedActor {
|
||||||
//#schedule-receive
|
//#schedule-receive
|
||||||
ActorRef target;
|
// this variable and constructor is declared here to not show up in the docs
|
||||||
|
final ActorRef target;
|
||||||
public ScheduleInReceive(ActorRef target) {
|
public ScheduleInReceive(ActorRef target) {
|
||||||
this.target = target;
|
this.target = target;
|
||||||
}
|
}
|
||||||
|
|
@ -85,7 +84,7 @@ public class SchedulerPatternTestBase {
|
||||||
public void preStart() {
|
public void preStart() {
|
||||||
getContext().system().scheduler().scheduleOnce(
|
getContext().system().scheduler().scheduleOnce(
|
||||||
Duration.create(500, TimeUnit.MILLISECONDS),
|
Duration.create(500, TimeUnit.MILLISECONDS),
|
||||||
getSelf(), "tick", getContext().system().dispatcher());
|
getSelf(), "tick", getContext().dispatcher());
|
||||||
}
|
}
|
||||||
|
|
||||||
// override postRestart so we don't call preStart and schedule a new message
|
// override postRestart so we don't call preStart and schedule a new message
|
||||||
|
|
@ -99,7 +98,7 @@ public class SchedulerPatternTestBase {
|
||||||
// send another periodic tick after the specified delay
|
// send another periodic tick after the specified delay
|
||||||
getContext().system().scheduler().scheduleOnce(
|
getContext().system().scheduler().scheduleOnce(
|
||||||
Duration.create(1000, TimeUnit.MILLISECONDS),
|
Duration.create(1000, TimeUnit.MILLISECONDS),
|
||||||
getSelf(), "tick", getContext().system().dispatcher());
|
getSelf(), "tick", getContext().dispatcher());
|
||||||
// do something useful here
|
// do something useful here
|
||||||
//#schedule-receive
|
//#schedule-receive
|
||||||
target.tell(message, getSelf());
|
target.tell(message, getSelf());
|
||||||
|
|
@ -164,6 +163,7 @@ public class SchedulerPatternTestBase {
|
||||||
Iterable<akka.testkit.EventFilter> filter =
|
Iterable<akka.testkit.EventFilter> filter =
|
||||||
Arrays.asList(new akka.testkit.EventFilter[]{
|
Arrays.asList(new akka.testkit.EventFilter[]{
|
||||||
(akka.testkit.EventFilter) new ErrorFilter(ArithmeticException.class)});
|
(akka.testkit.EventFilter) new ErrorFilter(ArithmeticException.class)});
|
||||||
|
try {
|
||||||
system.eventStream().publish(new Mute(filter));
|
system.eventStream().publish(new Mute(filter));
|
||||||
|
|
||||||
final ActorRef actor = system.actorOf(props);
|
final ActorRef actor = system.actorOf(props);
|
||||||
|
|
@ -182,8 +182,10 @@ public class SchedulerPatternTestBase {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
system.stop(actor);
|
system.stop(actor);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
system.eventStream().publish(new UnMute(filter));
|
system.eventStream().publish(new UnMute(filter));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
|
||||||
*/
|
|
||||||
|
|
||||||
package docs.pattern
|
|
||||||
|
|
||||||
import org.scalatest.junit.JUnitSuite
|
|
||||||
|
|
||||||
class SchedulerPatternTest extends SchedulerPatternTestBase with JUnitSuite
|
|
||||||
|
|
@ -29,9 +29,12 @@ message sends to the same actor.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
With this approach the scheduler will be restarted with the actor on restarts.
|
With this approach the scheduled periodic message send will be restarted with the actor on restarts.
|
||||||
|
This also means that the time period that elapses between two tick messages during a restart may drift
|
||||||
|
off based on when you restart the scheduled message sends relative to the time that the last message was
|
||||||
|
sent, and how long the initial delay is. Worst case scenario is ``interval`` plus ``initialDelay``.
|
||||||
|
|
||||||
.. includecode:: code/docs/pattern/SchedulerPatternTestBase.java#schedule-constructor
|
.. includecode:: code/docs/pattern/SchedulerPatternTest.java#schedule-constructor
|
||||||
|
|
||||||
The second variant sets up an initial one shot message send in the ``preStart`` method
|
The second variant sets up an initial one shot message send in the ``preStart`` method
|
||||||
of the actor, and the then the actor when it receives this message sets up a new one shot
|
of the actor, and the then the actor when it receives this message sets up a new one shot
|
||||||
|
|
@ -43,7 +46,7 @@ and schedule the initial message send again.
|
||||||
With this approach we won't fill up the mailbox with tick messages if the actor is
|
With this approach we won't fill up the mailbox with tick messages if the actor is
|
||||||
under pressure, but only schedule a new tick message when we have seen the previous one.
|
under pressure, but only schedule a new tick message when we have seen the previous one.
|
||||||
|
|
||||||
.. includecode:: code/docs/pattern/SchedulerPatternTestBase.java#schedule-receive
|
.. includecode:: code/docs/pattern/SchedulerPatternTest.java#schedule-receive
|
||||||
|
|
||||||
Template Pattern
|
Template Pattern
|
||||||
================
|
================
|
||||||
|
|
|
||||||
|
|
@ -15,10 +15,10 @@ import docs.pattern.SchedulerPatternSpec.ScheduleInConstructor
|
||||||
object SchedulerPatternSpec {
|
object SchedulerPatternSpec {
|
||||||
//#schedule-constructor
|
//#schedule-constructor
|
||||||
class ScheduleInConstructor extends Actor {
|
class ScheduleInConstructor extends Actor {
|
||||||
import context._
|
val tick =
|
||||||
|
context.system.scheduler.schedule(500 millis, 1000 millis, self, "tick")
|
||||||
val tick = system.scheduler.schedule(500 millis, 1000 millis, self, "tick")
|
|
||||||
//#schedule-constructor
|
//#schedule-constructor
|
||||||
|
// this var and constructor is declared here to not show up in the docs
|
||||||
var target: ActorRef = null
|
var target: ActorRef = null
|
||||||
def this(target: ActorRef) = { this(); this.target = target }
|
def this(target: ActorRef) = { this(); this.target = target }
|
||||||
//#schedule-constructor
|
//#schedule-constructor
|
||||||
|
|
@ -41,6 +41,7 @@ object SchedulerPatternSpec {
|
||||||
class ScheduleInReceive extends Actor {
|
class ScheduleInReceive extends Actor {
|
||||||
import context._
|
import context._
|
||||||
//#schedule-receive
|
//#schedule-receive
|
||||||
|
// this var and constructor is declared here to not show up in the docs
|
||||||
var target: ActorRef = null
|
var target: ActorRef = null
|
||||||
def this(target: ActorRef) = { this(); this.target = target }
|
def this(target: ActorRef) = { this(); this.target = target }
|
||||||
//#schedule-receive
|
//#schedule-receive
|
||||||
|
|
|
||||||
|
|
@ -123,7 +123,10 @@ message sends to the same actor.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
With this approach the scheduler will be restarted with the actor on restarts.
|
With this approach the scheduled periodic message send will be restarted with the actor on restarts.
|
||||||
|
This also means that the time period that elapses between two tick messages during a restart may drift
|
||||||
|
off based on when you restart the scheduled message sends relative to the time that the last message was
|
||||||
|
sent, and how long the initial delay is. Worst case scenario is ``interval`` plus ``initialDelay``.
|
||||||
|
|
||||||
.. includecode:: code/docs/pattern/SchedulerPatternSpec.scala#schedule-constructor
|
.. includecode:: code/docs/pattern/SchedulerPatternSpec.scala#schedule-constructor
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue