Mailboxes

You are viewing the documentation for the new actor APIs, to view the Pekko Classic documentation, see Classic Mailboxes.

Dependency

Mailboxes are part of core Pekko, which means that they are part of the pekko-actor dependency. This page describes how to use mailboxes with pekko-actor-typed, which has dependency:

sbt
val PekkoVersion = "1.1.1"
libraryDependencies += "org.apache.pekko" %% "pekko-actor-typed" % PekkoVersion
Maven
<properties>
  <scala.binary.version>2.13</scala.binary.version>
</properties>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.apache.pekko</groupId>
      <artifactId>pekko-bom_${scala.binary.version}</artifactId>
      <version>1.1.1</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
<dependencies>
  <dependency>
    <groupId>org.apache.pekko</groupId>
    <artifactId>pekko-actor-typed_${scala.binary.version}</artifactId>
  </dependency>
</dependencies>
Gradle
def versions = [
  ScalaBinary: "2.13"
]
dependencies {
  implementation platform("org.apache.pekko:pekko-bom_${versions.ScalaBinary}:1.1.1")

  implementation "org.apache.pekko:pekko-actor-typed_${versions.ScalaBinary}"
}

Introduction

Each actor in Pekko has a Mailbox, this is where the messages are enqueued before being processed by the actor.

By default an unbounded mailbox is used, this means any number of messages can be enqueued into the mailbox.

The unbounded mailbox is a convenient default but in a scenario where messages are added to the mailbox faster than the actor can process them, this can lead to the application running out of memory. For this reason a bounded mailbox can be specified, the bounded mailbox will pass new messages to deadletters when the mailbox is full.

For advanced use cases it is also possible to defer mailbox selection to config by pointing to a config path.

Selecting what mailbox is used

Selecting a Mailbox Type for an Actor

To select a specific mailbox for an actor use MailboxSelectorMailboxSelector to create a PropsProps instance for spawning your actor:

Scala
sourcecontext.spawn(childBehavior, "bounded-mailbox-child", MailboxSelector.bounded(100))

val props = MailboxSelector.fromConfig("my-app.my-special-mailbox")
context.spawn(childBehavior, "from-config-mailbox-child", props)
Java
sourcecontext.spawn(childBehavior, "bounded-mailbox-child", MailboxSelector.bounded(100));

context.spawn(
    childBehavior,
    "from-config-mailbox-child",
    MailboxSelector.fromConfig("my-app.my-special-mailbox"));

fromConfigfromConfig takes an absolute config path to a block defining the dispatcher in the config file:

source# SPDX-License-Identifier: Apache-2.0

my-app {
  my-special-mailbox {
    mailbox-type = "org.apache.pekko.dispatch.SingleConsumerOnlyUnboundedMailbox"
  }
}

Default Mailbox

The default mailbox is used when the mailbox is not specified and is the SingleConsumerOnlyUnboundedMailboxSingleConsumerOnlyUnboundedMailbox

Which Configuration is passed to the Mailbox Type

Each mailbox type is implemented by a class which extends MailboxTypeMailboxType and takes two constructor arguments: a ActorSystem.SettingsActorSystem.Settings object and a Config section. The latter is computed by obtaining the named configuration section from the ActorSystemActorSystem configuration, overriding its id key with the configuration path of the mailbox type and adding a fall-back to the default mailbox configuration section.

Interoperability with DispatcherSelector

The MailboxSelectorMailboxSelector will create a PropsProps instance that can be both set up Dispatcher and Mailbox, which means that you can continue to set up Dispatcher through chain calls.

Scala
sourcecontext.spawn(childBehavior, "bounded-mailbox-child", MailboxSelector.bounded(100).withDispatcherDefault)

val props =
  MailboxSelector.fromConfig("my-app.my-special-mailbox").withDispatcherFromConfig(
    Dispatchers.DefaultDispatcherId)
context.spawn(childBehavior, "from-config-mailbox-child", props)
Java
sourcecontext.spawn(
    childBehavior,
    "bounded-mailbox-child",
    MailboxSelector.bounded(100).withDispatcherDefault());

context.spawn(
    childBehavior,
    "from-config-mailbox-child",
    MailboxSelector.fromConfig("my-app.my-special-mailbox")
        .withDispatcherFromConfig(Dispatchers.DefaultDispatcherId()));

Mailbox Implementations

Pekko ships with a number of mailbox implementations:

Other bounded mailbox implementations which will block the sender if the capacity is reached and configured with non-zero mailbox-push-timeout-time.

Note

The following mailboxes should only be used with zero mailbox-push-timeout-time.

Custom Mailbox type

The best way to show how to create your own Mailbox type is by example:

Scala
source// Marker trait used for mailbox requirements mapping
trait MyUnboundedMessageQueueSemantics
Java
source// Marker interface used for mailbox requirements mapping
public interface MyUnboundedMessageQueueSemantics {}
Scala
sourceimport org.apache.pekko
import pekko.actor.ActorRef
import pekko.actor.ActorSystem
import pekko.dispatch.Envelope
import pekko.dispatch.MailboxType
import pekko.dispatch.MessageQueue
import pekko.dispatch.ProducesMessageQueue
import com.typesafe.config.Config
import java.util.concurrent.ConcurrentLinkedQueue
import scala.Option

object MyUnboundedMailbox {
  // This is the MessageQueue implementation
  class MyMessageQueue extends MessageQueue with MyUnboundedMessageQueueSemantics {

    private final val queue = new ConcurrentLinkedQueue[Envelope]()

    // these should 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): Unit = {
      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 Pekko
  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()
}
Java
sourceimport org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.dispatch.Envelope;
import org.apache.pekko.dispatch.MailboxType;
import org.apache.pekko.dispatch.MessageQueue;
import org.apache.pekko.dispatch.ProducesMessageQueue;
import com.typesafe.config.Config;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.Queue;
import scala.Option;

public class MyUnboundedMailbox
    implements MailboxType, ProducesMessageQueue<MyUnboundedMailbox.MyMessageQueue> {

  // This is the MessageQueue implementation
  public static class MyMessageQueue implements MessageQueue, MyUnboundedMessageQueueSemantics {
    private final Queue<Envelope> queue = new ConcurrentLinkedQueue<Envelope>();

    // these must be implemented; queue used as example
    public void enqueue(ActorRef receiver, Envelope handle) {
      queue.offer(handle);
    }

    public Envelope dequeue() {
      return queue.poll();
    }

    public int numberOfMessages() {
      return queue.size();
    }

    public boolean hasMessages() {
      return !queue.isEmpty();
    }

    public void cleanUp(ActorRef owner, MessageQueue deadLetters) {
      while (!queue.isEmpty()) {
        deadLetters.enqueue(owner, dequeue());
      }
    }
  }

  // This constructor signature must exist, it will be called by Pekko
  public MyUnboundedMailbox(ActorSystem.Settings settings, Config config) {
    // put your initialization code here
  }

  // The create method is called to create the MessageQueue
  public MessageQueue create(Option<ActorRef> owner, Option<ActorSystem> system) {
    return new MyMessageQueue();
  }
}

And then you specify the FQCN of your MailboxType as the value of the “mailbox-type” in the dispatcher configuration, or the mailbox configuration.

Note

Make sure to include a constructor which takes actor.ActorSystem.Settingsactor.ActorSystem.Settings and com.typesafe.config.Config arguments, as this constructor is invoked reflectively to construct your mailbox type. The config passed in as second argument is that section from the configuration which describes the dispatcher or mailbox setting using this mailbox type; the mailbox type will be instantiated once for each dispatcher or mailbox setting using it.