2011-12-19 11:07:59 +01:00
|
|
|
/**
|
2015-03-07 22:58:48 -08:00
|
|
|
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
2011-12-19 11:07:59 +01:00
|
|
|
*/
|
2012-05-22 11:37:09 +02:00
|
|
|
package docs.dispatcher;
|
2011-12-13 16:18:51 +01:00
|
|
|
|
2014-03-11 17:03:05 +01:00
|
|
|
import akka.dispatch.ControlMessage;
|
2013-10-14 13:12:17 +02:00
|
|
|
import akka.dispatch.RequiresMessageQueue;
|
|
|
|
|
import akka.testkit.AkkaSpec;
|
|
|
|
|
import com.typesafe.config.ConfigFactory;
|
|
|
|
|
import docs.actor.MyBoundedUntypedActor;
|
|
|
|
|
import docs.actor.MyUntypedActor;
|
|
|
|
|
import org.junit.ClassRule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import scala.concurrent.ExecutionContext;
|
|
|
|
|
|
2011-12-13 16:18:51 +01:00
|
|
|
//#imports
|
2012-05-07 20:37:56 +02:00
|
|
|
import akka.actor.*;
|
2011-12-13 16:18:51 +01:00
|
|
|
//#imports
|
|
|
|
|
|
|
|
|
|
//#imports-prio
|
|
|
|
|
import akka.event.Logging;
|
|
|
|
|
import akka.event.LoggingAdapter;
|
|
|
|
|
|
|
|
|
|
//#imports-prio
|
|
|
|
|
|
2011-12-21 20:34:46 +01:00
|
|
|
//#imports-prio-mailbox
|
|
|
|
|
import akka.dispatch.PriorityGenerator;
|
2015-01-10 14:44:25 +00:00
|
|
|
import akka.dispatch.UnboundedStablePriorityMailbox;
|
2013-05-02 17:12:36 +02:00
|
|
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
2013-03-31 03:04:12 +02:00
|
|
|
import akka.testkit.JavaTestKit;
|
2011-12-21 20:34:46 +01:00
|
|
|
import com.typesafe.config.Config;
|
|
|
|
|
|
|
|
|
|
//#imports-prio-mailbox
|
|
|
|
|
|
2013-04-18 13:35:36 +02:00
|
|
|
//#imports-required-mailbox
|
|
|
|
|
|
|
|
|
|
//#imports-required-mailbox
|
|
|
|
|
|
2013-05-08 09:42:25 +02:00
|
|
|
public class DispatcherDocTest {
|
2011-12-13 16:18:51 +01:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
@ClassRule
|
|
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource =
|
|
|
|
|
new AkkaJUnitActorSystemResource("DispatcherDocTest", ConfigFactory.parseString(
|
|
|
|
|
DispatcherDocSpec.javaConfig()).withFallback(ConfigFactory.parseString(
|
|
|
|
|
DispatcherDocSpec.config())).withFallback(AkkaSpec.testConf()));
|
2011-12-13 16:18:51 +01:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
2011-12-13 16:18:51 +01:00
|
|
|
|
2013-04-14 22:56:41 +02:00
|
|
|
@SuppressWarnings("unused")
|
2011-12-13 16:18:51 +01:00
|
|
|
@Test
|
2013-04-05 16:12:45 +02:00
|
|
|
public void defineDispatcherInConfig() {
|
|
|
|
|
//#defining-dispatcher-in-config
|
|
|
|
|
ActorRef myActor =
|
2013-04-14 22:56:41 +02:00
|
|
|
system.actorOf(Props.create(MyUntypedActor.class),
|
2013-04-05 16:12:45 +02:00
|
|
|
"myactor");
|
|
|
|
|
//#defining-dispatcher-in-config
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-14 22:56:41 +02:00
|
|
|
@SuppressWarnings("unused")
|
2013-04-05 16:12:45 +02:00
|
|
|
@Test
|
|
|
|
|
public void defineDispatcherInCode() {
|
|
|
|
|
//#defining-dispatcher-in-code
|
2012-02-24 14:28:17 +01:00
|
|
|
ActorRef myActor =
|
2013-04-14 22:56:41 +02:00
|
|
|
system.actorOf(Props.create(MyUntypedActor.class).withDispatcher("my-dispatcher"),
|
2012-02-24 14:28:17 +01:00
|
|
|
"myactor3");
|
2013-04-05 16:12:45 +02:00
|
|
|
//#defining-dispatcher-in-code
|
2011-12-13 16:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
2015-12-17 09:40:03 +01:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
@Test
|
|
|
|
|
public void defineFixedPoolSizeDispatcher() {
|
|
|
|
|
//#defining-fixed-pool-size-dispatcher
|
|
|
|
|
ActorRef myActor = system.actorOf(Props.create(MyUntypedActor.class)
|
|
|
|
|
.withDispatcher("blocking-io-dispatcher"));
|
|
|
|
|
//#defining-fixed-pool-size-dispatcher
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-14 22:56:41 +02:00
|
|
|
@SuppressWarnings("unused")
|
2011-12-13 16:18:51 +01:00
|
|
|
@Test
|
|
|
|
|
public void definePinnedDispatcher() {
|
|
|
|
|
//#defining-pinned-dispatcher
|
2013-04-14 22:56:41 +02:00
|
|
|
ActorRef myActor = system.actorOf(Props.create(MyUntypedActor.class)
|
2012-02-24 14:28:17 +01:00
|
|
|
.withDispatcher("my-pinned-dispatcher"));
|
2011-12-13 16:18:51 +01:00
|
|
|
//#defining-pinned-dispatcher
|
|
|
|
|
}
|
2015-01-10 14:44:25 +00:00
|
|
|
|
2013-04-14 22:56:41 +02:00
|
|
|
@SuppressWarnings("unused")
|
2012-10-15 21:34:31 +02:00
|
|
|
public void compileLookup() {
|
|
|
|
|
//#lookup
|
|
|
|
|
// this is scala.concurrent.ExecutionContext
|
|
|
|
|
// for use with Futures, Scheduler, etc.
|
|
|
|
|
final ExecutionContext ex = system.dispatchers().lookup("my-dispatcher");
|
|
|
|
|
//#lookup
|
|
|
|
|
}
|
2011-12-13 16:18:51 +01:00
|
|
|
|
2013-04-18 13:35:36 +02:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
@Test
|
|
|
|
|
public void defineMailboxInConfig() {
|
|
|
|
|
//#defining-mailbox-in-config
|
|
|
|
|
ActorRef myActor =
|
|
|
|
|
system.actorOf(Props.create(MyUntypedActor.class),
|
|
|
|
|
"priomailboxactor");
|
|
|
|
|
//#defining-mailbox-in-config
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
@Test
|
|
|
|
|
public void defineMailboxInCode() {
|
|
|
|
|
//#defining-mailbox-in-code
|
|
|
|
|
ActorRef myActor =
|
|
|
|
|
system.actorOf(Props.create(MyUntypedActor.class)
|
|
|
|
|
.withMailbox("prio-mailbox"));
|
|
|
|
|
//#defining-mailbox-in-code
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
@Test
|
|
|
|
|
public void usingARequiredMailbox() {
|
|
|
|
|
ActorRef myActor =
|
|
|
|
|
system.actorOf(Props.create(MyBoundedUntypedActor.class));
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-13 16:18:51 +01:00
|
|
|
@Test
|
|
|
|
|
public void priorityDispatcher() throws Exception {
|
2013-03-31 03:04:12 +02:00
|
|
|
JavaTestKit probe = new JavaTestKit(system);
|
2011-12-13 16:18:51 +01:00
|
|
|
//#prio-dispatcher
|
|
|
|
|
|
2013-04-14 22:56:41 +02:00
|
|
|
class Demo extends UntypedActor {
|
|
|
|
|
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
|
|
|
|
|
{
|
|
|
|
|
for (Object msg : new Object[] { "lowpriority", "lowpriority",
|
|
|
|
|
"highpriority", "pigdog", "pigdog2", "pigdog3", "highpriority",
|
|
|
|
|
PoisonPill.getInstance() }) {
|
|
|
|
|
getSelf().tell(msg, getSelf());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onReceive(Object message) {
|
|
|
|
|
log.info(message.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
// We create a new Actor that just prints out what it processes
|
2013-04-14 22:56:41 +02:00
|
|
|
ActorRef myActor = system.actorOf(Props.create(Demo.class, this)
|
|
|
|
|
.withDispatcher("prio-dispatcher"));
|
2011-12-13 16:18:51 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Logs:
|
|
|
|
|
'highpriority
|
|
|
|
|
'highpriority
|
|
|
|
|
'pigdog
|
|
|
|
|
'pigdog2
|
|
|
|
|
'pigdog3
|
|
|
|
|
'lowpriority
|
|
|
|
|
'lowpriority
|
|
|
|
|
*/
|
|
|
|
|
//#prio-dispatcher
|
|
|
|
|
|
2013-03-31 03:04:12 +02:00
|
|
|
probe.watch(myActor);
|
|
|
|
|
probe.expectMsgClass(Terminated.class);
|
2011-12-13 16:18:51 +01:00
|
|
|
}
|
2011-12-21 20:34:46 +01:00
|
|
|
|
2014-03-11 17:03:05 +01:00
|
|
|
@Test
|
|
|
|
|
public void controlAwareDispatcher() throws Exception {
|
|
|
|
|
JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
//#control-aware-dispatcher
|
|
|
|
|
|
|
|
|
|
class Demo extends UntypedActor {
|
|
|
|
|
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
|
|
|
|
|
{
|
|
|
|
|
for (Object msg : new Object[] { "foo", "bar", new MyControlMessage(),
|
|
|
|
|
PoisonPill.getInstance() }) {
|
|
|
|
|
getSelf().tell(msg, getSelf());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onReceive(Object message) {
|
|
|
|
|
log.info(message.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We create a new Actor that just prints out what it processes
|
|
|
|
|
ActorRef myActor = system.actorOf(Props.create(Demo.class, this)
|
|
|
|
|
.withDispatcher("control-aware-dispatcher"));
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Logs:
|
|
|
|
|
'MyControlMessage
|
|
|
|
|
'foo
|
|
|
|
|
'bar
|
|
|
|
|
*/
|
|
|
|
|
//#control-aware-dispatcher
|
|
|
|
|
|
|
|
|
|
probe.watch(myActor);
|
|
|
|
|
probe.expectMsgClass(Terminated.class);
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
static
|
2011-12-21 20:34:46 +01:00
|
|
|
//#prio-mailbox
|
2015-01-10 14:44:25 +00:00
|
|
|
public class MyPrioMailbox extends UnboundedStablePriorityMailbox {
|
2012-09-26 10:56:25 +02:00
|
|
|
// needed for reflective instantiation
|
|
|
|
|
public MyPrioMailbox(ActorSystem.Settings settings, Config config) {
|
2012-02-24 14:28:17 +01:00
|
|
|
// Create a new PriorityGenerator, lower prio means more important
|
|
|
|
|
super(new PriorityGenerator() {
|
2012-02-21 18:24:38 +01:00
|
|
|
@Override
|
|
|
|
|
public int gen(Object message) {
|
|
|
|
|
if (message.equals("highpriority"))
|
|
|
|
|
return 0; // 'highpriority messages should be treated first if possible
|
|
|
|
|
else if (message.equals("lowpriority"))
|
2012-02-24 14:28:17 +01:00
|
|
|
return 2; // 'lowpriority messages should be treated last if possible
|
2012-05-07 20:37:56 +02:00
|
|
|
else if (message.equals(PoisonPill.getInstance()))
|
2012-02-24 14:28:17 +01:00
|
|
|
return 3; // PoisonPill when no other left
|
2012-02-21 18:24:38 +01:00
|
|
|
else
|
2012-02-24 14:28:17 +01:00
|
|
|
return 1; // By default they go between high and low prio
|
2012-02-21 18:24:38 +01:00
|
|
|
}
|
|
|
|
|
});
|
2011-12-21 20:34:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#prio-mailbox
|
2012-09-26 10:56:25 +02:00
|
|
|
|
2014-03-11 17:03:05 +01:00
|
|
|
static
|
|
|
|
|
//#control-aware-mailbox-messages
|
|
|
|
|
public class MyControlMessage implements ControlMessage {}
|
|
|
|
|
//#control-aware-mailbox-messages
|
|
|
|
|
|
2013-10-14 13:12:17 +02:00
|
|
|
@Test
|
|
|
|
|
public void requiredMailboxDispatcher() throws Exception {
|
|
|
|
|
ActorRef myActor = system.actorOf(Props.create(MyUntypedActor.class)
|
|
|
|
|
.withDispatcher("custom-dispatcher"));
|
|
|
|
|
}
|
2012-06-19 14:52:02 +02:00
|
|
|
|
2013-10-14 13:12:17 +02:00
|
|
|
static
|
|
|
|
|
//#require-mailbox-on-actor
|
|
|
|
|
public class MySpecialActor extends UntypedActor implements
|
|
|
|
|
RequiresMessageQueue<MyUnboundedJMessageQueueSemantics> {
|
|
|
|
|
//#require-mailbox-on-actor
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
|
|
|
|
unhandled(message);
|
2012-06-19 14:52:02 +02:00
|
|
|
}
|
2013-10-14 13:12:17 +02:00
|
|
|
//#require-mailbox-on-actor
|
|
|
|
|
// ...
|
|
|
|
|
}
|
|
|
|
|
//#require-mailbox-on-actor
|
2012-06-19 14:52:02 +02:00
|
|
|
|
2013-10-14 13:12:17 +02:00
|
|
|
@Test
|
|
|
|
|
public void requiredMailboxActor() throws Exception {
|
|
|
|
|
ActorRef myActor = system.actorOf(Props.create(MySpecialActor.class));
|
2012-06-19 14:52:02 +02:00
|
|
|
}
|
2013-10-14 13:12:17 +02:00
|
|
|
|
2011-12-13 16:18:51 +01:00
|
|
|
}
|