2010-09-21 18:52:41 +02:00
|
|
|
/**
|
2010-12-22 15:35:50 +01:00
|
|
|
* Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
|
2010-09-21 18:52:41 +02:00
|
|
|
*/
|
|
|
|
|
|
2010-10-26 12:49:25 +02:00
|
|
|
package akka.dispatch
|
2010-09-21 18:52:41 +02:00
|
|
|
|
2010-10-26 12:49:25 +02:00
|
|
|
import akka.actor.{Actor, ActorType, ActorRef, ActorInitializationException}
|
|
|
|
|
import akka.AkkaException
|
2010-09-21 18:52:41 +02:00
|
|
|
|
2011-03-11 14:51:24 +01:00
|
|
|
import java.util.{Queue, List, Comparator, PriorityQueue}
|
2010-09-21 18:52:41 +02:00
|
|
|
import java.util.concurrent._
|
2010-10-26 12:49:25 +02:00
|
|
|
import akka.util._
|
2010-09-21 18:52:41 +02:00
|
|
|
|
|
|
|
|
class MessageQueueAppendFailedException(message: String) extends AkkaException(message)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
|
|
|
|
trait MessageQueue {
|
|
|
|
|
val dispatcherLock = new SimpleLock
|
2011-02-14 02:34:40 +01:00
|
|
|
val suspended = new SimpleLock
|
2010-09-21 18:52:41 +02:00
|
|
|
def enqueue(handle: MessageInvocation)
|
|
|
|
|
def dequeue(): MessageInvocation
|
|
|
|
|
def size: Int
|
|
|
|
|
def isEmpty: Boolean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mailbox configuration.
|
|
|
|
|
*/
|
|
|
|
|
sealed trait MailboxType
|
|
|
|
|
|
2011-01-20 17:16:44 +01:00
|
|
|
case class UnboundedMailbox(val blocking: Boolean = false) extends MailboxType
|
2010-09-21 18:52:41 +02:00
|
|
|
case class BoundedMailbox(
|
2011-02-27 22:44:37 +01:00
|
|
|
val blocking: Boolean = false,
|
2010-09-21 18:52:41 +02:00
|
|
|
val capacity: Int = { if (Dispatchers.MAILBOX_CAPACITY < 0) Int.MaxValue else Dispatchers.MAILBOX_CAPACITY },
|
2011-01-20 17:16:44 +01:00
|
|
|
val pushTimeOut: Duration = Dispatchers.MAILBOX_PUSH_TIME_OUT) extends MailboxType {
|
2010-09-21 18:52:41 +02:00
|
|
|
if (capacity < 0) throw new IllegalArgumentException("The capacity for BoundedMailbox can not be negative")
|
|
|
|
|
if (pushTimeOut eq null) throw new IllegalArgumentException("The push time-out for BoundedMailbox can not be null")
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-09 18:11:45 +01:00
|
|
|
trait UnboundedMessageQueueSemantics extends MessageQueue { self: BlockingQueue[MessageInvocation] =>
|
|
|
|
|
def blockDequeue: Boolean
|
2010-10-29 16:33:31 +02:00
|
|
|
|
2010-09-21 18:52:41 +02:00
|
|
|
final def enqueue(handle: MessageInvocation) {
|
|
|
|
|
this add handle
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final def dequeue(): MessageInvocation = {
|
|
|
|
|
if (blockDequeue) this.take()
|
|
|
|
|
else this.poll()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-09 18:11:45 +01:00
|
|
|
trait BoundedMessageQueueSemantics extends MessageQueue { self: BlockingQueue[MessageInvocation] =>
|
|
|
|
|
def blockDequeue: Boolean
|
|
|
|
|
def pushTimeOut: Duration
|
2010-09-21 18:52:41 +02:00
|
|
|
|
|
|
|
|
final def enqueue(handle: MessageInvocation) {
|
2011-03-11 14:51:24 +01:00
|
|
|
if (pushTimeOut.length > 0 && pushTimeOut.toMillis > 0) {
|
2010-09-21 18:52:41 +02:00
|
|
|
if (!this.offer(handle, pushTimeOut.length, pushTimeOut.unit))
|
|
|
|
|
throw new MessageQueueAppendFailedException("Couldn't enqueue message " + handle + " to " + toString)
|
|
|
|
|
} else this put handle
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final def dequeue(): MessageInvocation =
|
|
|
|
|
if (blockDequeue) this.take()
|
|
|
|
|
else this.poll()
|
2011-03-09 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DefaultUnboundedMessageQueue(val blockDequeue: Boolean) extends
|
|
|
|
|
LinkedBlockingQueue[MessageInvocation] with
|
|
|
|
|
UnboundedMessageQueueSemantics
|
|
|
|
|
|
|
|
|
|
class DefaultBoundedMessageQueue(capacity: Int, val pushTimeOut: Duration, val blockDequeue: Boolean) extends
|
|
|
|
|
LinkedBlockingQueue[MessageInvocation](capacity) with
|
|
|
|
|
BoundedMessageQueueSemantics
|
|
|
|
|
|
|
|
|
|
class UnboundedPriorityMessageQueue(val blockDequeue: Boolean, cmp: Comparator[MessageInvocation]) extends
|
|
|
|
|
PriorityBlockingQueue[MessageInvocation](11, cmp) with
|
|
|
|
|
UnboundedMessageQueueSemantics
|
|
|
|
|
|
2011-03-11 16:48:44 +01:00
|
|
|
class BoundedPriorityMessageQueue(capacity: Int, val pushTimeOut: Duration, val blockDequeue: Boolean, cmp: Comparator[MessageInvocation]) extends
|
|
|
|
|
BoundedBlockingQueue[MessageInvocation](capacity, new PriorityQueue[MessageInvocation](11, cmp)) with
|
|
|
|
|
BoundedMessageQueueSemantics
|