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

73 lines
2.6 KiB
Scala
Raw Normal View History

2010-09-21 18:52:41 +02:00
/**
* Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
2010-09-21 18:52:41 +02:00
*/
package akka.dispatch
2010-09-21 18:52:41 +02:00
import akka.AkkaException
2010-09-21 18:52:41 +02:00
import java.util.{Comparator, PriorityQueue}
2010-09-21 18:52:41 +02:00
import java.util.concurrent._
import akka.util._
2010-09-21 18:52:41 +02:00
2011-04-29 17:15:00 +02:00
class MessageQueueAppendFailedException(message: String, cause: Throwable = null) extends AkkaException(message, cause)
2010-09-21 18:52:41 +02:00
/**
* @author <a href="http://jonasboner.com">Jonas Bon&#233;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
case class UnboundedMailbox() extends MailboxType
2010-09-21 18:52:41 +02:00
case class BoundedMailbox(
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")
}
trait UnboundedMessageQueueSemantics extends MessageQueue { self: BlockingQueue[MessageInvocation] =>
@inline final def enqueue(handle: MessageInvocation): Unit = this add handle
@inline final def dequeue(): MessageInvocation = this.poll()
2010-09-21 18:52:41 +02:00
}
trait BoundedMessageQueueSemantics extends MessageQueue { self: BlockingQueue[MessageInvocation] =>
def pushTimeOut: Duration
2010-09-21 18:52:41 +02:00
final def enqueue(handle: MessageInvocation) {
if (pushTimeOut.length > 0) {
this.offer(handle, pushTimeOut.length, pushTimeOut.unit) || {
throw new MessageQueueAppendFailedException("Couldn't enqueue message " + handle + " to " + toString) }
2010-09-21 18:52:41 +02:00
} else this put handle
}
@inline final def dequeue(): MessageInvocation = this.poll()
}
class DefaultUnboundedMessageQueue extends
LinkedBlockingQueue[MessageInvocation] with
UnboundedMessageQueueSemantics
class DefaultBoundedMessageQueue(capacity: Int, val pushTimeOut: Duration) extends
LinkedBlockingQueue[MessageInvocation](capacity) with
BoundedMessageQueueSemantics
class UnboundedPriorityMessageQueue(cmp: Comparator[MessageInvocation]) extends
PriorityBlockingQueue[MessageInvocation](11, cmp) with
UnboundedMessageQueueSemantics
class BoundedPriorityMessageQueue(capacity: Int, val pushTimeOut: Duration, cmp: Comparator[MessageInvocation]) extends
BoundedBlockingQueue[MessageInvocation](capacity, new PriorityQueue[MessageInvocation](11, cmp)) with
BoundedMessageQueueSemantics