2011-12-19 11:07:59 +01:00
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
2011-12-19 11:07:59 +01:00
|
|
|
*/
|
2011-12-12 21:20:32 +01:00
|
|
|
package akka.docs.dispatcher
|
|
|
|
|
|
|
|
|
|
import org.scalatest.{ BeforeAndAfterAll, WordSpec }
|
|
|
|
|
import org.scalatest.matchers.MustMatchers
|
|
|
|
|
import akka.testkit.AkkaSpec
|
|
|
|
|
import akka.actor.Props
|
|
|
|
|
import akka.actor.Actor
|
|
|
|
|
import akka.event.Logging
|
|
|
|
|
import akka.event.LoggingAdapter
|
|
|
|
|
import akka.util.duration._
|
|
|
|
|
import akka.actor.PoisonPill
|
2011-12-20 21:08:27 +01:00
|
|
|
import akka.dispatch.MessageDispatcherConfigurator
|
|
|
|
|
import akka.dispatch.MessageDispatcher
|
|
|
|
|
import akka.dispatch.DispatcherPrerequisites
|
2011-12-12 21:20:32 +01:00
|
|
|
|
|
|
|
|
object DispatcherDocSpec {
|
|
|
|
|
val config = """
|
|
|
|
|
//#my-dispatcher-config
|
|
|
|
|
my-dispatcher {
|
2012-02-07 09:50:03 +01:00
|
|
|
# Dispatcher is the name of the event-based dispatcher
|
|
|
|
|
type = Dispatcher
|
|
|
|
|
# What kind of ExecutionService to use
|
|
|
|
|
executor = "fork-join-executor"
|
|
|
|
|
# Configuration for the fork join pool
|
|
|
|
|
fork-join-executor {
|
|
|
|
|
# Min number of threads to cap factor-based parallelism number to
|
|
|
|
|
parallelism-min = 2
|
|
|
|
|
# Parallelism (threads) ... ceil(available processors * factor)
|
|
|
|
|
parallelism-factor = 2.0
|
|
|
|
|
# Max number of threads to cap factor-based parallelism number to
|
|
|
|
|
parallelism-max = 10
|
|
|
|
|
}
|
|
|
|
|
# Throughput defines the number of messages that are processed in a batch before the
|
|
|
|
|
# thread is returned to the pool. Set to 1 for as fair as possible.
|
|
|
|
|
throughput = 100
|
|
|
|
|
}
|
|
|
|
|
//#my-dispatcher-config
|
|
|
|
|
|
|
|
|
|
//#my-thread-pool-dispatcher-config
|
|
|
|
|
my-thread-pool-dispatcher {
|
2011-12-14 15:12:40 +01:00
|
|
|
# Dispatcher is the name of the event-based dispatcher
|
|
|
|
|
type = Dispatcher
|
2012-01-30 16:34:25 +01:00
|
|
|
# What kind of ExecutionService to use
|
|
|
|
|
executor = "thread-pool-executor"
|
|
|
|
|
# Configuration for the thread pool
|
|
|
|
|
thread-pool-executor {
|
|
|
|
|
# minimum number of threads to cap factor-based core number to
|
|
|
|
|
core-pool-size-min = 2
|
|
|
|
|
# No of core threads ... ceil(available processors * factor)
|
|
|
|
|
core-pool-size-factor = 2.0
|
|
|
|
|
# maximum number of threads to cap factor-based number to
|
|
|
|
|
core-pool-size-max = 10
|
|
|
|
|
}
|
2011-12-14 15:12:40 +01:00
|
|
|
# Throughput defines the number of messages that are processed in a batch before the
|
|
|
|
|
# thread is returned to the pool. Set to 1 for as fair as possible.
|
|
|
|
|
throughput = 100
|
2011-12-12 21:20:32 +01:00
|
|
|
}
|
2012-02-07 09:50:03 +01:00
|
|
|
//#my-thread-pool-dispatcher-config
|
|
|
|
|
|
|
|
|
|
//#my-pinned-dispatcher-config
|
|
|
|
|
my-pinned-dispatcher {
|
|
|
|
|
executor = "thread-pool-executor"
|
|
|
|
|
type = PinnedDispatcher
|
|
|
|
|
}
|
|
|
|
|
//#my-pinned-dispatcher-config
|
2011-12-14 15:12:40 +01:00
|
|
|
|
2011-12-12 21:20:32 +01:00
|
|
|
//#my-bounded-config
|
|
|
|
|
my-dispatcher-bounded-queue {
|
|
|
|
|
type = Dispatcher
|
2012-01-30 16:34:25 +01:00
|
|
|
executor = "thread-pool-executor"
|
|
|
|
|
thread-pool-executor {
|
|
|
|
|
core-pool-size-factor = 8.0
|
|
|
|
|
max-pool-size-factor = 16.0
|
|
|
|
|
}
|
2012-01-03 10:03:58 +01:00
|
|
|
# Specifies the bounded capacity of the mailbox queue
|
|
|
|
|
mailbox-capacity = 100
|
2011-12-12 21:20:32 +01:00
|
|
|
throughput = 3
|
|
|
|
|
}
|
|
|
|
|
//#my-bounded-config
|
2011-12-14 15:12:40 +01:00
|
|
|
|
2011-12-12 21:20:32 +01:00
|
|
|
//#my-balancing-config
|
|
|
|
|
my-balancing-dispatcher {
|
|
|
|
|
type = BalancingDispatcher
|
2012-01-30 16:34:25 +01:00
|
|
|
executor = "thread-pool-executor"
|
|
|
|
|
thread-pool-executor {
|
|
|
|
|
core-pool-size-factor = 8.0
|
|
|
|
|
max-pool-size-factor = 16.0
|
|
|
|
|
}
|
2011-12-12 21:20:32 +01:00
|
|
|
}
|
|
|
|
|
//#my-balancing-config
|
2011-12-21 20:34:46 +01:00
|
|
|
|
|
|
|
|
//#prio-dispatcher-config
|
|
|
|
|
prio-dispatcher {
|
2012-02-09 20:40:09 +01:00
|
|
|
mailbox-type = "akka.docs.dispatcher.DispatcherDocSpec$PrioMailbox"
|
2011-12-21 20:34:46 +01:00
|
|
|
}
|
|
|
|
|
//#prio-dispatcher-config
|
|
|
|
|
|
|
|
|
|
//#prio-dispatcher-config-java
|
|
|
|
|
prio-dispatcher-java {
|
2012-02-09 20:40:09 +01:00
|
|
|
mailbox-type = "akka.docs.dispatcher.DispatcherDocTestBase$PrioMailbox"
|
2011-12-21 20:34:46 +01:00
|
|
|
}
|
|
|
|
|
//#prio-dispatcher-config-java
|
2011-12-12 21:20:32 +01:00
|
|
|
"""
|
|
|
|
|
|
2011-12-21 20:34:46 +01:00
|
|
|
//#prio-mailbox
|
|
|
|
|
import akka.dispatch.PriorityGenerator
|
|
|
|
|
import akka.dispatch.UnboundedPriorityMailbox
|
2012-02-21 16:40:34 +01:00
|
|
|
import akka.dispatch.MailboxType
|
|
|
|
|
import akka.actor.ActorContext
|
2011-12-21 20:34:46 +01:00
|
|
|
import com.typesafe.config.Config
|
|
|
|
|
|
|
|
|
|
// We create a new Priority dispatcher and seed it with the priority generator
|
2012-02-21 18:24:38 +01:00
|
|
|
class PrioMailbox(config: Config) extends UnboundedPriorityMailbox(
|
|
|
|
|
PriorityGenerator { // Create a new PriorityGenerator, lower prio means more important
|
|
|
|
|
case 'highpriority ⇒ 0 // 'highpriority messages should be treated first if possible
|
|
|
|
|
case 'lowpriority ⇒ 100 // 'lowpriority messages should be treated last if possible
|
|
|
|
|
case PoisonPill ⇒ 1000 // PoisonPill when no other left
|
|
|
|
|
case otherwise ⇒ 50 // We default to 50
|
|
|
|
|
})
|
2011-12-21 20:34:46 +01:00
|
|
|
//#prio-mailbox
|
|
|
|
|
|
2011-12-12 21:20:32 +01:00
|
|
|
class MyActor extends Actor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case x ⇒
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DispatcherDocSpec extends AkkaSpec(DispatcherDocSpec.config) {
|
|
|
|
|
|
|
|
|
|
import DispatcherDocSpec.MyActor
|
|
|
|
|
|
|
|
|
|
"defining dispatcher" in {
|
|
|
|
|
//#defining-dispatcher
|
|
|
|
|
import akka.actor.Props
|
2011-12-20 21:08:27 +01:00
|
|
|
val myActor1 = system.actorOf(Props[MyActor].withDispatcher("my-dispatcher"), name = "myactor1")
|
|
|
|
|
val myActor2 = system.actorOf(Props[MyActor].withDispatcher("my-dispatcher"), name = "myactor2")
|
2011-12-12 21:20:32 +01:00
|
|
|
//#defining-dispatcher
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"defining dispatcher with bounded queue" in {
|
2011-12-21 19:07:54 +01:00
|
|
|
val dispatcher = system.dispatchers.lookup("my-dispatcher-bounded-queue")
|
2011-12-12 21:20:32 +01:00
|
|
|
}
|
|
|
|
|
|
2011-12-13 16:18:51 +01:00
|
|
|
"defining pinned dispatcher" in {
|
|
|
|
|
//#defining-pinned-dispatcher
|
2011-12-21 21:24:57 +01:00
|
|
|
val myActor = system.actorOf(Props[MyActor].withDispatcher("my-dispatcher"), name = "myactor")
|
2011-12-13 16:18:51 +01:00
|
|
|
//#defining-pinned-dispatcher
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-12 21:20:32 +01:00
|
|
|
"defining priority dispatcher" in {
|
|
|
|
|
//#prio-dispatcher
|
|
|
|
|
|
|
|
|
|
val a = system.actorOf( // We create a new Actor that just prints out what it processes
|
|
|
|
|
Props(new Actor {
|
|
|
|
|
val log: LoggingAdapter = Logging(context.system, this)
|
|
|
|
|
|
|
|
|
|
self ! 'lowpriority
|
|
|
|
|
self ! 'lowpriority
|
|
|
|
|
self ! 'highpriority
|
|
|
|
|
self ! 'pigdog
|
|
|
|
|
self ! 'pigdog2
|
|
|
|
|
self ! 'pigdog3
|
|
|
|
|
self ! 'highpriority
|
|
|
|
|
self ! PoisonPill
|
|
|
|
|
|
|
|
|
|
def receive = {
|
|
|
|
|
case x ⇒ log.info(x.toString)
|
|
|
|
|
}
|
2011-12-21 20:34:46 +01:00
|
|
|
}).withDispatcher("prio-dispatcher"))
|
2011-12-12 21:20:32 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Logs:
|
|
|
|
|
'highpriority
|
|
|
|
|
'highpriority
|
|
|
|
|
'pigdog
|
|
|
|
|
'pigdog2
|
|
|
|
|
'pigdog3
|
|
|
|
|
'lowpriority
|
|
|
|
|
'lowpriority
|
|
|
|
|
*/
|
|
|
|
|
//#prio-dispatcher
|
|
|
|
|
|
|
|
|
|
awaitCond(a.isTerminated, 5 seconds)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"defining balancing dispatcher" in {
|
2011-12-21 19:07:54 +01:00
|
|
|
val dispatcher = system.dispatchers.lookup("my-balancing-dispatcher")
|
2011-12-12 21:20:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|