Adding support for safe mailboxes

This commit is contained in:
Viktor Klang 2010-09-07 18:32:50 +02:00
parent c2b85ee6e4
commit 928fa63411
6 changed files with 98 additions and 82 deletions

View file

@ -65,15 +65,15 @@ import java.util.concurrent.{ConcurrentLinkedQueue, LinkedBlockingQueue}
class ExecutorBasedEventDrivenDispatcher(
_name: String,
throughput: Int = Dispatchers.THROUGHPUT,
mailboxBounds: BoundedMailbox = Dispatchers.MAILBOX_BOUNDS,
mailboxConfig: MailboxConfig = Dispatchers.MAILBOX_CONFIG,
config: (ThreadPoolBuilder) => Unit = _ => ()) extends MessageDispatcher with ThreadPoolBuilder {
def this(_name: String, throughput: Int, capacity: Int) = this(_name,throughput,BoundedMailbox(capacity,None))
def this(_name: String, throughput: Int, capacity: Int) = this(_name,throughput,MailboxConfig(capacity,None,false))
def this(_name: String, throughput: Int) = this(_name, throughput, Dispatchers.MAILBOX_CAPACITY) // Needed for Java API usage
def this(_name: String) = this(_name,Dispatchers.THROUGHPUT,Dispatchers.MAILBOX_CAPACITY) // Needed for Java API usage
mailboxCapacity = mailboxBounds.capacity
mailboxCapacity = mailboxConfig.capacity
@volatile private var active: Boolean = false
@ -92,16 +92,7 @@ class ExecutorBasedEventDrivenDispatcher(
override def mailboxSize(actorRef: ActorRef) = getMailbox(actorRef).size
override def createMailbox(actorRef: ActorRef): AnyRef = {
if (mailboxCapacity <= 0)
new ConcurrentLinkedQueue[MessageInvocation]
else if (mailboxBounds.pushTimeOut.isDefined) {
val timeout = mailboxBounds.pushTimeOut.get
new BoundedTransferQueue[MessageInvocation](mailboxCapacity,timeout.length,timeout.unit)
}
else
new LinkedBlockingQueue[MessageInvocation](mailboxCapacity)
}
override def createMailbox(actorRef: ActorRef): AnyRef = mailboxConfig.newMailbox(bounds = mailboxCapacity)
def dispatch(receiver: ActorRef): Unit = if (active) {