2009-05-25 14:48:43 +02:00
|
|
|
/**
|
2009-12-27 16:01:53 +01:00
|
|
|
* Copyright (C) 2009-2010 Scalable Solutions AB <http://scalablesolutions.se>
|
2009-05-25 14:48:43 +02:00
|
|
|
*/
|
|
|
|
|
|
2009-10-08 19:01:04 +02:00
|
|
|
package se.scalablesolutions.akka.dispatch
|
2009-05-25 14:48:43 +02:00
|
|
|
|
2009-07-28 10:45:41 +02:00
|
|
|
import java.util.List
|
2009-10-06 00:07:27 +02:00
|
|
|
|
2010-01-02 08:40:09 +01:00
|
|
|
import se.scalablesolutions.akka.util.{HashCode, Logging}
|
2010-06-07 09:06:42 +02:00
|
|
|
import se.scalablesolutions.akka.actor.{Actor, ActorRef, ActorInitializationException}
|
2009-10-06 00:07:27 +02:00
|
|
|
|
2010-02-23 19:49:01 +01:00
|
|
|
import org.multiverse.commitbarriers.CountDownCommitBarrier
|
2010-08-26 16:31:41 +02:00
|
|
|
import se.scalablesolutions.akka.AkkaException
|
2010-08-27 12:12:33 +02:00
|
|
|
import java.util.concurrent.{ConcurrentSkipListSet}
|
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>
|
|
|
|
|
*/
|
2010-05-06 08:13:12 +02:00
|
|
|
final 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],
|
|
|
|
|
val senderFuture: Option[CompletableFuture[Any]],
|
2010-02-23 19:49:01 +01:00
|
|
|
val transactionSet: Option[CountDownCommitBarrier]) {
|
2009-12-27 08:24:11 +01:00
|
|
|
if (receiver eq null) throw new IllegalArgumentException("receiver is 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(
|
|
|
|
|
"Don't call 'self ! message' in the Actor's constructor (e.g. body of the class).")
|
|
|
|
|
}
|
2009-12-11 16:37:44 +01:00
|
|
|
|
|
|
|
|
def send = receiver.dispatcher.dispatch(this)
|
|
|
|
|
|
2009-10-06 00:07:27 +02:00
|
|
|
override def hashCode(): Int = synchronized {
|
2009-05-25 15:18:18 +02:00
|
|
|
var result = HashCode.SEED
|
2010-04-30 20:22:45 +02:00
|
|
|
result = HashCode.hash(result, receiver.actor)
|
2009-12-14 19:22:37 +01:00
|
|
|
result = HashCode.hash(result, message.asInstanceOf[AnyRef])
|
2009-05-25 15:18:18 +02:00
|
|
|
result
|
2009-05-25 14:48:43 +02:00
|
|
|
}
|
2009-05-25 15:18:18 +02:00
|
|
|
|
2009-10-06 00:07:27 +02:00
|
|
|
override def equals(that: Any): Boolean = synchronized {
|
2009-05-25 15:18:18 +02:00
|
|
|
that != null &&
|
2009-07-06 23:45:15 +02:00
|
|
|
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
|
|
|
}
|
2009-12-13 12:29:18 +01:00
|
|
|
|
2010-02-23 19:49:01 +01:00
|
|
|
override def toString = synchronized {
|
2009-12-08 16:17:22 +01:00
|
|
|
"MessageInvocation[" +
|
|
|
|
|
"\n\tmessage = " + message +
|
|
|
|
|
"\n\treceiver = " + receiver +
|
2010-05-19 10:09:30 +02:00
|
|
|
"\n\tsender = " + sender +
|
|
|
|
|
"\n\tsenderFuture = " + senderFuture +
|
2010-02-23 19:49:01 +01:00
|
|
|
"\n\ttransactionSet = " + transactionSet +
|
2010-07-13 12:37:11 +02:00
|
|
|
"]"
|
2009-10-06 00:07:27 +02:00
|
|
|
}
|
2009-05-25 14:48:43 +02:00
|
|
|
}
|
2009-12-13 12:29:18 +01:00
|
|
|
|
2010-08-26 16:31:41 +02:00
|
|
|
class MessageQueueAppendFailedException(message: String) extends AkkaException(message)
|
|
|
|
|
|
2010-06-01 18:10:48 +02:00
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2009-12-13 12:29:18 +01:00
|
|
|
trait MessageQueue {
|
|
|
|
|
def append(handle: MessageInvocation)
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-01 18:10:48 +02:00
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2010-01-02 08:40:09 +01:00
|
|
|
trait MessageDispatcher extends Logging {
|
2010-08-27 12:12:33 +02:00
|
|
|
protected val uuids = new ConcurrentSkipListSet[String]
|
2009-12-13 12:29:18 +01:00
|
|
|
def dispatch(invocation: MessageInvocation)
|
|
|
|
|
def start
|
|
|
|
|
def shutdown
|
2010-08-27 12:12:33 +02:00
|
|
|
def register(actorRef: ActorRef) = uuids add actorRef.uuid
|
2010-05-07 11:19:19 +02:00
|
|
|
def unregister(actorRef: ActorRef) = {
|
2010-08-27 12:12:33 +02:00
|
|
|
uuids remove actorRef.uuid
|
2010-06-01 09:27:14 +02:00
|
|
|
if (canBeShutDown) shutdown // shut down in the dispatcher's references is zero
|
2010-04-01 22:34:16 +02:00
|
|
|
}
|
2010-08-27 12:12:33 +02:00
|
|
|
def canBeShutDown: Boolean = uuids.isEmpty
|
2010-01-02 08:40:09 +01:00
|
|
|
def isShutdown: Boolean
|
2010-07-21 09:44:18 -04:00
|
|
|
def mailboxSize(actorRef: ActorRef):Int = 0
|
2009-12-13 12:29:18 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-01 18:10:48 +02:00
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2009-12-13 12:29:18 +01:00
|
|
|
trait MessageDemultiplexer {
|
|
|
|
|
def select
|
|
|
|
|
def wakeUp
|
|
|
|
|
def acquireSelectedInvocations: List[MessageInvocation]
|
|
|
|
|
def releaseSelectedInvocations
|
|
|
|
|
}
|