2009-05-25 14:48:43 +02:00
|
|
|
/**
|
2011-07-14 16:03:08 +02:00
|
|
|
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
2009-05-25 14:48:43 +02:00
|
|
|
*/
|
|
|
|
|
|
2010-10-26 12:49:25 +02:00
|
|
|
package akka.dispatch
|
2009-05-25 14:48:43 +02:00
|
|
|
|
2010-09-07 18:32:50 +02:00
|
|
|
import java.util.concurrent._
|
2011-04-27 20:45:39 -06:00
|
|
|
import java.util.concurrent.atomic.AtomicLong
|
2011-03-23 15:12:09 +01:00
|
|
|
import akka.event.EventHandler
|
2011-03-08 20:29:50 +08:00
|
|
|
import akka.config.Configuration
|
2011-03-04 20:55:12 +01:00
|
|
|
import akka.config.Config.TIME_UNIT
|
2011-05-18 17:25:30 +02:00
|
|
|
import akka.util.{ Duration, Switch, ReentrantGuard }
|
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor.{ AbortPolicy, CallerRunsPolicy, DiscardOldestPolicy, DiscardPolicy }
|
2010-10-26 12:49:25 +02:00
|
|
|
import akka.actor._
|
2010-02-23 19:49:01 +01:00
|
|
|
|
2010-06-01 18:10:48 +02:00
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2011-09-21 15:01:47 +02:00
|
|
|
final case class Envelope(val receiver: ActorCell, val message: Any, val channel: UntypedChannel) {
|
2010-09-21 18:52:41 +02:00
|
|
|
if (receiver eq null) throw new IllegalArgumentException("Receiver can't be null")
|
2009-05-25 14:48:43 +02:00
|
|
|
|
2011-09-23 13:14:17 +02:00
|
|
|
final def invoke() {
|
|
|
|
|
receiver invoke this
|
|
|
|
|
}
|
2011-09-20 18:34:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sealed trait SystemMessage
|
|
|
|
|
case object Create extends SystemMessage
|
|
|
|
|
case object Recreate extends SystemMessage
|
|
|
|
|
case object Suspend extends SystemMessage
|
|
|
|
|
case object Resume extends SystemMessage
|
|
|
|
|
case object Terminate extends SystemMessage
|
|
|
|
|
|
2011-09-21 15:01:47 +02:00
|
|
|
final case class SystemEnvelope(val receiver: ActorCell, val message: SystemMessage, val channel: UntypedChannel) {
|
2011-09-20 18:34:21 +02:00
|
|
|
if (receiver eq null) throw new IllegalArgumentException("Receiver can't be null")
|
2011-09-21 15:01:47 +02:00
|
|
|
/**
|
|
|
|
|
* @return whether to proceed with processing other messages
|
|
|
|
|
*/
|
2011-09-23 13:14:17 +02:00
|
|
|
final def invoke(): Unit = {
|
|
|
|
|
receiver systemInvoke this
|
|
|
|
|
}
|
2009-05-25 14:48:43 +02:00
|
|
|
}
|
2009-12-13 12:29:18 +01:00
|
|
|
|
2011-07-26 22:23:16 -06:00
|
|
|
final case class TaskInvocation(function: () ⇒ Unit, cleanup: () ⇒ Unit) extends Runnable {
|
2011-05-18 08:37:58 +02:00
|
|
|
def run() {
|
2011-07-26 22:23:16 -06:00
|
|
|
try {
|
|
|
|
|
function()
|
2011-04-27 20:45:39 -06:00
|
|
|
} catch {
|
2011-07-26 22:23:16 -06:00
|
|
|
case e ⇒ EventHandler.error(e, this, e.getMessage)
|
2011-07-26 18:33:59 +12:00
|
|
|
} finally {
|
2011-04-27 20:45:39 -06:00
|
|
|
cleanup()
|
2011-07-26 22:23:16 -06:00
|
|
|
}
|
2011-04-27 20:45:39 -06:00
|
|
|
}
|
2011-02-25 15:20:58 -07:00
|
|
|
}
|
|
|
|
|
|
2010-10-25 00:01:31 +02:00
|
|
|
object MessageDispatcher {
|
|
|
|
|
val UNSCHEDULED = 0
|
2011-05-18 08:37:58 +02:00
|
|
|
val SCHEDULED = 1
|
2010-10-25 00:01:31 +02:00
|
|
|
val RESCHEDULED = 2
|
2011-02-28 10:37:42 -07:00
|
|
|
|
|
|
|
|
implicit def defaultGlobalDispatcher = Dispatchers.defaultGlobalDispatcher
|
2010-10-25 00:01:31 +02:00
|
|
|
}
|
|
|
|
|
|
2010-06-01 18:10:48 +02:00
|
|
|
/**
|
2011-05-18 08:37:58 +02:00
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
2010-06-01 18:10:48 +02:00
|
|
|
*/
|
2011-09-08 11:02:17 +02:00
|
|
|
abstract class MessageDispatcher extends Serializable {
|
2010-10-25 00:38:48 +02:00
|
|
|
import MessageDispatcher._
|
2010-11-22 17:58:21 +01:00
|
|
|
|
2011-05-18 08:37:58 +02:00
|
|
|
protected val uuids = new ConcurrentSkipListSet[Uuid]
|
2011-08-02 11:21:48 -06:00
|
|
|
protected val _tasks = new AtomicLong(0L)
|
2011-05-18 08:37:58 +02:00
|
|
|
protected val guard = new ReentrantGuard
|
|
|
|
|
protected val active = new Switch(false)
|
2010-09-01 16:33:56 +02:00
|
|
|
|
2010-11-22 17:58:21 +01:00
|
|
|
private var shutdownSchedule = UNSCHEDULED //This can be non-volatile since it is protected by guard withGuard
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates and returns a mailbox for the given actor.
|
|
|
|
|
*/
|
2011-09-21 15:01:47 +02:00
|
|
|
protected[akka] def createMailbox(actor: ActorCell): Mailbox
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a blackhole mailbox for the purpose of replacing the real one upon actor termination
|
|
|
|
|
*/
|
2011-09-23 16:21:57 +02:00
|
|
|
protected[akka] val deadLetterMailbox: Mailbox = DeadLetterMailbox
|
|
|
|
|
|
|
|
|
|
object DeadLetterMailbox extends Mailbox {
|
|
|
|
|
dispatcherLock.tryLock()
|
2011-09-26 11:39:07 +02:00
|
|
|
become(Mailbox.Closed)
|
2011-09-21 16:27:31 +02:00
|
|
|
override def dispatcher = null //MessageDispatcher.this
|
|
|
|
|
override def enqueue(envelope: Envelope) { envelope.channel sendException new ActorKilledException("Actor has been stopped") }
|
2011-09-21 15:01:47 +02:00
|
|
|
override def dequeue() = null
|
2011-09-21 16:27:31 +02:00
|
|
|
override def systemEnqueue(handle: SystemEnvelope): Unit = ()
|
|
|
|
|
override def systemDequeue(): SystemEnvelope = null
|
|
|
|
|
override def hasMessages = false
|
|
|
|
|
override def hasSystemMessages = false
|
|
|
|
|
override def numberOfMessages = 0
|
2011-09-21 15:01:47 +02:00
|
|
|
}
|
2010-11-22 17:58:21 +01:00
|
|
|
|
2011-07-25 13:24:39 +12:00
|
|
|
/**
|
|
|
|
|
* Name of this dispatcher.
|
|
|
|
|
*/
|
|
|
|
|
def name: String
|
|
|
|
|
|
2010-10-24 16:01:00 +02:00
|
|
|
/**
|
2011-09-15 08:12:07 +02:00
|
|
|
* Attaches the specified actor instance to this dispatcher
|
2010-10-24 16:01:00 +02:00
|
|
|
*/
|
2011-09-20 15:43:57 +02:00
|
|
|
final def attach(actor: ActorCell): Unit = {
|
2011-09-19 15:25:19 +02:00
|
|
|
guard.lock.lock()
|
|
|
|
|
try {
|
2011-09-26 19:52:49 +02:00
|
|
|
startIfUnstarted()
|
2011-09-15 08:12:07 +02:00
|
|
|
register(actor)
|
2011-09-19 15:25:19 +02:00
|
|
|
} finally {
|
|
|
|
|
guard.lock.unlock()
|
|
|
|
|
}
|
2010-10-24 15:22:28 +02:00
|
|
|
}
|
|
|
|
|
|
2010-10-24 16:01:00 +02:00
|
|
|
/**
|
2011-09-15 08:12:07 +02:00
|
|
|
* Detaches the specified actor instance from this dispatcher
|
2010-10-24 16:01:00 +02:00
|
|
|
*/
|
2011-09-20 15:43:57 +02:00
|
|
|
final def detach(actor: ActorCell): Unit = {
|
2011-05-18 08:37:58 +02:00
|
|
|
guard withGuard {
|
2011-09-15 08:12:07 +02:00
|
|
|
unregister(actor)
|
2011-09-26 19:52:49 +02:00
|
|
|
if (uuids.isEmpty && _tasks.get == 0) {
|
|
|
|
|
shutdownSchedule match {
|
|
|
|
|
case UNSCHEDULED ⇒
|
|
|
|
|
shutdownSchedule = SCHEDULED
|
|
|
|
|
Scheduler.scheduleOnce(shutdownAction, timeoutMs, TimeUnit.MILLISECONDS)
|
|
|
|
|
case SCHEDULED ⇒
|
|
|
|
|
shutdownSchedule = RESCHEDULED
|
|
|
|
|
case RESCHEDULED ⇒ //Already marked for reschedule
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-18 08:37:58 +02:00
|
|
|
}
|
2010-10-24 15:22:28 +02:00
|
|
|
}
|
|
|
|
|
|
2011-09-26 17:52:52 +02:00
|
|
|
protected final def startIfUnstarted(): Unit = {
|
|
|
|
|
if (active.isOff) guard withGuard { active.switchOn { start() } }
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-30 15:50:52 +02:00
|
|
|
protected[akka] final def dispatchTask(block: () ⇒ Unit): Unit = {
|
2011-08-02 11:21:48 -06:00
|
|
|
_tasks.getAndIncrement()
|
2011-04-28 16:01:11 +02:00
|
|
|
try {
|
2011-09-26 19:52:49 +02:00
|
|
|
startIfUnstarted()
|
2011-07-26 22:23:16 -06:00
|
|
|
executeTask(TaskInvocation(block, taskCleanup))
|
2011-04-28 16:01:11 +02:00
|
|
|
} catch {
|
2011-05-18 17:25:30 +02:00
|
|
|
case e ⇒
|
2011-08-02 11:21:48 -06:00
|
|
|
_tasks.decrementAndGet
|
2011-04-28 16:01:11 +02:00
|
|
|
throw e
|
2011-02-28 11:48:51 -07:00
|
|
|
}
|
2011-04-27 20:45:39 -06:00
|
|
|
}
|
|
|
|
|
|
2011-07-26 22:23:16 -06:00
|
|
|
private val taskCleanup: () ⇒ Unit =
|
2011-08-02 11:21:48 -06:00
|
|
|
() ⇒ if (_tasks.decrementAndGet() == 0) {
|
2011-02-28 11:48:51 -07:00
|
|
|
guard withGuard {
|
2011-08-02 11:21:48 -06:00
|
|
|
if (_tasks.get == 0 && uuids.isEmpty) {
|
2011-02-28 11:48:51 -07:00
|
|
|
shutdownSchedule match {
|
2011-05-18 17:25:30 +02:00
|
|
|
case UNSCHEDULED ⇒
|
2011-02-28 11:48:51 -07:00
|
|
|
shutdownSchedule = SCHEDULED
|
|
|
|
|
Scheduler.scheduleOnce(shutdownAction, timeoutMs, TimeUnit.MILLISECONDS)
|
2011-05-18 17:25:30 +02:00
|
|
|
case SCHEDULED ⇒
|
2011-02-28 11:48:51 -07:00
|
|
|
shutdownSchedule = RESCHEDULED
|
2011-05-18 17:25:30 +02:00
|
|
|
case RESCHEDULED ⇒ //Already marked for reschedule
|
2011-02-28 11:48:51 -07:00
|
|
|
}
|
2011-02-25 15:20:58 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-15 09:39:04 +02:00
|
|
|
/**
|
|
|
|
|
* Only "private[akka] for the sake of intercepting calls, DO NOT CALL THIS OUTSIDE OF THE DISPATCHER,
|
|
|
|
|
* and only call it under the dispatcher-guard, see "attach" for the only invocation
|
|
|
|
|
*/
|
2011-09-23 13:14:17 +02:00
|
|
|
protected[akka] def register(actor: ActorCell): Unit = {
|
2011-09-23 16:21:57 +02:00
|
|
|
if (uuids add actor.uuid) {
|
2011-09-26 15:28:13 +02:00
|
|
|
systemDispatch(SystemEnvelope(actor, Create, NullChannel)) //FIXME should this be here or moved into ActorCell.start perhaps?
|
2011-09-23 16:21:57 +02:00
|
|
|
} else System.err.println("Couldn't register: " + actor)
|
2010-09-01 16:33:56 +02:00
|
|
|
}
|
2010-10-29 16:33:31 +02:00
|
|
|
|
2011-07-15 09:39:04 +02:00
|
|
|
/**
|
|
|
|
|
* Only "private[akka] for the sake of intercepting calls, DO NOT CALL THIS OUTSIDE OF THE DISPATCHER,
|
|
|
|
|
* and only call it under the dispatcher-guard, see "detach" for the only invocation
|
|
|
|
|
*/
|
2011-09-23 13:14:17 +02:00
|
|
|
protected[akka] def unregister(actor: ActorCell): Unit = {
|
2011-09-15 08:12:07 +02:00
|
|
|
if (uuids remove actor.uuid) {
|
2011-09-22 17:15:51 +02:00
|
|
|
val mailBox = actor.mailbox
|
2011-09-26 11:39:07 +02:00
|
|
|
mailBox.become(Mailbox.Closed)
|
2011-09-23 16:21:57 +02:00
|
|
|
actor.mailbox = deadLetterMailbox //FIXME getAndSet would be preferrable here
|
2011-09-22 17:15:51 +02:00
|
|
|
cleanUpMailboxFor(actor, mailBox)
|
2011-09-23 16:21:57 +02:00
|
|
|
} else System.err.println("Couldn't unregister: " + actor)
|
2010-10-24 01:18:59 +02:00
|
|
|
}
|
|
|
|
|
|
2011-05-26 20:38:42 +02:00
|
|
|
/**
|
|
|
|
|
* Overridable callback to clean up the mailbox for a given actor,
|
|
|
|
|
* called when an actor is unregistered.
|
|
|
|
|
*/
|
2011-09-23 13:14:17 +02:00
|
|
|
protected def cleanUpMailboxFor(actor: ActorCell, mailBox: Mailbox): Unit = {
|
2011-09-22 17:15:51 +02:00
|
|
|
|
2011-09-26 11:39:07 +02:00
|
|
|
if (mailBox.hasSystemMessages) {
|
|
|
|
|
var envelope = mailBox.systemDequeue()
|
2011-09-22 17:15:51 +02:00
|
|
|
while (envelope ne null) {
|
|
|
|
|
deadLetterMailbox.systemEnqueue(envelope)
|
2011-09-26 11:39:07 +02:00
|
|
|
envelope = mailBox.systemDequeue()
|
2011-09-22 17:15:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-26 11:39:07 +02:00
|
|
|
if (mailBox.hasMessages) {
|
|
|
|
|
var envelope = mailBox.dequeue
|
2011-09-22 17:15:51 +02:00
|
|
|
while (envelope ne null) {
|
|
|
|
|
deadLetterMailbox.enqueue(envelope)
|
2011-09-26 11:39:07 +02:00
|
|
|
envelope = mailBox.dequeue
|
2011-09-22 17:15:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-26 20:38:42 +02:00
|
|
|
|
2010-10-25 00:01:31 +02:00
|
|
|
private val shutdownAction = new Runnable {
|
2011-05-18 08:37:58 +02:00
|
|
|
def run() {
|
|
|
|
|
guard withGuard {
|
|
|
|
|
shutdownSchedule match {
|
2011-05-18 17:25:30 +02:00
|
|
|
case RESCHEDULED ⇒
|
2011-05-18 08:37:58 +02:00
|
|
|
shutdownSchedule = SCHEDULED
|
|
|
|
|
Scheduler.scheduleOnce(this, timeoutMs, TimeUnit.MILLISECONDS)
|
2011-05-18 17:25:30 +02:00
|
|
|
case SCHEDULED ⇒
|
2011-08-02 11:21:48 -06:00
|
|
|
if (uuids.isEmpty && _tasks.get == 0) {
|
2011-05-18 08:37:58 +02:00
|
|
|
active switchOff {
|
2011-09-26 17:52:52 +02:00
|
|
|
shutdown() // shut down in the dispatcher's references is zero
|
2011-05-18 08:37:58 +02:00
|
|
|
}
|
2010-10-25 00:01:31 +02:00
|
|
|
}
|
2011-05-18 08:37:58 +02:00
|
|
|
shutdownSchedule = UNSCHEDULED
|
2011-05-18 17:25:30 +02:00
|
|
|
case UNSCHEDULED ⇒ //Do nothing
|
2011-05-18 08:37:58 +02:00
|
|
|
}
|
2010-10-25 00:01:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-12 14:04:06 +01:00
|
|
|
/**
|
|
|
|
|
* When the dispatcher no longer has any actors registered, how long will it wait until it shuts itself down, in Ms
|
|
|
|
|
* defaulting to your akka configs "akka.actor.dispatcher-shutdown-timeout" or otherwise, 1 Second
|
|
|
|
|
*/
|
2011-08-30 15:50:52 +02:00
|
|
|
protected[akka] def timeoutMs: Long = Dispatchers.DEFAULT_SHUTDOWN_TIMEOUT.toMillis
|
2010-10-25 00:01:31 +02:00
|
|
|
|
2010-10-24 16:01:00 +02:00
|
|
|
/**
|
|
|
|
|
* After the call to this method, the dispatcher mustn't begin any new message processing for the specified reference
|
|
|
|
|
*/
|
2011-09-23 13:14:17 +02:00
|
|
|
def suspend(actor: ActorCell): Unit =
|
2011-09-26 11:39:07 +02:00
|
|
|
if (uuids.contains(actor.uuid)) actor.mailbox.become(Mailbox.Suspended)
|
2010-10-24 16:01:00 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* After the call to this method, the dispatcher must begin any new message processing for the specified reference
|
|
|
|
|
*/
|
2011-09-23 09:33:53 +02:00
|
|
|
def resume(actor: ActorCell): Unit = if (uuids.contains(actor.uuid)) {
|
|
|
|
|
val mbox = actor.mailbox
|
2011-09-26 11:39:07 +02:00
|
|
|
mbox.become(Mailbox.Open)
|
2011-09-23 13:14:17 +02:00
|
|
|
registerForExecution(mbox, false, false)
|
2011-09-23 09:33:53 +02:00
|
|
|
}
|
2010-09-01 16:33:56 +02:00
|
|
|
|
2011-09-26 19:52:49 +02:00
|
|
|
/**
|
|
|
|
|
* Will be called when the dispatcher is to queue an invocation for execution
|
|
|
|
|
*/
|
|
|
|
|
protected[akka] def systemDispatch(invocation: SystemEnvelope)
|
|
|
|
|
|
2010-10-24 16:01:00 +02:00
|
|
|
/**
|
|
|
|
|
* Will be called when the dispatcher is to queue an invocation for execution
|
|
|
|
|
*/
|
2011-09-21 15:01:47 +02:00
|
|
|
protected[akka] def dispatch(invocation: Envelope)
|
|
|
|
|
|
|
|
|
|
/**
|
2011-09-23 13:14:17 +02:00
|
|
|
* Suggest to register the provided mailbox for execution
|
2011-09-21 15:01:47 +02:00
|
|
|
*/
|
2011-09-23 13:14:17 +02:00
|
|
|
protected[akka] def registerForExecution(mbox: Mailbox, hasMessageHint: Boolean, hasSystemMessageHint: Boolean): Boolean
|
2011-09-21 15:01:47 +02:00
|
|
|
|
|
|
|
|
// TODO check whether this should not actually be a property of the mailbox
|
|
|
|
|
protected[akka] def throughput: Int
|
|
|
|
|
protected[akka] def throughputDeadlineTime: Int
|
2010-10-24 01:18:59 +02:00
|
|
|
|
2011-08-30 15:50:52 +02:00
|
|
|
protected[akka] def executeTask(invocation: TaskInvocation)
|
2011-02-25 15:20:58 -07:00
|
|
|
|
2010-10-24 16:01:00 +02:00
|
|
|
/**
|
|
|
|
|
* Called one time every time an actor is attached to this dispatcher and this dispatcher was previously shutdown
|
|
|
|
|
*/
|
2011-08-30 15:50:52 +02:00
|
|
|
protected[akka] def start(): Unit
|
2010-10-24 16:01:00 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called one time every time an actor is detached from this dispatcher and this dispatcher has no actors left attached
|
|
|
|
|
*/
|
2011-08-30 15:50:52 +02:00
|
|
|
protected[akka] def shutdown(): Unit
|
2010-09-01 16:33:56 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the size of the mailbox for the specified actor
|
|
|
|
|
*/
|
2011-09-21 16:27:31 +02:00
|
|
|
def mailboxSize(actor: ActorCell): Int = actor.mailbox.numberOfMessages
|
2011-03-20 17:15:23 -06:00
|
|
|
|
2011-06-07 13:23:24 -05:00
|
|
|
/**
|
|
|
|
|
* Returns the "current" emptiness status of the mailbox for the specified actor
|
|
|
|
|
*/
|
2011-09-21 17:22:08 +02:00
|
|
|
def mailboxIsEmpty(actor: ActorCell): Boolean = !actor.mailbox.hasMessages
|
2011-06-07 13:23:24 -05:00
|
|
|
|
2011-03-20 17:15:23 -06:00
|
|
|
/**
|
2011-07-26 22:23:16 -06:00
|
|
|
* Returns the amount of tasks queued for execution
|
2011-03-20 17:15:23 -06:00
|
|
|
*/
|
2011-08-02 11:21:48 -06:00
|
|
|
def tasks: Long = _tasks.get
|
2010-10-29 16:33:31 +02:00
|
|
|
}
|
2011-03-04 20:55:12 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Trait to be used for hooking in new dispatchers into Dispatchers.fromConfig
|
|
|
|
|
*/
|
|
|
|
|
abstract class MessageDispatcherConfigurator {
|
2011-04-12 15:40:09 +02:00
|
|
|
/**
|
|
|
|
|
* Returns an instance of MessageDispatcher given a Configuration
|
|
|
|
|
*/
|
2011-03-08 20:29:50 +08:00
|
|
|
def configure(config: Configuration): MessageDispatcher
|
2011-03-04 20:55:12 +01:00
|
|
|
|
2011-03-08 20:29:50 +08:00
|
|
|
def mailboxType(config: Configuration): MailboxType = {
|
2011-03-04 20:55:12 +01:00
|
|
|
val capacity = config.getInt("mailbox-capacity", Dispatchers.MAILBOX_CAPACITY)
|
2011-04-05 12:06:27 +02:00
|
|
|
if (capacity < 1) UnboundedMailbox()
|
2011-05-18 08:37:58 +02:00
|
|
|
else {
|
|
|
|
|
val duration = Duration(
|
|
|
|
|
config.getInt("mailbox-push-timeout-time", Dispatchers.MAILBOX_PUSH_TIME_OUT.toMillis.toInt),
|
|
|
|
|
TIME_UNIT)
|
|
|
|
|
BoundedMailbox(capacity, duration)
|
|
|
|
|
}
|
2011-03-04 20:55:12 +01:00
|
|
|
}
|
|
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
def configureThreadPool(config: Configuration, createDispatcher: ⇒ (ThreadPoolConfig) ⇒ MessageDispatcher): ThreadPoolConfigDispatcherBuilder = {
|
2011-03-04 20:55:12 +01:00
|
|
|
import ThreadPoolConfigDispatcherBuilder.conf_?
|
|
|
|
|
|
|
|
|
|
//Apply the following options to the config if they are present in the config
|
2011-05-18 08:37:58 +02:00
|
|
|
ThreadPoolConfigDispatcherBuilder(createDispatcher, ThreadPoolConfig()).configure(
|
2011-05-18 17:25:30 +02:00
|
|
|
conf_?(config getInt "keep-alive-time")(time ⇒ _.setKeepAliveTime(Duration(time, TIME_UNIT))),
|
|
|
|
|
conf_?(config getDouble "core-pool-size-factor")(factor ⇒ _.setCorePoolSizeFromFactor(factor)),
|
|
|
|
|
conf_?(config getDouble "max-pool-size-factor")(factor ⇒ _.setMaxPoolSizeFromFactor(factor)),
|
|
|
|
|
conf_?(config getInt "executor-bounds")(bounds ⇒ _.setExecutorBounds(bounds)),
|
|
|
|
|
conf_?(config getBool "allow-core-timeout")(allow ⇒ _.setAllowCoreThreadTimeout(allow)),
|
2011-08-24 12:41:21 +02:00
|
|
|
conf_?(config getInt "task-queue-size" flatMap {
|
2011-08-24 12:47:11 +02:00
|
|
|
case size if size > 0 ⇒
|
2011-08-24 12:41:21 +02:00
|
|
|
config getString "task-queue-type" map {
|
2011-08-24 12:47:11 +02:00
|
|
|
case "array" ⇒ ThreadPoolConfig.arrayBlockingQueue(size, false) //TODO config fairness?
|
|
|
|
|
case "" | "linked" ⇒ ThreadPoolConfig.linkedBlockingQueue(size)
|
|
|
|
|
case x ⇒ throw new IllegalArgumentException("[%s] is not a valid task-queue-type [array|linked]!" format x)
|
2011-08-24 12:41:21 +02:00
|
|
|
}
|
2011-08-24 12:47:11 +02:00
|
|
|
case _ ⇒ None
|
|
|
|
|
})(queueFactory ⇒ _.setQueueFactory(queueFactory)),
|
2011-03-04 20:55:12 +01:00
|
|
|
conf_?(config getString "rejection-policy" map {
|
2011-05-18 17:25:30 +02:00
|
|
|
case "abort" ⇒ new AbortPolicy()
|
|
|
|
|
case "caller-runs" ⇒ new CallerRunsPolicy()
|
|
|
|
|
case "discard-oldest" ⇒ new DiscardOldestPolicy()
|
|
|
|
|
case "discard" ⇒ new DiscardPolicy()
|
2011-08-24 12:41:21 +02:00
|
|
|
case x ⇒ throw new IllegalArgumentException("[%s] is not a valid rejectionPolicy [abort|caller-runs|discard-oldest|discard]!" format x)
|
2011-05-18 17:25:30 +02:00
|
|
|
})(policy ⇒ _.setRejectionPolicy(policy)))
|
2011-03-04 20:55:12 +01:00
|
|
|
}
|
|
|
|
|
}
|