2009-12-11 16:37:44 +01:00
|
|
|
/**
|
2011-07-14 16:03:08 +02:00
|
|
|
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
2009-12-11 16:37:44 +01:00
|
|
|
*/
|
|
|
|
|
|
2010-10-26 12:49:25 +02:00
|
|
|
package akka.dispatch
|
2009-12-11 16:37:44 +01:00
|
|
|
|
2011-10-27 12:23:01 +02:00
|
|
|
import akka.event.Logging.Warning
|
2010-10-22 17:50:48 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicReference
|
2011-05-18 17:25:30 +02:00
|
|
|
import java.util.concurrent.{ TimeUnit, ExecutorService, RejectedExecutionException, ConcurrentLinkedQueue }
|
2011-09-20 15:43:57 +02:00
|
|
|
import akka.actor.{ ActorCell, ActorKilledException }
|
2011-11-10 20:08:00 +01:00
|
|
|
import akka.actor.ActorSystem
|
2011-11-14 16:03:26 +01:00
|
|
|
import akka.event.EventStream
|
|
|
|
|
import akka.actor.Scheduler
|
2011-11-21 10:48:21 +01:00
|
|
|
import akka.util.Duration
|
2010-05-27 15:50:11 +12:00
|
|
|
|
2009-12-11 16:37:44 +01:00
|
|
|
/**
|
|
|
|
|
* 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/>
|
2011-05-20 22:41:41 +02:00
|
|
|
* val dispatcher = new Dispatcher("name")
|
2009-12-11 16:37:44 +01:00
|
|
|
* dispatcher
|
|
|
|
|
* .withNewThreadPoolWithBoundedBlockingQueue(100)
|
|
|
|
|
* .setCorePoolSize(16)
|
|
|
|
|
* .setMaxPoolSize(128)
|
2011-11-22 13:04:10 +01:00
|
|
|
* .setKeepAliveTime(60 seconds)
|
2009-12-11 16:37:44 +01:00
|
|
|
* .buildThreadPool
|
|
|
|
|
* </pre>
|
|
|
|
|
* <p/>
|
|
|
|
|
*
|
|
|
|
|
* Java API.
|
|
|
|
|
* <p/>
|
|
|
|
|
* Example usage:
|
|
|
|
|
* <pre/>
|
2011-05-20 22:41:41 +02:00
|
|
|
* Dispatcher dispatcher = new Dispatcher("name");
|
2009-12-11 16:37:44 +01:00
|
|
|
* dispatcher
|
|
|
|
|
* .withNewThreadPoolWithBoundedBlockingQueue(100)
|
|
|
|
|
* .setCorePoolSize(16)
|
|
|
|
|
* .setMaxPoolSize(128)
|
2011-11-22 13:04:10 +01:00
|
|
|
* .setKeepAliveTime(60 seconds)
|
2009-12-11 16:37:44 +01:00
|
|
|
* .buildThreadPool();
|
|
|
|
|
* </pre>
|
|
|
|
|
* <p/>
|
|
|
|
|
*
|
|
|
|
|
* But the preferred way of creating dispatchers is to use
|
2010-10-26 12:49:25 +02:00
|
|
|
* the {@link akka.dispatch.Dispatchers} factory object.
|
2009-12-11 16:37:44 +01:00
|
|
|
*
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
2010-06-02 16:56:36 +02:00
|
|
|
* @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.
|
2011-04-23 08:11:31 +02:00
|
|
|
* Larger values (or zero or negative) increase throughput, smaller values increase fairness
|
2009-12-11 16:37:44 +01:00
|
|
|
*/
|
2011-05-20 22:41:41 +02:00
|
|
|
class Dispatcher(
|
2011-11-17 16:09:18 +01:00
|
|
|
_prerequisites: DispatcherPrerequisites,
|
2011-11-04 00:39:02 +01:00
|
|
|
val name: String,
|
2011-10-06 21:19:46 +02:00
|
|
|
val throughput: Int,
|
2011-11-21 10:48:21 +01:00
|
|
|
val throughputDeadlineTime: Duration,
|
2011-10-06 21:19:46 +02:00
|
|
|
val mailboxType: MailboxType,
|
|
|
|
|
executorServiceFactoryProvider: ExecutorServiceFactoryProvider,
|
2011-11-21 10:48:21 +01:00
|
|
|
val shutdownTimeout: Duration)
|
2011-11-17 16:09:18 +01:00
|
|
|
extends MessageDispatcher(_prerequisites) {
|
2010-08-21 15:36:50 +02:00
|
|
|
|
2011-08-30 15:50:52 +02:00
|
|
|
protected[akka] val executorServiceFactory = executorServiceFactoryProvider.createExecutorServiceFactory(name)
|
2011-11-14 19:19:44 +01:00
|
|
|
protected[akka] val executorService = new AtomicReference[ExecutorService](new ExecutorServiceDelegate {
|
|
|
|
|
lazy val executor = executorServiceFactory.createExecutorService
|
|
|
|
|
})
|
2010-03-04 21:22:16 +01:00
|
|
|
|
2011-10-19 13:19:44 +02:00
|
|
|
protected[akka] def dispatch(receiver: ActorCell, invocation: Envelope) = {
|
|
|
|
|
val mbox = receiver.mailbox
|
2011-11-12 10:57:28 +01:00
|
|
|
mbox.enqueue(receiver.self, invocation)
|
2011-09-26 19:52:49 +02:00
|
|
|
registerForExecution(mbox, true, false)
|
2011-09-20 18:34:21 +02:00
|
|
|
}
|
|
|
|
|
|
2011-10-18 16:44:35 +02:00
|
|
|
protected[akka] def systemDispatch(receiver: ActorCell, invocation: SystemMessage) = {
|
|
|
|
|
val mbox = receiver.mailbox
|
2011-11-12 10:57:28 +01:00
|
|
|
mbox.systemEnqueue(receiver.self, invocation)
|
2011-09-26 19:52:49 +02:00
|
|
|
registerForExecution(mbox, false, true)
|
2010-09-11 15:24:09 +02:00
|
|
|
}
|
|
|
|
|
|
2011-09-27 17:41:02 +02:00
|
|
|
protected[akka] def executeTask(invocation: TaskInvocation) {
|
2011-09-26 17:52:52 +02:00
|
|
|
try {
|
|
|
|
|
executorService.get() execute invocation
|
|
|
|
|
} catch {
|
2011-05-18 17:25:30 +02:00
|
|
|
case e: RejectedExecutionException ⇒
|
2011-11-16 15:54:14 +01:00
|
|
|
try {
|
|
|
|
|
executorService.get() execute invocation
|
|
|
|
|
} catch {
|
|
|
|
|
case e2: RejectedExecutionException ⇒
|
2011-11-18 11:59:43 +01:00
|
|
|
prerequisites.eventStream.publish(Warning("Dispatcher", e2.toString))
|
2011-11-16 15:54:14 +01:00
|
|
|
throw e2
|
|
|
|
|
}
|
2011-03-02 16:44:44 -07:00
|
|
|
}
|
|
|
|
|
}
|
2011-02-25 15:20:58 -07:00
|
|
|
|
2011-11-14 14:09:23 +01:00
|
|
|
protected[akka] def createMailbox(actor: ActorCell): Mailbox = mailboxType.create(actor)
|
2010-09-11 15:24:09 +02:00
|
|
|
|
2011-11-18 17:03:35 +01:00
|
|
|
protected[akka] def shutdown: Unit =
|
|
|
|
|
Option(executorService.getAndSet(new ExecutorServiceDelegate {
|
2011-11-14 19:19:44 +01:00
|
|
|
lazy val executor = executorServiceFactory.createExecutorService
|
2011-11-18 17:03:35 +01:00
|
|
|
})) foreach { _.shutdown() }
|
2010-07-29 17:29:51 +02:00
|
|
|
|
2011-09-23 09:33:53 +02:00
|
|
|
/**
|
|
|
|
|
* Returns if it was registered
|
|
|
|
|
*/
|
2011-09-23 13:14:17 +02:00
|
|
|
protected[akka] override def registerForExecution(mbox: Mailbox, hasMessageHint: Boolean, hasSystemMessageHint: Boolean): Boolean = {
|
2011-11-18 17:03:35 +01:00
|
|
|
if (mbox.canBeScheduledForExecution(hasMessageHint, hasSystemMessageHint)) { //This needs to be here to ensure thread safety and no races
|
2011-09-28 19:55:42 +02:00
|
|
|
if (mbox.setAsScheduled()) {
|
2011-02-27 23:44:04 +01:00
|
|
|
try {
|
|
|
|
|
executorService.get() execute mbox
|
2011-09-23 09:33:53 +02:00
|
|
|
true
|
2011-02-27 23:44:04 +01:00
|
|
|
} catch {
|
2011-11-14 19:48:06 +01:00
|
|
|
case e: RejectedExecutionException ⇒
|
|
|
|
|
try {
|
|
|
|
|
executorService.get() execute mbox
|
|
|
|
|
true
|
|
|
|
|
} catch { //Retry once
|
|
|
|
|
case e: RejectedExecutionException ⇒ mbox.setAsIdle(); throw e
|
|
|
|
|
}
|
2011-02-27 23:44:04 +01:00
|
|
|
}
|
2011-09-26 19:52:49 +02:00
|
|
|
} else false
|
2011-09-23 09:33:53 +02:00
|
|
|
} else false
|
2011-02-28 22:54:32 +01:00
|
|
|
}
|
2010-10-11 12:42:02 +02:00
|
|
|
|
|
|
|
|
override val toString = getClass.getSimpleName + "[" + name + "]"
|
2010-09-29 10:30:05 +02:00
|
|
|
}
|
|
|
|
|
|
2011-04-11 17:08:10 +02:00
|
|
|
object PriorityGenerator {
|
|
|
|
|
/**
|
|
|
|
|
* Creates a PriorityGenerator that uses the supplied function as priority generator
|
|
|
|
|
*/
|
2011-05-18 17:25:30 +02:00
|
|
|
def apply(priorityFunction: Any ⇒ Int): PriorityGenerator = new PriorityGenerator {
|
2011-04-11 17:08:10 +02:00
|
|
|
def gen(message: Any): Int = priorityFunction(message)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A PriorityGenerator is a convenience API to create a Comparator that orders the messages of a
|
2011-05-20 22:41:41 +02:00
|
|
|
* PriorityDispatcher
|
2011-04-11 17:08:10 +02:00
|
|
|
*/
|
2011-09-21 15:01:47 +02:00
|
|
|
abstract class PriorityGenerator extends java.util.Comparator[Envelope] {
|
2011-04-11 17:08:10 +02:00
|
|
|
def gen(message: Any): Int
|
|
|
|
|
|
2011-09-21 15:01:47 +02:00
|
|
|
final def compare(thisMessage: Envelope, thatMessage: Envelope): Int =
|
2011-04-11 17:08:10 +02:00
|
|
|
gen(thisMessage.message) - gen(thatMessage.message)
|
2011-10-28 15:55:15 +02:00
|
|
|
}
|