pekko/akka-actors/src/main/scala/dispatch/Reactor.scala

79 lines
2.1 KiB
Scala
Raw Normal View History

/**
* Copyright (C) 2009 Scalable Solutions.
*/
package se.scalablesolutions.akka.dispatch
import java.util.List
2009-10-06 00:07:27 +02:00
import se.scalablesolutions.akka.util.HashCode
import se.scalablesolutions.akka.stm.Transaction
import se.scalablesolutions.akka.actor.Actor
import java.util.concurrent.atomic.AtomicInteger
trait MessageQueue {
def append(handle: MessageInvocation)
def prepend(handle: MessageInvocation)
}
trait MessageInvoker {
def invoke(message: MessageInvocation)
}
trait MessageDispatcher {
2009-12-11 06:44:59 +01:00
def dispatch(invocation: MessageInvocation)
def registerHandler(key: AnyRef, handler: MessageInvoker)
def unregisterHandler(key: AnyRef)
def canBeShutDown: Boolean
def start
def shutdown
}
trait MessageDemultiplexer {
def select
def acquireSelectedInvocations: List[MessageInvocation]
def releaseSelectedInvocations
def wakeUp
}
2009-10-06 00:07:27 +02:00
class MessageInvocation(val receiver: Actor,
val message: Any,
val future: Option[CompletableFutureResult],
val sender: Option[Actor],
val tx: Option[Transaction]) {
2009-10-06 00:07:27 +02:00
if (receiver == null) throw new IllegalArgumentException("receiver is null")
if (message == null) throw new IllegalArgumentException("message is null")
2009-10-06 00:07:27 +02:00
private [akka] val nrOfDeliveryAttempts = new AtomicInteger(0)
def send = synchronized {
2009-12-11 06:44:59 +01:00
receiver.dispatcher.dispatch(this)
2009-10-06 00:07:27 +02:00
nrOfDeliveryAttempts.incrementAndGet
}
override def hashCode(): Int = synchronized {
var result = HashCode.SEED
2009-10-06 00:07:27 +02:00
result = HashCode.hash(result, receiver)
result = HashCode.hash(result, message)
result
}
2009-10-06 00:07:27 +02:00
override def equals(that: Any): Boolean = synchronized {
that != null &&
that.isInstanceOf[MessageInvocation] &&
2009-10-06 00:07:27 +02:00
that.asInstanceOf[MessageInvocation].receiver == receiver &&
2009-07-28 17:48:11 +02:00
that.asInstanceOf[MessageInvocation].message == message
2009-10-06 00:07:27 +02:00
}
override def toString(): String = synchronized {
2009-12-08 16:17:22 +01:00
"MessageInvocation[" +
"\n\tmessage = " + message +
"\n\treceiver = " + receiver +
"\n\tsender = " + sender +
"\n\tfuture = " + future +
"\n\ttx = " + tx +
"\n]"
2009-10-06 00:07:27 +02:00
}
}