2009-05-25 14:48:43 +02:00
|
|
|
/**
|
2010-12-22 15:35:50 +01:00
|
|
|
* Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
|
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._
|
2010-10-25 00:01:31 +02:00
|
|
|
import atomic. {AtomicInteger, AtomicBoolean, AtomicReference, AtomicLong}
|
2010-11-22 17:58:21 +01:00
|
|
|
|
|
|
|
|
import akka.util.{Switch, ReentrantGuard, Logging, HashCode, ReflectiveAccess}
|
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-01-20 18:33:29 +01:00
|
|
|
final case class MessageInvocation(val receiver: ActorRef,
|
2009-12-13 12:29:18 +01:00
|
|
|
val message: Any,
|
2010-05-19 10:09:30 +02:00
|
|
|
val sender: Option[ActorRef],
|
2010-11-05 16:11:34 +13:00
|
|
|
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")
|
2009-05-25 14:48:43 +02:00
|
|
|
|
2010-06-30 16:26:15 +02:00
|
|
|
def invoke = try {
|
2010-06-07 09:06:42 +02:00
|
|
|
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).")
|
2010-06-07 09:06:42 +02:00
|
|
|
}
|
2009-05-25 14:48:43 +02:00
|
|
|
}
|
2009-12-13 12:29:18 +01:00
|
|
|
|
2010-10-25 00:01:31 +02:00
|
|
|
object MessageDispatcher {
|
|
|
|
|
val UNSCHEDULED = 0
|
|
|
|
|
val SCHEDULED = 1
|
|
|
|
|
val RESCHEDULED = 2
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-01 18:10:48 +02:00
|
|
|
/**
|
2010-09-07 18:32:50 +02:00
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
2010-06-01 18:10:48 +02:00
|
|
|
*/
|
2011-01-20 17:16:44 +01:00
|
|
|
trait MessageDispatcher extends Logging {
|
2010-10-25 00:38:48 +02:00
|
|
|
import MessageDispatcher._
|
2010-11-22 17:58:21 +01:00
|
|
|
|
2010-09-17 16:04:25 +02:00
|
|
|
protected val uuids = new ConcurrentSkipListSet[Uuid]
|
2010-10-24 15:22:28 +02:00
|
|
|
protected val guard = new ReentrantGuard
|
2010-10-24 16:01:00 +02:00
|
|
|
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
|
|
|
|
2010-10-24 16:01:00 +02:00
|
|
|
/**
|
|
|
|
|
* Attaches the specified actorRef to this dispatcher
|
|
|
|
|
*/
|
2010-10-24 15:22:28 +02:00
|
|
|
final def attach(actorRef: ActorRef): Unit = guard withGuard {
|
|
|
|
|
register(actorRef)
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-24 16:01:00 +02:00
|
|
|
/**
|
|
|
|
|
* Detaches the specified actorRef from this dispatcher
|
|
|
|
|
*/
|
2010-10-24 15:22:28 +02:00
|
|
|
final def detach(actorRef: ActorRef): Unit = guard withGuard {
|
|
|
|
|
unregister(actorRef)
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-24 16:01:00 +02:00
|
|
|
private[akka] final def dispatchMessage(invocation: MessageInvocation): Unit = if (active.isOn) {
|
|
|
|
|
dispatch(invocation)
|
|
|
|
|
} else throw new IllegalActorStateException("Can't submit invocations to dispatcher since it's not started")
|
|
|
|
|
|
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 {
|
|
|
|
|
start
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-01 16:33:56 +02:00
|
|
|
}
|
2010-10-29 16:33:31 +02:00
|
|
|
|
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) {
|
|
|
|
|
actorRef.mailbox = null
|
2010-10-24 16:01:00 +02:00
|
|
|
if (uuids.isEmpty){
|
2010-10-25 00:38:48 +02:00
|
|
|
shutdownSchedule match {
|
|
|
|
|
case UNSCHEDULED =>
|
|
|
|
|
shutdownSchedule = SCHEDULED
|
2010-10-25 00:01:31 +02:00
|
|
|
Scheduler.scheduleOnce(shutdownAction, timeoutMs, TimeUnit.MILLISECONDS)
|
2010-10-25 00:38:48 +02:00
|
|
|
case SCHEDULED =>
|
|
|
|
|
shutdownSchedule = RESCHEDULED
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2010-10-24 16:01:00 +02:00
|
|
|
/**
|
|
|
|
|
* Traverses the list of actors (uuids) currently being attached to this dispatcher and stops those actors
|
|
|
|
|
*/
|
2010-10-24 16:31:19 +02:00
|
|
|
def stopAllAttachedActors {
|
2010-10-24 01:18:59 +02:00
|
|
|
val i = uuids.iterator
|
|
|
|
|
while(i.hasNext()) {
|
|
|
|
|
val uuid = i.next()
|
2011-01-04 13:24:28 +01:00
|
|
|
Actor.registry.actorFor(uuid) match {
|
2010-10-24 01:18:59 +02:00
|
|
|
case Some(actor) => actor.stop
|
2010-10-24 16:01:00 +02:00
|
|
|
case None =>
|
2010-11-24 13:42:41 +01:00
|
|
|
log.slf4j.error("stopAllLinkedActors couldn't find linked actor: " + uuid)
|
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 {
|
|
|
|
|
def run = guard withGuard {
|
2010-10-25 00:38:48 +02:00
|
|
|
shutdownSchedule match {
|
|
|
|
|
case RESCHEDULED =>
|
|
|
|
|
shutdownSchedule = SCHEDULED
|
2010-10-25 00:01:31 +02:00
|
|
|
Scheduler.scheduleOnce(this, timeoutMs, TimeUnit.MILLISECONDS)
|
2010-10-25 00:38:48 +02:00
|
|
|
case SCHEDULED =>
|
2010-10-25 00:01:31 +02:00
|
|
|
if (uuids.isEmpty()) {
|
|
|
|
|
active switchOff {
|
|
|
|
|
shutdown // shut down in the dispatcher's references is zero
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-10-25 00:38:48 +02:00
|
|
|
shutdownSchedule = UNSCHEDULED
|
|
|
|
|
case UNSCHEDULED => //Do nothing
|
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
|
|
|
|
|
*/
|
2010-10-11 16:11:51 +02:00
|
|
|
def suspend(actorRef: ActorRef): Unit
|
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
|
|
|
|
|
*/
|
2010-10-11 16:11:51 +02:00
|
|
|
def resume(actorRef: ActorRef): Unit
|
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
|
|
|
|
|
*/
|
2010-10-25 12:51:13 +02:00
|
|
|
private[akka] def dispatch(invocation: MessageInvocation): Unit
|
2010-10-24 01:18:59 +02: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
|
|
|
|
|
*/
|
2010-10-25 12:51:13 +02:00
|
|
|
private[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
|
|
|
|
|
*/
|
2010-10-25 12:51:13 +02:00
|
|
|
private[akka] def shutdown: Unit
|
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
|
2010-10-29 16:33:31 +02:00
|
|
|
}
|