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-10-27 12:23:01 +02:00
|
|
|
|
import akka.event.Logging.Error
|
2011-05-18 17:25:30 +02:00
|
|
|
|
import akka.util.{ Duration, Switch, ReentrantGuard }
|
2011-11-16 15:54:14 +01:00
|
|
|
|
import atomic.{ AtomicInteger, AtomicLong }
|
2011-05-18 17:25:30 +02:00
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor.{ AbortPolicy, CallerRunsPolicy, DiscardOldestPolicy, DiscardPolicy }
|
2010-10-26 12:49:25 +02:00
|
|
|
|
import akka.actor._
|
2011-11-10 20:08:00 +01:00
|
|
|
|
import akka.actor.ActorSystem
|
2011-11-16 15:54:14 +01:00
|
|
|
|
import locks.ReentrantLock
|
2011-10-18 18:06:17 +02:00
|
|
|
|
import scala.annotation.tailrec
|
2011-11-14 16:03:26 +01:00
|
|
|
|
import akka.event.EventStream
|
2011-11-17 11:51:14 +01:00
|
|
|
|
import akka.actor.ActorSystem.Settings
|
2011-11-15 11:34:39 +01:00
|
|
|
|
import com.typesafe.config.Config
|
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-10-22 16:06:20 +02:00
|
|
|
|
final case class Envelope(val message: Any, val sender: ActorRef) {
|
2011-10-19 17:48:27 +02:00
|
|
|
|
if (message.isInstanceOf[AnyRef] && (message.asInstanceOf[AnyRef] eq null)) throw new InvalidMessageException("Message is null")
|
2011-09-20 18:34:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-10-18 18:06:17 +02:00
|
|
|
|
object SystemMessage {
|
|
|
|
|
|
@tailrec
|
|
|
|
|
|
final def size(list: SystemMessage, acc: Int = 0): Int = {
|
|
|
|
|
|
if (list eq null) acc else size(list.next, acc + 1)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@tailrec
|
|
|
|
|
|
final def reverse(list: SystemMessage, acc: SystemMessage = null): SystemMessage = {
|
|
|
|
|
|
if (list eq null) acc else {
|
|
|
|
|
|
val next = list.next
|
|
|
|
|
|
list.next = acc
|
|
|
|
|
|
reverse(next, list)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* System messages are handled specially: they form their own queue within
|
|
|
|
|
|
* each actor’s mailbox. This queue is encoded in the messages themselves to
|
|
|
|
|
|
* avoid extra allocations and overhead. The next pointer is a normal var, and
|
|
|
|
|
|
* it does not need to be volatile because in the enqueuing method its update
|
|
|
|
|
|
* is immediately succeeded by a volatile write and all reads happen after the
|
|
|
|
|
|
* volatile read in the dequeuing thread. Afterwards, the obtained list of
|
|
|
|
|
|
* system messages is handled in a single thread only and not ever passed around,
|
|
|
|
|
|
* hence no further synchronization is needed.
|
|
|
|
|
|
*
|
|
|
|
|
|
* ➡➡➡ NEVER SEND THE SAME SYSTEM MESSAGE OBJECT TO TWO ACTORS ⬅⬅⬅
|
|
|
|
|
|
*/
|
2011-10-18 16:44:35 +02:00
|
|
|
|
sealed trait SystemMessage extends PossiblyHarmful {
|
2011-10-18 18:06:17 +02:00
|
|
|
|
var next: SystemMessage = _
|
2009-05-25 14:48:43 +02:00
|
|
|
|
}
|
2011-10-18 15:39:26 +02:00
|
|
|
|
case class Create() extends SystemMessage // send to self from Dispatcher.register
|
|
|
|
|
|
case class Recreate(cause: Throwable) extends SystemMessage // sent to self from ActorCell.restart
|
|
|
|
|
|
case class Suspend() extends SystemMessage // sent to self from ActorCell.suspend
|
|
|
|
|
|
case class Resume() extends SystemMessage // sent to self from ActorCell.resume
|
|
|
|
|
|
case class Terminate() extends SystemMessage // sent to self from ActorCell.stop
|
|
|
|
|
|
case class Supervise(child: ActorRef) extends SystemMessage // sent to supervisor ActorRef from ActorCell.start
|
2011-12-03 18:16:41 +01:00
|
|
|
|
case class ChildTerminated(child: ActorRef) extends SystemMessage // sent to supervisor from ActorCell.doTerminate
|
2011-11-16 16:46:16 +01:00
|
|
|
|
case class Link(subject: ActorRef) extends SystemMessage // sent to self from ActorCell.startsWatching
|
|
|
|
|
|
case class Unlink(subject: ActorRef) extends SystemMessage // sent to self from ActorCell.stopsWatching
|
2009-12-13 12:29:18 +01:00
|
|
|
|
|
2011-11-14 16:03:26 +01:00
|
|
|
|
final case class TaskInvocation(eventStream: EventStream, 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-11-30 10:20:57 +01:00
|
|
|
|
// FIXME catching all and continue isn't good for OOME, ticket #1418
|
2011-11-18 11:59:43 +01:00
|
|
|
|
case e ⇒ eventStream.publish(Error(e, "TaskInvocation", 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 {
|
2011-11-16 16:09:58 +01:00
|
|
|
|
val UNSCHEDULED = 0 //WARNING DO NOT CHANGE THE VALUE OF THIS: It relies on the faster init of 0 in AbstractMessageDispatcher
|
2011-05-18 08:37:58 +02:00
|
|
|
|
val SCHEDULED = 1
|
2010-10-25 00:01:31 +02:00
|
|
|
|
val RESCHEDULED = 2
|
2011-10-11 16:05:48 +02:00
|
|
|
|
|
2011-11-17 12:36:35 +01:00
|
|
|
|
implicit def defaultDispatcher(implicit system: ActorSystem) = system.dispatcher
|
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-11-17 16:09:18 +01:00
|
|
|
|
abstract class MessageDispatcher(val prerequisites: DispatcherPrerequisites) extends AbstractMessageDispatcher with Serializable {
|
2011-11-14 16:03:26 +01:00
|
|
|
|
|
2010-10-25 00:38:48 +02:00
|
|
|
|
import MessageDispatcher._
|
2011-11-16 16:09:58 +01:00
|
|
|
|
import AbstractMessageDispatcher.{ inhabitantsUpdater, shutdownScheduleUpdater }
|
2011-11-17 16:09:18 +01:00
|
|
|
|
import prerequisites._
|
2010-11-22 17:58:21 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Creates and returns a mailbox for the given actor.
|
|
|
|
|
|
*/
|
2011-09-21 15:01:47 +02:00
|
|
|
|
protected[akka] def createMailbox(actor: ActorCell): Mailbox
|
|
|
|
|
|
|
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-11-16 16:19:56 +01:00
|
|
|
|
final def attach(actor: ActorCell): Unit = register(actor)
|
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-11-16 16:19:56 +01:00
|
|
|
|
final def detach(actor: ActorCell): Unit = try {
|
2011-11-16 15:54:14 +01:00
|
|
|
|
unregister(actor)
|
2011-11-16 16:19:56 +01:00
|
|
|
|
} finally {
|
2011-11-16 15:54:14 +01:00
|
|
|
|
ifSensibleToDoSoThenScheduleShutdown()
|
2011-09-26 17:52:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-09-27 17:41:02 +02:00
|
|
|
|
protected[akka] final def dispatchTask(block: () ⇒ Unit) {
|
2011-11-17 14:01:57 +01:00
|
|
|
|
val invocation = TaskInvocation(eventStream, block, taskCleanup)
|
2011-11-16 16:09:58 +01:00
|
|
|
|
inhabitantsUpdater.incrementAndGet(this)
|
2011-04-28 16:01:11 +02:00
|
|
|
|
try {
|
2011-11-16 15:54:14 +01:00
|
|
|
|
executeTask(invocation)
|
2011-04-28 16:01:11 +02:00
|
|
|
|
} catch {
|
2011-05-18 17:25:30 +02:00
|
|
|
|
case e ⇒
|
2011-11-16 16:09:58 +01:00
|
|
|
|
inhabitantsUpdater.decrementAndGet(this)
|
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-11-16 15:54:14 +01:00
|
|
|
|
@tailrec
|
2011-11-16 16:09:58 +01:00
|
|
|
|
private final def ifSensibleToDoSoThenScheduleShutdown(): Unit = inhabitantsUpdater.get(this) match {
|
2011-11-16 15:54:14 +01:00
|
|
|
|
case 0 ⇒
|
2011-11-16 16:09:58 +01:00
|
|
|
|
shutdownScheduleUpdater.get(this) match {
|
2011-11-16 15:54:14 +01:00
|
|
|
|
case UNSCHEDULED ⇒
|
2011-11-16 16:09:58 +01:00
|
|
|
|
if (shutdownScheduleUpdater.compareAndSet(this, UNSCHEDULED, SCHEDULED)) {
|
2011-12-02 17:13:46 +01:00
|
|
|
|
scheduler.scheduleOnce(shutdownTimeout, shutdownAction)
|
2011-11-16 15:54:14 +01:00
|
|
|
|
()
|
|
|
|
|
|
} else ifSensibleToDoSoThenScheduleShutdown()
|
|
|
|
|
|
case SCHEDULED ⇒
|
2011-11-16 16:09:58 +01:00
|
|
|
|
if (shutdownScheduleUpdater.compareAndSet(this, SCHEDULED, RESCHEDULED)) ()
|
2011-11-16 15:54:14 +01:00
|
|
|
|
else ifSensibleToDoSoThenScheduleShutdown()
|
|
|
|
|
|
case RESCHEDULED ⇒ ()
|
|
|
|
|
|
}
|
|
|
|
|
|
case _ ⇒ ()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-11-16 16:09:58 +01:00
|
|
|
|
private final val taskCleanup: () ⇒ Unit =
|
|
|
|
|
|
() ⇒ if (inhabitantsUpdater.decrementAndGet(this) == 0) ifSensibleToDoSoThenScheduleShutdown()
|
2011-02-25 15:20:58 -07:00
|
|
|
|
|
2011-07-15 09:39:04 +02:00
|
|
|
|
/**
|
2011-11-16 16:19:56 +01:00
|
|
|
|
* If you override it, you must call it. But only ever once. See "attach" for only invocation
|
2011-07-15 09:39:04 +02:00
|
|
|
|
*/
|
2011-09-27 17:41:02 +02:00
|
|
|
|
protected[akka] def register(actor: ActorCell) {
|
2011-11-16 16:09:58 +01:00
|
|
|
|
inhabitantsUpdater.incrementAndGet(this)
|
2011-10-18 18:06:17 +02:00
|
|
|
|
// ➡➡➡ NEVER SEND THE SAME SYSTEM MESSAGE OBJECT TO TWO ACTORS ⬅⬅⬅
|
2011-10-18 16:47:07 +02:00
|
|
|
|
systemDispatch(actor, Create()) //FIXME should this be here or moved into ActorCell.start perhaps?
|
2010-09-01 16:33:56 +02:00
|
|
|
|
}
|
2010-10-29 16:33:31 +02:00
|
|
|
|
|
2011-07-15 09:39:04 +02:00
|
|
|
|
/**
|
2011-11-16 16:19:56 +01:00
|
|
|
|
* If you override it, you must call it. But only ever once. See "detach" for the only invocation
|
2011-07-15 09:39:04 +02:00
|
|
|
|
*/
|
2011-09-27 17:41:02 +02:00
|
|
|
|
protected[akka] def unregister(actor: ActorCell) {
|
2011-11-16 16:09:58 +01:00
|
|
|
|
inhabitantsUpdater.decrementAndGet(this)
|
2011-10-18 15:09:35 +02:00
|
|
|
|
val mailBox = actor.mailbox
|
2011-11-16 15:54:14 +01:00
|
|
|
|
mailBox.becomeClosed() // FIXME reschedule in tell if possible race with cleanUp is detected in order to properly clean up
|
2011-11-12 22:37:12 +01:00
|
|
|
|
actor.mailbox = deadLetterMailbox
|
2011-10-18 15:09:35 +02:00
|
|
|
|
cleanUpMailboxFor(actor, mailBox)
|
2011-11-15 14:39:43 +01:00
|
|
|
|
mailBox.cleanUp()
|
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-27 17:41:02 +02:00
|
|
|
|
protected def cleanUpMailboxFor(actor: ActorCell, mailBox: Mailbox) {
|
2011-09-22 17:15:51 +02:00
|
|
|
|
|
2011-09-26 11:39:07 +02:00
|
|
|
|
if (mailBox.hasSystemMessages) {
|
2011-10-18 18:06:17 +02:00
|
|
|
|
var message = mailBox.systemDrain()
|
|
|
|
|
|
while (message ne null) {
|
2011-11-11 17:55:17 +01:00
|
|
|
|
// message must be “virgin” before being able to systemEnqueue again
|
2011-11-11 17:44:57 +01:00
|
|
|
|
val next = message.next
|
2011-11-11 17:55:17 +01:00
|
|
|
|
message.next = null
|
2011-11-12 10:57:28 +01:00
|
|
|
|
deadLetterMailbox.systemEnqueue(actor.self, message)
|
2011-11-11 17:44:57 +01:00
|
|
|
|
message = next
|
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) {
|
2011-11-12 10:57:28 +01:00
|
|
|
|
deadLetterMailbox.enqueue(actor.self, 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-11-16 15:54:14 +01:00
|
|
|
|
@tailrec
|
|
|
|
|
|
final def run() {
|
2011-11-16 16:09:58 +01:00
|
|
|
|
shutdownScheduleUpdater.get(MessageDispatcher.this) match {
|
2011-11-16 15:54:14 +01:00
|
|
|
|
case UNSCHEDULED ⇒ ()
|
|
|
|
|
|
case SCHEDULED ⇒
|
|
|
|
|
|
try {
|
2011-11-16 16:09:58 +01:00
|
|
|
|
if (inhabitantsUpdater.get(MessageDispatcher.this) == 0) //Warning, racy
|
2011-11-16 15:54:14 +01:00
|
|
|
|
shutdown()
|
|
|
|
|
|
} finally {
|
2011-11-16 16:09:58 +01:00
|
|
|
|
shutdownScheduleUpdater.getAndSet(MessageDispatcher.this, UNSCHEDULED) //TODO perhaps check if it was modified since we checked?
|
2011-11-16 15:54:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
case RESCHEDULED ⇒
|
2011-11-16 16:09:58 +01:00
|
|
|
|
if (shutdownScheduleUpdater.compareAndSet(MessageDispatcher.this, RESCHEDULED, SCHEDULED))
|
2011-12-05 22:46:34 +01:00
|
|
|
|
try scheduler.scheduleOnce(shutdownTimeout, this) catch {
|
2011-12-03 21:26:32 +01:00
|
|
|
|
case _: IllegalStateException ⇒ shutdown()
|
|
|
|
|
|
}
|
2011-11-16 15:54:14 +01:00
|
|
|
|
else run()
|
|
|
|
|
|
}
|
2010-10-25 00:01:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2010-11-12 14:04:06 +01:00
|
|
|
|
/**
|
2011-11-21 10:48:21 +01:00
|
|
|
|
* When the dispatcher no longer has any actors registered, how long will it wait until it shuts itself down,
|
|
|
|
|
|
* defaulting to your akka configs "akka.actor.dispatcher-shutdown-timeout" or default specified in
|
|
|
|
|
|
* akka-actor-reference.conf
|
2010-11-12 14:04:06 +01:00
|
|
|
|
*/
|
2011-11-21 10:48:21 +01:00
|
|
|
|
protected[akka] def shutdownTimeout: Duration
|
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-10-18 15:09:35 +02:00
|
|
|
|
def suspend(actor: ActorCell): Unit = {
|
|
|
|
|
|
val mbox = actor.mailbox
|
|
|
|
|
|
if (mbox.dispatcher eq this)
|
|
|
|
|
|
mbox.becomeSuspended()
|
|
|
|
|
|
}
|
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-10-18 15:09:35 +02:00
|
|
|
|
def resume(actor: ActorCell): Unit = {
|
2011-09-23 09:33:53 +02:00
|
|
|
|
val mbox = actor.mailbox
|
2011-11-18 17:03:35 +01:00
|
|
|
|
if ((mbox.dispatcher eq this) && mbox.becomeOpen())
|
2011-10-18 15:09:35 +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
|
|
|
|
|
|
*/
|
2011-10-18 16:44:35 +02:00
|
|
|
|
protected[akka] def systemDispatch(receiver: ActorCell, invocation: SystemMessage)
|
2011-09-26 19:52:49 +02:00
|
|
|
|
|
2010-10-24 16:01:00 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Will be called when the dispatcher is to queue an invocation for execution
|
|
|
|
|
|
*/
|
2011-10-19 13:19:44 +02:00
|
|
|
|
protected[akka] def dispatch(receiver: ActorCell, invocation: Envelope)
|
2011-09-21 15:01:47 +02:00
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
2011-11-21 10:48:21 +01:00
|
|
|
|
protected[akka] def throughputDeadlineTime: Duration
|
2010-10-24 01:18:59 +02:00
|
|
|
|
|
2011-10-07 11:34:07 +02:00
|
|
|
|
@inline
|
2011-11-21 10:48:21 +01:00
|
|
|
|
protected[akka] final val isThroughputDeadlineTimeDefined = throughputDeadlineTime.toMillis > 0
|
2011-10-07 11:34:07 +02:00
|
|
|
|
@inline
|
|
|
|
|
|
protected[akka] final val isThroughputDefined = throughput > 1
|
|
|
|
|
|
|
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 detached from this dispatcher and this dispatcher has no actors left attached
|
2011-11-16 15:54:14 +01:00
|
|
|
|
* Must be idempotent
|
2010-10-24 16:01:00 +02:00
|
|
|
|
*/
|
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
|
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
|
|
|
|
|
|
*/
|
2011-11-17 16:09:18 +01:00
|
|
|
|
abstract class MessageDispatcherConfigurator() {
|
2011-04-12 15:40:09 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Returns an instance of MessageDispatcher given a Configuration
|
|
|
|
|
|
*/
|
2011-11-15 11:34:39 +01:00
|
|
|
|
def configure(config: Config, settings: Settings, prerequisites: DispatcherPrerequisites): MessageDispatcher
|
2011-03-04 20:55:12 +01:00
|
|
|
|
|
2011-11-15 11:34:39 +01:00
|
|
|
|
def mailboxType(config: Config, settings: Settings): MailboxType = {
|
|
|
|
|
|
val capacity = config.getInt("mailbox-capacity")
|
2011-04-05 12:06:27 +02:00
|
|
|
|
if (capacity < 1) UnboundedMailbox()
|
2011-05-18 08:37:58 +02:00
|
|
|
|
else {
|
2011-11-21 10:48:21 +01:00
|
|
|
|
val duration = Duration(config.getNanoseconds("mailbox-push-timeout-time"), TimeUnit.NANOSECONDS)
|
2011-05-18 08:37:58 +02:00
|
|
|
|
BoundedMailbox(capacity, duration)
|
|
|
|
|
|
}
|
2011-03-04 20:55:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-11-15 11:34:39 +01:00
|
|
|
|
def configureThreadPool(config: Config,
|
2011-11-17 16:09:18 +01:00
|
|
|
|
settings: Settings,
|
|
|
|
|
|
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-11-15 11:34:39 +01:00
|
|
|
|
|
2011-11-17 16:09:18 +01:00
|
|
|
|
ThreadPoolConfigDispatcherBuilder(createDispatcher, ThreadPoolConfig()).configure(
|
2011-11-21 10:48:21 +01:00
|
|
|
|
conf_?(Some(config getMilliseconds "keep-alive-time"))(time ⇒ _.setKeepAliveTime(Duration(time, TimeUnit.MILLISECONDS))),
|
2011-11-15 11:34:39 +01:00
|
|
|
|
conf_?(Some(config getDouble "core-pool-size-factor"))(factor ⇒ _.setCorePoolSizeFromFactor(factor)),
|
|
|
|
|
|
conf_?(Some(config getDouble "max-pool-size-factor"))(factor ⇒ _.setMaxPoolSizeFromFactor(factor)),
|
|
|
|
|
|
conf_?(Some(config getBoolean "allow-core-timeout"))(allow ⇒ _.setAllowCoreThreadTimeout(allow)),
|
|
|
|
|
|
conf_?(Some(config getInt "task-queue-size") flatMap {
|
2011-08-24 12:47:11 +02:00
|
|
|
|
case size if size > 0 ⇒
|
2011-11-15 11:34:39 +01:00
|
|
|
|
Some(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
|
2011-11-15 10:25:05 +01:00
|
|
|
|
})(queueFactory ⇒ _.setQueueFactory(queueFactory)))
|
2011-03-04 20:55:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|