2011-12-19 11:07:59 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
2011-12-13 16:18:51 +01:00
|
|
|
package akka.docs.dispatcher;
|
|
|
|
|
|
|
|
|
|
//#imports
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.Props;
|
|
|
|
|
import akka.dispatch.MessageDispatcher;
|
|
|
|
|
|
|
|
|
|
//#imports
|
|
|
|
|
|
|
|
|
|
//#imports-prio
|
|
|
|
|
import akka.actor.UntypedActor;
|
|
|
|
|
import akka.actor.UntypedActorFactory;
|
|
|
|
|
import akka.actor.Actors;
|
|
|
|
|
import akka.dispatch.PriorityGenerator;
|
|
|
|
|
import akka.dispatch.UnboundedPriorityMailbox;
|
2011-12-20 21:08:27 +01:00
|
|
|
import akka.dispatch.MessageDispatcherConfigurator;
|
|
|
|
|
import akka.dispatch.DispatcherPrerequisites;
|
2011-12-13 16:18:51 +01:00
|
|
|
import akka.event.Logging;
|
|
|
|
|
import akka.event.LoggingAdapter;
|
|
|
|
|
|
|
|
|
|
//#imports-prio
|
|
|
|
|
|
|
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import scala.Option;
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
|
|
import com.typesafe.config.ConfigFactory;
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.docs.actor.MyUntypedActor;
|
2011-12-14 21:52:39 +01:00
|
|
|
import akka.docs.actor.UntypedActorDocTestBase.MyActor;
|
2011-12-13 16:18:51 +01:00
|
|
|
import akka.testkit.AkkaSpec;
|
|
|
|
|
|
|
|
|
|
public class DispatcherDocTestBase {
|
|
|
|
|
|
|
|
|
|
ActorSystem system;
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setUp() {
|
|
|
|
|
system = ActorSystem.create("MySystem",
|
|
|
|
|
ConfigFactory.parseString(DispatcherDocSpec.config()).withFallback(AkkaSpec.testConf()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void tearDown() {
|
2011-12-14 16:32:25 +01:00
|
|
|
system.shutdown();
|
2011-12-13 16:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void defineDispatcher() {
|
|
|
|
|
//#defining-dispatcher
|
2011-12-20 21:08:27 +01:00
|
|
|
ActorRef myActor1 = system.actorOf(new Props().withCreator(MyUntypedActor.class).withDispatcher("my-dispatcher"),
|
2011-12-13 16:18:51 +01:00
|
|
|
"myactor1");
|
2011-12-20 21:08:27 +01:00
|
|
|
ActorRef myActor2 = system.actorOf(new Props().withCreator(MyUntypedActor.class).withDispatcher("my-dispatcher"),
|
2011-12-13 16:18:51 +01:00
|
|
|
"myactor2");
|
|
|
|
|
//#defining-dispatcher
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void definePinnedDispatcher() {
|
|
|
|
|
//#defining-pinned-dispatcher
|
|
|
|
|
String name = "myactor";
|
2011-12-20 21:08:27 +01:00
|
|
|
ActorRef myActor = system.actorOf(new Props().withCreator(MyUntypedActor.class)
|
|
|
|
|
.withDispatcher("myactor-dispatcher"), name);
|
2011-12-13 16:18:51 +01:00
|
|
|
//#defining-pinned-dispatcher
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void priorityDispatcher() throws Exception {
|
|
|
|
|
//#prio-dispatcher
|
2011-12-20 21:08:27 +01:00
|
|
|
final PriorityGenerator generator = new PriorityGenerator() { // Create a new PriorityGenerator, lower prio means more important
|
2011-12-13 16:18:51 +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"))
|
|
|
|
|
return 100; // 'lowpriority messages should be treated last if possible
|
|
|
|
|
else if (message.equals(Actors.poisonPill()))
|
|
|
|
|
return 1000; // PoisonPill when no other left
|
|
|
|
|
else
|
|
|
|
|
return 50; // We default to 50
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2011-12-20 21:08:27 +01:00
|
|
|
// FIXME #1458: how should we make it easy to configure prio mailbox?
|
2011-12-13 16:18:51 +01:00
|
|
|
// We create a new Priority dispatcher and seed it with the priority generator
|
2011-12-20 21:08:27 +01:00
|
|
|
final String dispatcherKey = "prio-dispatcher";
|
|
|
|
|
MessageDispatcherConfigurator dispatcherConfigurator = new MessageDispatcherConfigurator(system.dispatcherFactory()
|
|
|
|
|
.defaultDispatcherConfig(), system.dispatcherFactory().prerequisites()) {
|
|
|
|
|
private final MessageDispatcher instance = system.dispatcherFactory()
|
|
|
|
|
.newDispatcher(dispatcherKey, 5, new UnboundedPriorityMailbox(generator)).build();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public MessageDispatcher dispatcher() {
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
system.dispatcherFactory().register(dispatcherKey, dispatcherConfigurator);
|
2011-12-13 16:18:51 +01:00
|
|
|
|
|
|
|
|
ActorRef myActor = system.actorOf( // We create a new Actor that just prints out what it processes
|
|
|
|
|
new Props().withCreator(new UntypedActorFactory() {
|
|
|
|
|
public UntypedActor create() {
|
|
|
|
|
return new UntypedActor() {
|
|
|
|
|
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
|
|
|
|
|
{
|
|
|
|
|
getSelf().tell("lowpriority");
|
|
|
|
|
getSelf().tell("lowpriority");
|
|
|
|
|
getSelf().tell("highpriority");
|
|
|
|
|
getSelf().tell("pigdog");
|
|
|
|
|
getSelf().tell("pigdog2");
|
|
|
|
|
getSelf().tell("pigdog3");
|
|
|
|
|
getSelf().tell("highpriority");
|
|
|
|
|
getSelf().tell(Actors.poisonPill());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onReceive(Object message) {
|
|
|
|
|
log.info(message.toString());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2011-12-20 21:08:27 +01:00
|
|
|
}).withDispatcher(dispatcherKey));
|
2011-12-13 16:18:51 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Logs:
|
|
|
|
|
'highpriority
|
|
|
|
|
'highpriority
|
|
|
|
|
'pigdog
|
|
|
|
|
'pigdog2
|
|
|
|
|
'pigdog3
|
|
|
|
|
'lowpriority
|
|
|
|
|
'lowpriority
|
|
|
|
|
*/
|
|
|
|
|
//#prio-dispatcher
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
|
if (myActor.isTerminated())
|
|
|
|
|
break;
|
|
|
|
|
Thread.sleep(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|