Interface ClusterEventService

All Known Subinterfaces:
ManagedClusterEventService
All Known Implementing Classes:
DefaultClusterEventService

public interface ClusterEventService
Publish-subscribe based messaging service.

This service is an abstraction for publish-subscribe based cluster communication. Messages are published and received based on arbitrary String topics. It supports several types of messaging:

  • broadcast(String, Object) broadcasts a message to all subscribers registered for the topic
  • #unicast(String, Object) sends a unicast message directly to one of the subscribers registered for the topic; unicast messages are generally delivered in round-robin fashion
  • #send(String, Object) sends a message directly to one of the subscribers registered for the topic and awaits a reply; direct messages are generally delivered in round-robin fashion
To register to listen for messages, use one of the subscribe(String, Consumer, Executor) methods:

 Subscription subscription = atomix.getEventService().subscribe("test", message -> {
   System.out.println("Received message");
 }, executor).join();

 
To cancel the subscription for a topic, call Subscription.close() on the returned Subscription object:

 subscription.close().join();

 
This API relies on CompletableFuture for asynchronous completion of all method calls.
  • Method Details

    • broadcast

      default <M> void broadcast(String topic, M message)
      Broadcasts a message to all subscribers registered for the given topic.
      Type Parameters:
      M - message type
      Parameters:
      topic - message topic
      message - message to send
    • broadcast

      <M> void broadcast(String topic, M message, Function<M,byte[]> encoder)
      Broadcasts a message to all subscribers registered for the given topic.
      Type Parameters:
      M - message type
      Parameters:
      topic - message topic
      message - message to send
      encoder - function for encoding message to byte[]
    • subscribe

      default <M, R> CompletableFuture<Subscription> subscribe(String topic, Function<M,R> handler, Executor executor)
      Adds a new subscriber for the specified message topic.
      Type Parameters:
      M - incoming message type
      R - reply message type
      Parameters:
      topic - message topic
      handler - handler function that processes the incoming message and produces a reply
      executor - executor to run this handler on
      Returns:
      future to be completed once the subscription has been propagated
    • subscribe

      <M, R> CompletableFuture<Subscription> subscribe(String topic, Function<byte[],M> decoder, Function<M,R> handler, Function<R,byte[]> encoder, Executor executor)
      Adds a new subscriber for the specified message topic.
      Type Parameters:
      M - incoming message type
      R - reply message type
      Parameters:
      topic - message topic
      decoder - decoder for resurrecting incoming message
      handler - handler function that processes the incoming message and produces a reply
      encoder - encoder for serializing reply
      executor - executor to run this handler on
      Returns:
      future to be completed once the subscription has been propagated
    • subscribe

      default <M, R> CompletableFuture<Subscription> subscribe(String topic, Function<M,CompletableFuture<R>> handler)
      Adds a new subscriber for the specified message topic.
      Type Parameters:
      M - incoming message type
      R - reply message type
      Parameters:
      topic - message topic
      handler - handler function that processes the incoming message and produces a reply
      Returns:
      future to be completed once the subscription has been propagated
    • subscribe

      <M, R> CompletableFuture<Subscription> subscribe(String topic, Function<byte[],M> decoder, Function<M,CompletableFuture<R>> handler, Function<R,byte[]> encoder)
      Adds a new subscriber for the specified message topic.
      Type Parameters:
      M - incoming message type
      R - reply message type
      Parameters:
      topic - message topic
      decoder - decoder for resurrecting incoming message
      handler - handler function that processes the incoming message and produces a reply
      encoder - encoder for serializing reply
      Returns:
      future to be completed once the subscription has been propagated
    • subscribe

      default <M> CompletableFuture<Subscription> subscribe(String topic, Consumer<M> handler, Executor executor)
      Adds a new subscriber for the specified message topic.
      Type Parameters:
      M - incoming message type
      Parameters:
      topic - message topic
      handler - handler for handling message
      executor - executor to run this handler on
      Returns:
      future to be completed once the subscription has been propagated
    • subscribe

      <M> CompletableFuture<Subscription> subscribe(String topic, Function<byte[],M> decoder, Consumer<M> handler, Executor executor)
      Adds a new subscriber for the specified message topic.
      Type Parameters:
      M - incoming message type
      Parameters:
      topic - message topic
      decoder - decoder to resurrecting incoming message
      handler - handler for handling message
      executor - executor to run this handler on
      Returns:
      future to be completed once the subscription has been propagated
    • getSubscriptions

      List<Subscription> getSubscriptions(String topic)
      Returns a list of subscriptions for the given topic.
      Parameters:
      topic - the topic for which to return subscriptions
      Returns:
      the subscriptions for the given topic
    • getSubscribers

      Set<MemberId> getSubscribers(String topic)
      Returns a list of remote members subscribed for the given topic.
      Parameters:
      topic - the topic for which to return subscriptions
      Returns:
      the subscribers for the given topic