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-06-13 22:36:46 +02:00
|
|
|
final case class MessageInvocation(val receiver: ActorRef,
|
|
|
|
|
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-06-06 19:42:52 -07:00
|
|
|
final def invoke() {
|
|
|
|
|
receiver invoke this
|
2010-06-07 09:06:42 +02:00
|
|
|
}
|
2009-05-25 14:48:43 +02:00
|
|
|
}
|
2009-12-13 12:29:18 +01:00
|
|
|
|
2011-05-23 11:31:01 +02:00
|
|
|
final case class FutureInvocation[T](future: Promise[T], function: () ⇒ T, cleanup: () ⇒ Unit) extends Runnable {
|
2011-05-18 08:37:58 +02:00
|
|
|
def run() {
|
2011-04-27 20:45:39 -06:00
|
|
|
future complete (try {
|
|
|
|
|
Right(function())
|
|
|
|
|
} catch {
|
2011-05-18 17:25:30 +02:00
|
|
|
case e ⇒
|
2011-04-27 20:45:39 -06:00
|
|
|
EventHandler.error(e, this, e.getMessage)
|
|
|
|
|
Left(e)
|
2011-05-18 17:25:30 +02:00
|
|
|
}
|
|
|
|
|
finally {
|
2011-04-27 20:45:39 -06:00
|
|
|
cleanup()
|
|
|
|
|
})
|
|
|
|
|
}
|
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-02-28 22:54:32 +01:00
|
|
|
trait MessageDispatcher {
|
2011-05-18 08:37:58 +02:00
|
|
|
|
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-04-27 20:45:39 -06:00
|
|
|
protected val futures = 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-01-20 17:16:44 +01:00
|
|
|
private[akka] def createMailbox(actorRef: ActorRef): AnyRef
|
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
|
|
|
/**
|
|
|
|
|
* Attaches the specified actorRef to this dispatcher
|
|
|
|
|
*/
|
2011-05-18 08:37:58 +02:00
|
|
|
final def attach(actorRef: ActorRef) {
|
|
|
|
|
guard withGuard {
|
|
|
|
|
register(actorRef)
|
|
|
|
|
}
|
2010-10-24 15:22:28 +02:00
|
|
|
}
|
|
|
|
|
|
2010-10-24 16:01:00 +02:00
|
|
|
/**
|
|
|
|
|
* Detaches the specified actorRef from this dispatcher
|
|
|
|
|
*/
|
2011-05-18 08:37:58 +02:00
|
|
|
final def detach(actorRef: ActorRef) {
|
|
|
|
|
guard withGuard {
|
|
|
|
|
unregister(actorRef)
|
|
|
|
|
}
|
2010-10-24 15:22:28 +02:00
|
|
|
}
|
|
|
|
|
|
2011-05-18 08:37:58 +02:00
|
|
|
private[akka] final def dispatchMessage(invocation: MessageInvocation) {
|
|
|
|
|
dispatch(invocation)
|
|
|
|
|
}
|
2010-10-24 16:01:00 +02:00
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
private[akka] final def dispatchFuture[T](block: () ⇒ T, timeout: Long): Future[T] = {
|
2011-04-27 20:45:39 -06:00
|
|
|
futures.getAndIncrement()
|
2011-04-28 16:01:11 +02:00
|
|
|
try {
|
2011-05-23 11:31:01 +02:00
|
|
|
val future = new DefaultPromise[T](timeout)
|
2011-04-28 16:01:11 +02:00
|
|
|
|
|
|
|
|
if (active.isOff)
|
2011-05-18 08:37:58 +02:00
|
|
|
guard withGuard {
|
|
|
|
|
active.switchOn {
|
|
|
|
|
start()
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-28 16:01:11 +02:00
|
|
|
|
|
|
|
|
executeFuture(FutureInvocation[T](future, block, futureCleanup))
|
|
|
|
|
future
|
|
|
|
|
} catch {
|
2011-05-18 17:25:30 +02:00
|
|
|
case e ⇒
|
2011-04-28 16:01:11 +02:00
|
|
|
futures.decrementAndGet
|
|
|
|
|
throw e
|
2011-02-28 11:48:51 -07:00
|
|
|
}
|
2011-04-27 20:45:39 -06:00
|
|
|
}
|
|
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
private val futureCleanup: () ⇒ Unit =
|
|
|
|
|
() ⇒ if (futures.decrementAndGet() == 0) {
|
2011-02-28 11:48:51 -07:00
|
|
|
guard withGuard {
|
2011-04-28 16:01:11 +02:00
|
|
|
if (futures.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
|
|
|
|
|
*/
|
2010-10-25 12:51:13 +02:00
|
|
|
private[akka] def register(actorRef: ActorRef) {
|
2011-01-20 17:16:44 +01:00
|
|
|
if (actorRef.mailbox eq null)
|
|
|
|
|
actorRef.mailbox = createMailbox(actorRef)
|
|
|
|
|
|
2010-10-25 00:01:31 +02:00
|
|
|
uuids add actorRef.uuid
|
2010-10-24 21:39:39 +02:00
|
|
|
if (active.isOff) {
|
2010-10-24 16:01:00 +02:00
|
|
|
active.switchOn {
|
2011-05-18 08:37:58 +02:00
|
|
|
start()
|
2010-10-24 16:01:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
|
*/
|
2010-10-25 12:51:13 +02:00
|
|
|
private[akka] def unregister(actorRef: ActorRef) = {
|
2010-10-24 15:22:28 +02:00
|
|
|
if (uuids remove actorRef.uuid) {
|
2011-05-26 20:38:42 +02:00
|
|
|
cleanUpMailboxFor(actorRef)
|
2010-10-24 15:22:28 +02:00
|
|
|
actorRef.mailbox = null
|
2011-05-18 08:37:58 +02:00
|
|
|
if (uuids.isEmpty && futures.get == 0) {
|
2010-10-25 00:38:48 +02:00
|
|
|
shutdownSchedule match {
|
2011-05-18 17:25:30 +02:00
|
|
|
case UNSCHEDULED ⇒
|
2010-10-25 00:38:48 +02:00
|
|
|
shutdownSchedule = SCHEDULED
|
2010-10-25 00:01:31 +02:00
|
|
|
Scheduler.scheduleOnce(shutdownAction, timeoutMs, TimeUnit.MILLISECONDS)
|
2011-05-18 17:25:30 +02:00
|
|
|
case SCHEDULED ⇒
|
2010-10-25 00:38:48 +02:00
|
|
|
shutdownSchedule = RESCHEDULED
|
2011-05-18 17:25:30 +02:00
|
|
|
case RESCHEDULED ⇒ //Already marked for reschedule
|
2010-10-25 00:01:31 +02:00
|
|
|
}
|
2010-10-24 16:01:00 +02:00
|
|
|
}
|
2010-10-24 15:22:28 +02:00
|
|
|
}
|
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.
|
|
|
|
|
*/
|
|
|
|
|
protected def cleanUpMailboxFor(actorRef: ActorRef) {}
|
|
|
|
|
|
2010-10-24 16:01:00 +02:00
|
|
|
/**
|
|
|
|
|
* Traverses the list of actors (uuids) currently being attached to this dispatcher and stops those actors
|
|
|
|
|
*/
|
2011-05-18 08:37:58 +02:00
|
|
|
def stopAllAttachedActors() {
|
2010-10-24 01:18:59 +02:00
|
|
|
val i = uuids.iterator
|
2011-03-02 18:37:41 +01:00
|
|
|
while (i.hasNext()) {
|
2010-10-24 01:18:59 +02:00
|
|
|
val uuid = i.next()
|
2011-04-08 15:29:14 +02:00
|
|
|
Actor.registry.local.actorFor(uuid) match {
|
2011-05-18 17:25:30 +02:00
|
|
|
case Some(actor) ⇒ actor.stop()
|
2011-06-06 19:42:52 -07:00
|
|
|
case None ⇒
|
2010-10-24 01:18:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
2010-04-01 22:34:16 +02:00
|
|
|
}
|
2010-10-24 16:01:00 +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-05-18 08:37:58 +02:00
|
|
|
if (uuids.isEmpty && futures.get == 0) {
|
|
|
|
|
active switchOff {
|
|
|
|
|
shutdown() // shut down in the dispatcher's references is zero
|
|
|
|
|
}
|
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
|
|
|
|
|
*/
|
|
|
|
|
private[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-05-18 08:37:58 +02:00
|
|
|
def suspend(actorRef: ActorRef)
|
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-05-18 08:37:58 +02:00
|
|
|
def resume(actorRef: ActorRef)
|
2010-09-01 16:33:56 +02:00
|
|
|
|
2010-10-24 16:01:00 +02:00
|
|
|
/**
|
|
|
|
|
* Will be called when the dispatcher is to queue an invocation for execution
|
|
|
|
|
*/
|
2011-05-18 08:37:58 +02:00
|
|
|
private[akka] def dispatch(invocation: MessageInvocation)
|
2010-10-24 01:18:59 +02:00
|
|
|
|
2011-05-18 08:37:58 +02:00
|
|
|
private[akka] def executeFuture(invocation: FutureInvocation[_])
|
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-05-18 08:37:58 +02:00
|
|
|
private[akka] def start()
|
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-05-18 08:37:58 +02:00
|
|
|
private[akka] def shutdown()
|
2010-09-01 16:33:56 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the size of the mailbox for the specified actor
|
|
|
|
|
*/
|
2010-09-21 18:52:41 +02:00
|
|
|
def mailboxSize(actorRef: ActorRef): Int
|
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
|
|
|
|
|
*/
|
|
|
|
|
def mailboxIsEmpty(actorRef: ActorRef): Boolean
|
|
|
|
|
|
2011-03-20 17:15:23 -06:00
|
|
|
/**
|
2011-04-27 20:45:39 -06:00
|
|
|
* Returns the amount of futures queued for execution
|
2011-03-20 17:15:23 -06:00
|
|
|
*/
|
2011-04-27 20:45:39 -06:00
|
|
|
def pendingFutures: Long = futures.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-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()
|
|
|
|
|
case x ⇒ throw new IllegalArgumentException("[%s] is not a valid rejectionPolicy!" format x)
|
|
|
|
|
})(policy ⇒ _.setRejectionPolicy(policy)))
|
2011-03-04 20:55:12 +01:00
|
|
|
}
|
|
|
|
|
}
|