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
|
|
|
|
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-09-07 18:32:50 +02:00
|
|
|
import java.util.{Queue, List}
|
|
|
|
|
import java.util.concurrent._
|
|
|
|
|
import concurrent.forkjoin.LinkedTransferQueue
|
2010-09-10 18:12:09 +02:00
|
|
|
import se.scalablesolutions.akka.util.{SimpleLock, Duration, HashCode, Logging}
|
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
|
|
|
|
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 {
|
2010-09-10 18:12:09 +02:00
|
|
|
val dispatcherLock = new SimpleLock
|
2010-09-07 18:32:50 +02:00
|
|
|
def enqueue(handle: MessageInvocation)
|
|
|
|
|
def dequeue(): MessageInvocation
|
2010-09-09 15:49:59 +02:00
|
|
|
def size: Int
|
|
|
|
|
def isEmpty: Boolean
|
2009-12-13 12:29:18 +01:00
|
|
|
}
|
|
|
|
|
|
2010-09-06 19:12:05 +02:00
|
|
|
/* Tells the dispatcher that it should create a bounded mailbox with the specified push timeout
|
2010-09-07 10:12:26 +02:00
|
|
|
* (If capacity > 0)
|
2010-09-06 19:12:05 +02:00
|
|
|
*/
|
2010-09-07 18:32:50 +02:00
|
|
|
case class MailboxConfig(capacity: Int, pushTimeOut: Option[Duration], blockingDequeue: Boolean) {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a MessageQueue (Mailbox) with the specified properties
|
|
|
|
|
* bounds = whether the mailbox should be bounded (< 0 means unbounded)
|
|
|
|
|
* pushTime = only used if bounded, indicates if and how long an enqueue should block
|
|
|
|
|
* blockDequeue = whether dequeues should block or not
|
|
|
|
|
*
|
|
|
|
|
* The bounds + pushTime generates a MessageQueueAppendFailedException if enqueue times out
|
|
|
|
|
*/
|
|
|
|
|
def newMailbox(bounds: Int = capacity,
|
|
|
|
|
pushTime: Option[Duration] = pushTimeOut,
|
2010-09-11 15:24:09 +02:00
|
|
|
blockDequeue: Boolean = blockingDequeue) : MessageQueue =
|
|
|
|
|
if (capacity > 0) new DefaultBoundedMessageQueue(bounds,pushTime,blockDequeue)
|
|
|
|
|
else new DefaultUnboundedMessageQueue(blockDequeue)
|
2010-09-10 18:12:09 +02:00
|
|
|
}
|
|
|
|
|
|
2010-09-11 15:24:09 +02:00
|
|
|
class DefaultUnboundedMessageQueue(blockDequeue: Boolean) extends LinkedBlockingQueue[MessageInvocation] with MessageQueue {
|
|
|
|
|
final def enqueue(handle: MessageInvocation) {
|
|
|
|
|
this add handle
|
2010-09-07 18:32:50 +02:00
|
|
|
}
|
2010-09-11 15:24:09 +02:00
|
|
|
|
|
|
|
|
final def dequeue(): MessageInvocation =
|
2010-09-10 18:12:09 +02:00
|
|
|
if (blockDequeue) this.take()
|
|
|
|
|
else this.poll()
|
2010-09-11 15:24:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DefaultBoundedMessageQueue(capacity: Int, pushTimeOut: Option[Duration], blockDequeue: Boolean) extends LinkedBlockingQueue[MessageInvocation](capacity) with MessageQueue {
|
|
|
|
|
final def enqueue(handle: MessageInvocation) {
|
|
|
|
|
if (pushTimeOut.isDefined) {
|
|
|
|
|
if(!this.offer(handle,pushTimeOut.get.length,pushTimeOut.get.unit))
|
|
|
|
|
throw new MessageQueueAppendFailedException("Couldn't enqueue message " + handle + " to " + toString)
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
this put handle
|
|
|
|
|
}
|
2010-09-10 18:12:09 +02:00
|
|
|
}
|
2010-09-11 15:24:09 +02:00
|
|
|
|
|
|
|
|
final def dequeue(): MessageInvocation =
|
|
|
|
|
if (blockDequeue) this.take()
|
|
|
|
|
else this.poll()
|
|
|
|
|
|
2010-09-07 18:32:50 +02:00
|
|
|
}
|
2010-09-06 19:12:05 +02:00
|
|
|
|
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
|
|
|
*/
|
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]
|
2010-09-01 16:33:56 +02:00
|
|
|
|
2009-12-13 12:29:18 +01:00
|
|
|
def dispatch(invocation: MessageInvocation)
|
2010-09-01 16:33:56 +02:00
|
|
|
|
2009-12-13 12:29:18 +01:00
|
|
|
def start
|
2010-09-01 16:33:56 +02:00
|
|
|
|
2009-12-13 12:29:18 +01:00
|
|
|
def shutdown
|
2010-09-01 16:33:56 +02:00
|
|
|
|
|
|
|
|
def register(actorRef: ActorRef) {
|
|
|
|
|
if(actorRef.mailbox eq null)
|
|
|
|
|
actorRef.mailbox = createMailbox(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-09-11 15:24:09 +02:00
|
|
|
actorRef.mailbox = null
|
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-09-01 16:33:56 +02:00
|
|
|
|
2010-08-27 12:12:33 +02:00
|
|
|
def canBeShutDown: Boolean = uuids.isEmpty
|
2010-09-01 16:33:56 +02:00
|
|
|
|
2010-01-02 08:40:09 +01:00
|
|
|
def isShutdown: Boolean
|
2010-09-01 16:33:56 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the size of the mailbox for the specified actor
|
|
|
|
|
*/
|
2010-09-03 11:43:58 +02:00
|
|
|
def mailboxSize(actorRef: ActorRef):Int
|
2010-09-01 16:33:56 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates and returns a mailbox for the given actor
|
|
|
|
|
*/
|
|
|
|
|
protected def createMailbox(actorRef: ActorRef): AnyRef = null
|
2010-09-10 18:12:09 +02:00
|
|
|
}
|