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
|
|
|
|
|
|
|
|
import java.util.{Queue, List}
|
|
|
|
|
import java.util.concurrent._
|
|
|
|
|
import concurrent.forkjoin.LinkedTransferQueue
|
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
|
2010-10-11 16:11:51 +02:00
|
|
|
val suspended = new Switch(false)
|
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-01-20 17:16:44 +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")
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|