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

69 lines
2.2 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.actor.{Actor, ActorType, ActorRef, ActorInitializationException}
import akka.AkkaException
2010-09-21 18:52:41 +02:00
import java.util.{Queue, List}
import java.util.concurrent._
import concurrent.forkjoin.LinkedTransferQueue
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&#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
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(
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")
}
2010-10-29 16:33:31 +02:00
class DefaultUnboundedMessageQueue(blockDequeue: Boolean)
2010-09-21 18:52:41 +02:00
extends LinkedBlockingQueue[MessageInvocation] with MessageQueue {
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()
}
}
2010-10-29 16:33:31 +02:00
class DefaultBoundedMessageQueue(capacity: Int, pushTimeOut: Duration, blockDequeue: Boolean)
2010-09-21 18:52:41 +02:00
extends LinkedBlockingQueue[MessageInvocation](capacity) with MessageQueue {
final def enqueue(handle: MessageInvocation) {
if (pushTimeOut.toMillis > 0) {
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-01-20 17:16:44 +01:00
}