java.lang.Object
org.apache.pekko.actor.typed.pubsub.Topic

public class Topic extends Object
A pub sub topic is an actor that handles subscribing to a topic and publishing messages to all subscribed actors.

It is mostly useful in a clustered setting, where it is intended to be started once on every node that want to house subscribers or publish messages to the topic, but it also works in a local setting without cluster.

In a clustered context messages are deduplicated so that there is at most one message sent to each node for each publish and if there are no subscribers on a node, no message is sent to it. Note that the list of subscribers is eventually consistent and there are no delivery guarantees built in.

Each topic results in a pekko.actor.typed.receptionist.ServiceKey in the pekko.actor.typed.receptionist.Receptionist so the same scaling recommendation holds for topics, see docs: https://pekko.apache.org/docs/pekko/current/typed/actor-discovery.html#receptionist-scalability

  • Constructor Details

    • Topic

      public Topic()
  • Method Details

    • publish

      public static <T> Topic.Command<T> publish(T message)
      Java API: Publish the message to all currently known subscribers.
    • subscribe

      public static <T> Topic.Command<T> subscribe(ActorRef<T> subscriber)
      Java API: Subscribe to this topic. Should only be used for local subscribers.
    • getTopicStats

      public static <T> Topic.Command<T> getTopicStats(ActorRef<Topic.TopicStats> replyTo)
      Java API: Get a summary of the state for a local topic actor.

      See Topic.TopicStats for caveats

    • unsubscribe

      public static <T> Topic.Command<T> unsubscribe(ActorRef<T> subscriber)
      Java API: Unsubscribe a previously subscribed actor from this topic.
    • apply

      public static <T> Behavior<Topic.Command<T>> apply(String topicName, scala.reflect.ClassTag<T> classTag)
      Scala API: Create a topic actor behavior for the given topic name and message type.
    • create

      public static <T> Behavior<Topic.Command<T>> create(Class<T> messageClass, String topicName)
      Java API: Create a topic actor behavior for the given topic name and message class