pekko/akka-actor/src/main/scala/akka/dispatch/MessageHandling.scala

246 lines
8.3 KiB
Scala
Raw Normal View History

/**
* Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
*/
package akka.dispatch
2010-09-07 18:32:50 +02:00
import java.util.concurrent._
import atomic. {AtomicInteger, AtomicBoolean, AtomicReference, AtomicLong}
import akka.event.EventHandler
2011-03-08 20:29:50 +08:00
import akka.config.Configuration
import akka.config.Config.TIME_UNIT
import akka.util.{Duration, Switch, ReentrantGuard, HashCode, ReflectiveAccess}
import java.util.concurrent.ThreadPoolExecutor.{AbortPolicy, CallerRunsPolicy, DiscardOldestPolicy, DiscardPolicy}
import akka.actor._
/**
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
*/
2011-01-20 18:33:29 +01:00
final case class MessageInvocation(val receiver: ActorRef,
2011-03-14 13:38:27 +01:00
val message: Any,
val sender: Option[ActorRef],
val senderFuture: Option[CompletableFuture[Any]]) {
2010-09-21 18:52:41 +02:00
if (receiver eq null) throw new IllegalArgumentException("Receiver can't be null")
2010-06-30 16:26:15 +02:00
def invoke = try {
receiver.invoke(this)
} catch {
case e: NullPointerException => throw new ActorInitializationException(
2010-09-21 18:52:41 +02:00
"Don't call 'self ! message' in the Actor's constructor (in Scala this means in the body of the class).")
}
}
final case class FutureInvocation(future: CompletableFuture[Any], function: () => Any) extends Runnable {
val uuid = akka.actor.newUuid
def run = future complete (try {
Right(function.apply)
} catch {
case e =>
EventHandler.error(e, this, e.getMessage)
Left(e)
})
}
object MessageDispatcher {
val UNSCHEDULED = 0
val SCHEDULED = 1
val RESCHEDULED = 2
implicit def defaultGlobalDispatcher = Dispatchers.defaultGlobalDispatcher
}
/**
2010-09-07 18:32:50 +02:00
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
*/
2011-02-28 22:54:32 +01:00
trait MessageDispatcher {
import MessageDispatcher._
protected val uuids = new ConcurrentSkipListSet[Uuid]
2011-02-25 18:26:46 -07:00
protected val futures = new ConcurrentSkipListSet[Uuid]
protected val guard = new ReentrantGuard
protected val active = new Switch(false)
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
/**
* Attaches the specified actorRef to this dispatcher
*/
final def attach(actorRef: ActorRef): Unit = guard withGuard {
register(actorRef)
}
/**
* Detaches the specified actorRef from this dispatcher
*/
final def detach(actorRef: ActorRef): Unit = guard withGuard {
unregister(actorRef)
}
private[akka] final def dispatchMessage(invocation: MessageInvocation): Unit = dispatch(invocation)
private[akka] final def dispatchFuture(invocation: FutureInvocation): Unit = {
2011-02-28 11:48:51 -07:00
guard withGuard {
futures add invocation.uuid
if (active.isOff) { active.switchOn { start } }
}
invocation.future.onComplete { f =>
2011-02-28 11:48:51 -07:00
guard withGuard {
futures remove invocation.uuid
if (futures.isEmpty && uuids.isEmpty) {
shutdownSchedule match {
case UNSCHEDULED =>
shutdownSchedule = SCHEDULED
Scheduler.scheduleOnce(shutdownAction, timeoutMs, TimeUnit.MILLISECONDS)
case SCHEDULED =>
shutdownSchedule = RESCHEDULED
case RESCHEDULED => //Already marked for reschedule
}
}
}
}
executeFuture(invocation)
}
private[akka] def register(actorRef: ActorRef) {
2011-01-20 17:16:44 +01:00
if (actorRef.mailbox eq null)
actorRef.mailbox = createMailbox(actorRef)
uuids add actorRef.uuid
if (active.isOff) {
active.switchOn {
start
}
}
}
2010-10-29 16:33:31 +02:00
private[akka] def unregister(actorRef: ActorRef) = {
if (uuids remove actorRef.uuid) {
actorRef.mailbox = null
2011-02-25 18:26:46 -07:00
if (uuids.isEmpty && futures.isEmpty){
shutdownSchedule match {
case UNSCHEDULED =>
shutdownSchedule = SCHEDULED
Scheduler.scheduleOnce(shutdownAction, timeoutMs, TimeUnit.MILLISECONDS)
case SCHEDULED =>
shutdownSchedule = RESCHEDULED
case RESCHEDULED => //Already marked for reschedule
}
}
}
}
/**
* Traverses the list of actors (uuids) currently being attached to this dispatcher and stops those actors
*/
def stopAllAttachedActors {
val i = uuids.iterator
2011-03-02 18:37:41 +01:00
while (i.hasNext()) {
val uuid = i.next()
Actor.registry.actorFor(uuid) match {
case Some(actor) => actor.stop
2011-03-02 18:37:41 +01:00
case None => {}
}
}
}
private val shutdownAction = new Runnable {
def run = guard withGuard {
shutdownSchedule match {
case RESCHEDULED =>
shutdownSchedule = SCHEDULED
Scheduler.scheduleOnce(this, timeoutMs, TimeUnit.MILLISECONDS)
case SCHEDULED =>
2011-02-25 18:26:46 -07:00
if (uuids.isEmpty() && futures.isEmpty) {
active switchOff {
shutdown // shut down in the dispatcher's references is zero
}
}
shutdownSchedule = UNSCHEDULED
case UNSCHEDULED => //Do nothing
}
}
}
/**
* 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
/**
* After the call to this method, the dispatcher mustn't begin any new message processing for the specified reference
*/
2010-10-11 16:11:51 +02:00
def suspend(actorRef: ActorRef): Unit
/*
* After the call to this method, the dispatcher must begin any new message processing for the specified reference
*/
2010-10-11 16:11:51 +02:00
def resume(actorRef: ActorRef): Unit
/**
* Will be called when the dispatcher is to queue an invocation for execution
*/
private[akka] def dispatch(invocation: MessageInvocation): Unit
private[akka] def executeFuture(invocation: FutureInvocation): Unit
/**
* Called one time every time an actor is attached to this dispatcher and this dispatcher was previously shutdown
*/
private[akka] def start: Unit
/**
* Called one time every time an actor is detached from this dispatcher and this dispatcher has no actors left attached
*/
private[akka] def shutdown: Unit
/**
* Returns the size of the mailbox for the specified actor
*/
2010-09-21 18:52:41 +02:00
def mailboxSize(actorRef: ActorRef): Int
/**
* Returns the size of the Future queue
*/
def futureQueueSize: Int = futures.size
2010-10-29 16:33:31 +02:00
}
/**
* Trait to be used for hooking in new dispatchers into Dispatchers.fromConfig
*/
abstract class MessageDispatcherConfigurator {
2011-03-08 20:29:50 +08:00
def configure(config: Configuration): MessageDispatcher
2011-03-08 20:29:50 +08:00
def mailboxType(config: Configuration): MailboxType = {
val capacity = config.getInt("mailbox-capacity", Dispatchers.MAILBOX_CAPACITY)
// FIXME how do we read in isBlocking for mailbox? Now set to 'false'.
if (capacity < 0) UnboundedMailbox()
else BoundedMailbox(false, capacity, Duration(config.getInt("mailbox-push-timeout-time", Dispatchers.MAILBOX_PUSH_TIME_OUT.toMillis.toInt), TIME_UNIT))
}
2011-03-08 20:29:50 +08:00
def configureThreadPool(config: Configuration, createDispatcher: => (ThreadPoolConfig) => MessageDispatcher): ThreadPoolConfigDispatcherBuilder = {
import ThreadPoolConfigDispatcherBuilder.conf_?
//Apply the following options to the config if they are present in the config
ThreadPoolConfigDispatcherBuilder(createDispatcher,ThreadPoolConfig()).configure(
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)),
conf_?(config getString "rejection-policy" map {
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)))
}
}