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

155 lines
4.9 KiB
Scala
Raw Normal View History

/**
2009-12-27 16:01:53 +01:00
* Copyright (C) 2009-2010 Scalable Solutions AB <http://scalablesolutions.se>
*/
package se.scalablesolutions.akka.dispatch
import org.multiverse.commitbarriers.CountDownCommitBarrier
2010-09-21 18:52:41 +02:00
2010-09-07 18:32:50 +02:00
import java.util.concurrent._
import atomic. {AtomicReference, AtomicLong}
import se.scalablesolutions.akka.util. {Switch, ReentrantGuard, Logging, HashCode}
import se.scalablesolutions.akka.actor._
/**
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
*/
2010-05-06 08:13:12 +02:00
final class MessageInvocation(val receiver: ActorRef,
val message: Any,
val sender: Option[ActorRef],
val senderFuture: Option[CompletableFuture[Any]],
val transactionSet: Option[CountDownCommitBarrier]) {
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).")
}
2010-09-21 18:52:41 +02:00
override def hashCode(): Int = {
var result = HashCode.SEED
2010-04-30 20:22:45 +02:00
result = HashCode.hash(result, receiver.actor)
result = HashCode.hash(result, message.asInstanceOf[AnyRef])
result
}
2010-09-21 18:52:41 +02:00
override def equals(that: Any): Boolean = {
that.isInstanceOf[MessageInvocation] &&
2010-04-30 20:22:45 +02:00
that.asInstanceOf[MessageInvocation].receiver.actor == receiver.actor &&
2009-07-28 17:48:11 +02:00
that.asInstanceOf[MessageInvocation].message == message
2009-10-06 00:07:27 +02:00
}
2010-09-21 18:52:41 +02:00
override def toString = {
2009-12-08 16:17:22 +01:00
"MessageInvocation[" +
"\n\tmessage = " + message +
"\n\treceiver = " + receiver +
"\n\tsender = " + sender +
"\n\tsenderFuture = " + senderFuture +
"\n\ttransactionSet = " + transactionSet +
"]"
2009-10-06 00:07:27 +02:00
}
}
/**
2010-09-07 18:32:50 +02:00
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
*/
2010-09-21 18:52:41 +02:00
trait MessageDispatcher extends MailboxFactory with Logging {
2010-09-17 16:04:25 +02:00
protected val uuids = new ConcurrentSkipListSet[Uuid]
protected val guard = new ReentrantGuard
private val scheduledShutdown = new AtomicReference[ScheduledFuture[AnyRef]](null)
protected val active = new Switch(false)
/**
* 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 = if (active.isOn) {
dispatch(invocation)
} else throw new IllegalActorStateException("Can't submit invocations to dispatcher since it's not started")
protected def register(actorRef: ActorRef) {
if (actorRef.mailbox eq null) actorRef.mailbox = createMailbox(actorRef)
if (uuids add actorRef.uuid) {
val future = scheduledShutdown.getAndSet(null)
if (future ne null) future.cancel(false)
}
if (active.isOff) {
active.switchOn {
start
}
}
}
2010-09-21 18:52:41 +02:00
protected def unregister(actorRef: ActorRef) = {
if (uuids remove actorRef.uuid) {
actorRef.mailbox = null
if (uuids.isEmpty){
val future = scheduledShutdown.getAndSet(Scheduler.scheduleOnce(() => guard withGuard {
if (uuids.isEmpty()) {
active switchOff {
shutdown // shut down in the dispatcher's references is zero
}
}
}, 1, TimeUnit.SECONDS))
if (future ne null) future.cancel(false)
}
}
}
/**
* Traverses the list of actors (uuids) currently being attached to this dispatcher and stops those actors
*/
def stopAllAttachedActors {
val i = uuids.iterator
while(i.hasNext()) {
val uuid = i.next()
ActorRegistry.actorFor(uuid) match {
case Some(actor) => actor.stop
case None =>
log.error("stopAllLinkedActors couldn't find linked actor: " + uuid)
}
}
}
/**
* 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
*/
protected def dispatch(invocation: MessageInvocation): Unit
/**
* Called one time every time an actor is attached to this dispatcher and this dispatcher was previously shutdown
*/
protected def start: Unit
/**
* Called one time every time an actor is detached from this dispatcher and this dispatcher has no actors left attached
*/
protected 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
}