60 lines
1.8 KiB
Scala
60 lines
1.8 KiB
Scala
|
|
/**
|
||
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
||
|
|
*/
|
||
|
|
|
||
|
|
package docs.dispatcher
|
||
|
|
|
||
|
|
//#mailbox-implementation-example
|
||
|
|
import akka.actor.ActorRef
|
||
|
|
import akka.actor.ActorSystem
|
||
|
|
import akka.dispatch.Envelope
|
||
|
|
import akka.dispatch.MailboxType
|
||
|
|
import akka.dispatch.MessageQueue
|
||
|
|
import akka.dispatch.ProducesMessageQueue
|
||
|
|
import com.typesafe.config.Config
|
||
|
|
import java.util.concurrent.ConcurrentLinkedQueue
|
||
|
|
import scala.Option
|
||
|
|
|
||
|
|
// Marker trait used for mailbox requirements mapping
|
||
|
|
trait MyUnboundedMessageQueueSemantics
|
||
|
|
|
||
|
|
object MyUnboundedMailbox {
|
||
|
|
// This is the MessageQueue implementation
|
||
|
|
class MyMessageQueue extends MessageQueue
|
||
|
|
with MyUnboundedMessageQueueSemantics {
|
||
|
|
|
||
|
|
private final val queue = new ConcurrentLinkedQueue[Envelope]()
|
||
|
|
|
||
|
|
// these must be implemented; queue used as example
|
||
|
|
def enqueue(receiver: ActorRef, handle: Envelope): Unit =
|
||
|
|
queue.offer(handle)
|
||
|
|
def dequeue(): Envelope = queue.poll()
|
||
|
|
def numberOfMessages: Int = queue.size
|
||
|
|
def hasMessages: Boolean = !queue.isEmpty
|
||
|
|
def cleanUp(owner: ActorRef, deadLetters: MessageQueue) {
|
||
|
|
while (hasMessages) {
|
||
|
|
deadLetters.enqueue(owner, dequeue())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// This is the Mailbox implementation
|
||
|
|
class MyUnboundedMailbox extends MailboxType
|
||
|
|
with ProducesMessageQueue[MyUnboundedMailbox.MyMessageQueue] {
|
||
|
|
|
||
|
|
import MyUnboundedMailbox._
|
||
|
|
|
||
|
|
// This constructor signature must exist, it will be called by Akka
|
||
|
|
def this(settings: ActorSystem.Settings, config: Config) = {
|
||
|
|
// put your initialization code here
|
||
|
|
this()
|
||
|
|
}
|
||
|
|
|
||
|
|
// The create method is called to create the MessageQueue
|
||
|
|
final override def create(owner: Option[ActorRef],
|
||
|
|
system: Option[ActorSystem]): MessageQueue =
|
||
|
|
new MyMessageQueue()
|
||
|
|
}
|
||
|
|
//#mailbox-implementation-example
|