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

194 lines
6.8 KiB
Scala
Raw Normal View History

/**
2009-12-27 16:01:53 +01:00
* Copyright (C) 2009-2010 Scalable Solutions AB <http://scalablesolutions.se>
*/
package se.scalablesolutions.akka.dispatch
import se.scalablesolutions.akka.actor.{ActorRef, IllegalActorStateException}
import java.util.Queue
2010-09-11 15:24:09 +02:00
import java.util.concurrent.{RejectedExecutionException, ConcurrentLinkedQueue, LinkedBlockingQueue}
/**
* Default settings are:
* <pre/>
* - withNewThreadPoolWithLinkedBlockingQueueWithUnboundedCapacity
* - NR_START_THREADS = 16
* - NR_MAX_THREADS = 128
* - KEEP_ALIVE_TIME = 60000L // one minute
* </pre>
* <p/>
*
* The dispatcher has a fluent builder interface to build up a thread pool to suite your use-case.
* There is a default thread pool defined but make use of the builder if you need it. Here are some examples.
* <p/>
*
* Scala API.
* <p/>
* Example usage:
* <pre/>
* val dispatcher = new ExecutorBasedEventDrivenDispatcher("name")
* dispatcher
* .withNewThreadPoolWithBoundedBlockingQueue(100)
* .setCorePoolSize(16)
* .setMaxPoolSize(128)
* .setKeepAliveTimeInMillis(60000)
* .setRejectionPolicy(new CallerRunsPolicy)
* .buildThreadPool
* </pre>
* <p/>
*
* Java API.
* <p/>
* Example usage:
* <pre/>
* ExecutorBasedEventDrivenDispatcher dispatcher = new ExecutorBasedEventDrivenDispatcher("name");
* dispatcher
* .withNewThreadPoolWithBoundedBlockingQueue(100)
* .setCorePoolSize(16)
* .setMaxPoolSize(128)
* .setKeepAliveTimeInMillis(60000)
* .setRejectionPolicy(new CallerRunsPolicy())
* .buildThreadPool();
* </pre>
* <p/>
*
* But the preferred way of creating dispatchers is to use
2010-03-07 09:44:03 +01:00
* the {@link se.scalablesolutions.akka.dispatch.Dispatchers} factory object.
*
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
* @param throughput positive integer indicates the dispatcher will only process so much messages at a time from the
* mailbox, without checking the mailboxes of other actors. Zero or negative means the dispatcher
* always continues until the mailbox is empty.
2010-06-30 16:26:15 +02:00
* Larger values (or zero or negative) increase througput, smaller values increase fairness
*/
class ExecutorBasedEventDrivenDispatcher(
2010-08-21 16:13:16 +02:00
_name: String,
2010-09-09 17:34:05 +02:00
val throughput: Int = Dispatchers.THROUGHPUT,
2010-09-21 18:52:41 +02:00
val throughputDeadlineTime: Int = Dispatchers.THROUGHPUT_DEADLINE_TIME_MILLIS,
_mailboxType: MailboxType = Dispatchers.MAILBOX_TYPE,
config: (ThreadPoolBuilder) => Unit = _ => ())
extends MessageDispatcher with ThreadPoolBuilder {
2010-09-21 18:52:41 +02:00
def this(_name: String, throughput: Int, throughputDeadlineTime: Int, mailboxType: MailboxType) =
this(_name, throughput, throughputDeadlineTime, mailboxType, _ => ()) // Needed for Java API usage
2010-09-04 12:00:12 +02:00
2010-09-21 18:52:41 +02:00
def this(_name: String, throughput: Int, mailboxType: MailboxType) =
this(_name, throughput, Dispatchers.THROUGHPUT_DEADLINE_TIME_MILLIS, mailboxType) // Needed for Java API usage
2010-08-21 16:13:16 +02:00
2010-09-21 18:52:41 +02:00
def this(_name: String, throughput: Int) =
this(_name, throughput, Dispatchers.THROUGHPUT_DEADLINE_TIME_MILLIS, Dispatchers.MAILBOX_TYPE) // Needed for Java API usage
def this(_name: String) =
this(_name, Dispatchers.THROUGHPUT, Dispatchers.THROUGHPUT_DEADLINE_TIME_MILLIS, Dispatchers.MAILBOX_TYPE) // Needed for Java API usage
val mailboxType = Some(_mailboxType)
@volatile private var active = false
val name = "akka:event-driven:dispatcher:" + _name
init
/**
2010-09-11 15:24:09 +02:00
* This is the behavior of an ExecutorBasedEventDrivenDispatchers mailbox
*/
2010-09-12 11:24:53 +02:00
trait ExecutableMailbox extends Runnable { self: MessageQueue =>
2010-09-12 15:36:32 +02:00
final def run = {
val reschedule = try {
processMailbox()
} finally {
dispatcherLock.unlock()
}
2010-09-21 18:52:41 +02:00
if (reschedule || !self.isEmpty) registerForExecution(self)
}
2010-09-21 18:52:41 +02:00
/**
* Process the messages in the mailbox
*
* @return true if the processing finished before the mailbox was empty, due to the throughput constraint
*/
final def processMailbox(): Boolean = {
var nextMessage = self.dequeue
if (nextMessage ne null) {
val throttle = throughput > 0
var processedMessages = 0
val isDeadlineEnabled = throttle && throughputDeadlineTime > 0
val started = if (isDeadlineEnabled) System.currentTimeMillis else 0
do {
nextMessage.invoke
2010-09-27 17:30:49 +02:00
if (nextMessage.receiver.isBeingRestarted)
return !self.isEmpty
2010-09-21 18:52:41 +02:00
if (throttle) { // Will be elided when false
processedMessages += 1
if ((processedMessages >= throughput) ||
(isDeadlineEnabled && (System.currentTimeMillis - started) >= throughputDeadlineTime)) // If we're throttled, break out
return !self.isEmpty
}
nextMessage = self.dequeue
} while (nextMessage ne null)
}
false
}
}
2010-09-09 17:34:05 +02:00
2010-09-11 15:24:09 +02:00
def dispatch(invocation: MessageInvocation) = {
val mbox = getMailbox(invocation.receiver)
mbox enqueue invocation
2010-09-12 11:24:53 +02:00
registerForExecution(mbox)
2010-09-11 15:24:09 +02:00
}
2010-09-27 11:59:28 +02:00
def registerForExecution(mailbox: MessageQueue with ExecutableMailbox): Unit = if (active) {
if (mailbox.dispatcherLock.tryLock()) {
try {
executor execute mailbox
} catch {
case e: RejectedExecutionException =>
mailbox.dispatcherLock.unlock()
throw e
}
}
2010-09-21 18:52:41 +02:00
} else log.warning("%s is shut down,\n\tignoring the rest of the messages in the mailbox of\n\t%s", toString, mailbox)
2010-09-11 15:24:09 +02:00
/**
* @return the mailbox associated with the actor
*/
2010-09-12 11:24:53 +02:00
private def getMailbox(receiver: ActorRef) = receiver.mailbox.asInstanceOf[MessageQueue with ExecutableMailbox]
2010-09-11 15:24:09 +02:00
override def mailboxSize(actorRef: ActorRef) = getMailbox(actorRef).size
2010-09-21 18:52:41 +02:00
def createTransientMailbox(actorRef: ActorRef, mailboxType: TransientMailboxType): AnyRef = mailboxType match {
case UnboundedMailbox(blocking) =>
new DefaultUnboundedMessageQueue(blocking) with ExecutableMailbox
case BoundedMailbox(blocking, capacity, pushTimeOut) =>
val cap = if (mailboxCapacity == -1) capacity else mailboxCapacity
new DefaultBoundedMessageQueue(cap, pushTimeOut, blocking) with ExecutableMailbox
2010-09-12 11:24:53 +02:00
}
2010-09-11 15:24:09 +02:00
def start = if (!active) {
log.debug("Starting up %s\n\twith throughput [%d]", toString, throughput)
active = true
}
def shutdown = if (active) {
log.debug("Shutting down %s", toString)
executor.shutdownNow
active = false
2010-08-27 12:12:33 +02:00
uuids.clear
}
2010-05-21 20:08:49 +02:00
2010-09-12 11:24:53 +02:00
def ensureNotActive(): Unit = if (active) {
throw new IllegalActorStateException(
"Can't build a new thread pool for a dispatcher that is already up and running")
2010-09-12 11:24:53 +02:00
}
2010-07-29 17:29:51 +02:00
override def toString = "ExecutorBasedEventDrivenDispatcher[" + name + "]"
// FIXME: should we have an unbounded queue and not bounded as default ????
2010-09-04 12:00:12 +02:00
private[akka] def init = {
withNewThreadPoolWithLinkedBlockingQueueWithUnboundedCapacity
config(this)
buildThreadPool
}
}