Interface ClusterCommunicationService

All Known Subinterfaces:
ManagedClusterCommunicationService

public interface ClusterCommunicationService
High-level MemberId based intra-cluster messaging service.

The cluster communication service is used for high-level communication between cluster members. Messages are sent and received based on arbitrary String message subjects. Direct messages are sent using the MemberId to which to send the message. This API supports several types of messaging:

To register to listen for messages, use one of the subscribe(String, Consumer, Executor) methods:
   
   atomix.getCommunicationService().subscribe("test", message -> {
     System.out.println("Received message");
   }, executor);
   
 
  • Method Summary

    Modifier and Type Method Description
    default <M> void broadcast​(String subject, M message)
    Broadcasts a message to all members.
    default <M> void broadcast​(String subject, M message, boolean reliable)
    Broadcasts a message to all members.
    default <M> void broadcast​(String subject, M message, java.util.function.Function<M,​byte[]> encoder)
    Broadcasts a message to all members.
    <M> void broadcast​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, boolean reliable)
    Broadcasts a message to all members.
    default <M> void broadcastIncludeSelf​(String subject, M message)
    Broadcasts a message to all members over TCP including self.
    default <M> void broadcastIncludeSelf​(String subject, M message, boolean reliable)
    Broadcasts a message to all members including self.
    default <M> void broadcastIncludeSelf​(String subject, M message, java.util.function.Function<M,​byte[]> encoder)
    Broadcasts a message to all members over TCP including self.
    <M> void broadcastIncludeSelf​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, boolean reliable)
    Broadcasts a message to all members including self.
    default <M> void multicast​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, Set<MemberId> memberIds)
    Multicasts a message to a set of members over TCP.
    <M> void multicast​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, Set<MemberId> memberIds, boolean reliable)
    Multicasts a message to a set of members.
    default <M> void multicast​(String subject, M message, Set<MemberId> memberIds)
    Multicasts a message to a set of members over TCP.
    default <M> void multicast​(String subject, M message, Set<MemberId> memberIds, boolean reliable)
    Multicasts a message to a set of members.
    default <M,​ R> CompletableFuture<R> send​(String subject, M message, MemberId toMemberId)
    Sends a message and expects a reply.
    default <M,​ R> CompletableFuture<R> send​(String subject, M message, MemberId toMemberId, java.time.Duration timeout)
    Sends a message and expects a reply.
    default <M,​ R> CompletableFuture<R> send​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, java.util.function.Function<byte[],​R> decoder, MemberId toMemberId)
    Sends a message and expects a reply.
    <M,​ R> CompletableFuture<R> send​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, java.util.function.Function<byte[],​R> decoder, MemberId toMemberId, java.time.Duration timeout)
    Sends a message and expects a reply.
    default <M> CompletableFuture<Void> subscribe​(String subject, java.util.function.BiConsumer<MemberId,​M> handler, Executor executor)
    Adds a new subscriber for the specified message subject.
    default <M> CompletableFuture<Void> subscribe​(String subject, java.util.function.Consumer<M> handler, Executor executor)
    Adds a new subscriber for the specified message subject.
    <M> CompletableFuture<Void> subscribe​(String subject, java.util.function.Function<byte[],​M> decoder, java.util.function.BiConsumer<MemberId,​M> handler, Executor executor)
    Adds a new subscriber for the specified message subject.
    <M> CompletableFuture<Void> subscribe​(String subject, java.util.function.Function<byte[],​M> decoder, java.util.function.Consumer<M> handler, Executor executor)
    Adds a new subscriber for the specified message subject.
    <M,​ R> CompletableFuture<Void> subscribe​(String subject, java.util.function.Function<byte[],​M> decoder, java.util.function.Function<M,​CompletableFuture<R>> handler, java.util.function.Function<R,​byte[]> encoder)
    Adds a new subscriber for the specified message subject.
    <M,​ R> CompletableFuture<Void> subscribe​(String subject, java.util.function.Function<byte[],​M> decoder, java.util.function.Function<M,​R> handler, java.util.function.Function<R,​byte[]> encoder, Executor executor)
    Adds a new subscriber for the specified message subject.
    default <M,​ R> CompletableFuture<Void> subscribe​(String subject, java.util.function.Function<M,​CompletableFuture<R>> handler)
    Adds a new subscriber for the specified message subject.
    default <M,​ R> CompletableFuture<Void> subscribe​(String subject, java.util.function.Function<M,​R> handler, Executor executor)
    Adds a new subscriber for the specified message subject.
    default <M> CompletableFuture<Void> unicast​(String subject, M message, MemberId toMemberId)
    Sends a message to the specified member over TCP.
    default <M> CompletableFuture<Void> unicast​(String subject, M message, MemberId toMemberId, boolean reliable)
    Sends a message to the specified member.
    default <M> CompletableFuture<Void> unicast​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, MemberId toMemberId)
    Sends a message to the specified member over TCP.
    <M> CompletableFuture<Void> unicast​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, MemberId toMemberId, boolean reliable)
    Sends a message to the specified member.
    void unsubscribe​(String subject)
    Removes a subscriber for the specified message subject.
  • Method Details

    • broadcast

      default <M> void broadcast​(String subject, M message)
      Broadcasts a message to all members.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
    • broadcast

      default <M> void broadcast​(String subject, M message, boolean reliable)
      Broadcasts a message to all members.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
      reliable - whether to perform a reliable (TCP) unicast
    • broadcast

      default <M> void broadcast​(String subject, M message, java.util.function.Function<M,​byte[]> encoder)
      Broadcasts a message to all members.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
      encoder - function for encoding message to byte[]
    • broadcast

      <M> void broadcast​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, boolean reliable)
      Broadcasts a message to all members.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
      encoder - function for encoding message to byte[]
      reliable - whether to perform a reliable (TCP) unicast
    • broadcastIncludeSelf

      default <M> void broadcastIncludeSelf​(String subject, M message)
      Broadcasts a message to all members over TCP including self.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
    • broadcastIncludeSelf

      default <M> void broadcastIncludeSelf​(String subject, M message, boolean reliable)
      Broadcasts a message to all members including self.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
      reliable - whether to perform a reliable (TCP) unicast
    • broadcastIncludeSelf

      default <M> void broadcastIncludeSelf​(String subject, M message, java.util.function.Function<M,​byte[]> encoder)
      Broadcasts a message to all members over TCP including self.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
      encoder - function for encoding message to byte[]
    • broadcastIncludeSelf

      <M> void broadcastIncludeSelf​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, boolean reliable)
      Broadcasts a message to all members including self.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
      encoder - function for encoding message to byte[]
      reliable - whether to perform a reliable (TCP) unicast
    • unicast

      default <M> CompletableFuture<Void> unicast​(String subject, M message, MemberId toMemberId)
      Sends a message to the specified member over TCP.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
      toMemberId - destination node identifier
      Returns:
      future that is completed when the message is sent
    • unicast

      default <M> CompletableFuture<Void> unicast​(String subject, M message, MemberId toMemberId, boolean reliable)
      Sends a message to the specified member.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
      toMemberId - destination node identifier
      reliable - whether to perform a reliable (TCP) unicast
      Returns:
      future that is completed when the message is sent
    • unicast

      default <M> CompletableFuture<Void> unicast​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, MemberId toMemberId)
      Sends a message to the specified member over TCP.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
      encoder - function for encoding message to byte[]
      toMemberId - destination node identifier
      Returns:
      future that is completed when the message is sent
    • unicast

      <M> CompletableFuture<Void> unicast​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, MemberId toMemberId, boolean reliable)
      Sends a message to the specified member.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
      encoder - function for encoding message to byte[]
      toMemberId - destination node identifier
      reliable - whether to perform a reliable (TCP) unicast
      Returns:
      future that is completed when the message is sent
    • multicast

      default <M> void multicast​(String subject, M message, Set<MemberId> memberIds)
      Multicasts a message to a set of members over TCP.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
      memberIds - recipient node identifiers
    • multicast

      default <M> void multicast​(String subject, M message, Set<MemberId> memberIds, boolean reliable)
      Multicasts a message to a set of members.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
      memberIds - recipient node identifiers
      reliable - whether to perform a reliable (TCP) unicast
    • multicast

      default <M> void multicast​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, Set<MemberId> memberIds)
      Multicasts a message to a set of members over TCP.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
      encoder - function for encoding message to byte[]
      memberIds - recipient node identifiers
    • multicast

      <M> void multicast​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, Set<MemberId> memberIds, boolean reliable)
      Multicasts a message to a set of members.
      Type Parameters:
      M - message type
      Parameters:
      subject - message subject
      message - message to send
      encoder - function for encoding message to byte[]
      memberIds - recipient node identifiers
      reliable - whether to perform a reliable (TCP) unicast
    • send

      default <M,​ R> CompletableFuture<R> send​(String subject, M message, MemberId toMemberId)
      Sends a message and expects a reply.
      Type Parameters:
      M - request type
      R - reply type
      Parameters:
      subject - message subject
      message - message to send
      toMemberId - recipient node identifier
      Returns:
      reply future
    • send

      default <M,​ R> CompletableFuture<R> send​(String subject, M message, MemberId toMemberId, java.time.Duration timeout)
      Sends a message and expects a reply.
      Type Parameters:
      M - request type
      R - reply type
      Parameters:
      subject - message subject
      message - message to send
      toMemberId - recipient node identifier
      timeout - response timeout
      Returns:
      reply future
    • send

      default <M,​ R> CompletableFuture<R> send​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, java.util.function.Function<byte[],​R> decoder, MemberId toMemberId)
      Sends a message and expects a reply.
      Type Parameters:
      M - request type
      R - reply type
      Parameters:
      subject - message subject
      message - message to send
      encoder - function for encoding request to byte[]
      decoder - function for decoding response from byte[]
      toMemberId - recipient node identifier
      Returns:
      reply future
    • send

      <M,​ R> CompletableFuture<R> send​(String subject, M message, java.util.function.Function<M,​byte[]> encoder, java.util.function.Function<byte[],​R> decoder, MemberId toMemberId, java.time.Duration timeout)
      Sends a message and expects a reply.
      Type Parameters:
      M - request type
      R - reply type
      Parameters:
      subject - message subject
      message - message to send
      encoder - function for encoding request to byte[]
      decoder - function for decoding response from byte[]
      toMemberId - recipient node identifier
      timeout - response timeout
      Returns:
      reply future
    • subscribe

      default <M,​ R> CompletableFuture<Void> subscribe​(String subject, java.util.function.Function<M,​R> handler, Executor executor)
      Adds a new subscriber for the specified message subject.
      Type Parameters:
      M - incoming message type
      R - reply message type
      Parameters:
      subject - message subject
      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<Void> subscribe​(String subject, java.util.function.Function<byte[],​M> decoder, java.util.function.Function<M,​R> handler, java.util.function.Function<R,​byte[]> encoder, Executor executor)
      Adds a new subscriber for the specified message subject.
      Type Parameters:
      M - incoming message type
      R - reply message type
      Parameters:
      subject - message subject
      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<Void> subscribe​(String subject, java.util.function.Function<M,​CompletableFuture<R>> handler)
      Adds a new subscriber for the specified message subject.
      Type Parameters:
      M - incoming message type
      R - reply message type
      Parameters:
      subject - message subject
      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<Void> subscribe​(String subject, java.util.function.Function<byte[],​M> decoder, java.util.function.Function<M,​CompletableFuture<R>> handler, java.util.function.Function<R,​byte[]> encoder)
      Adds a new subscriber for the specified message subject.
      Type Parameters:
      M - incoming message type
      R - reply message type
      Parameters:
      subject - message subject
      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<Void> subscribe​(String subject, java.util.function.Consumer<M> handler, Executor executor)
      Adds a new subscriber for the specified message subject.
      Type Parameters:
      M - incoming message type
      Parameters:
      subject - message subject
      handler - handler for handling message
      executor - executor to run this handler on
      Returns:
      future to be completed once the subscription has been propagated
    • subscribe

      default <M> CompletableFuture<Void> subscribe​(String subject, java.util.function.BiConsumer<MemberId,​M> handler, Executor executor)
      Adds a new subscriber for the specified message subject.
      Type Parameters:
      M - incoming message type
      Parameters:
      subject - message subject
      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<Void> subscribe​(String subject, java.util.function.Function<byte[],​M> decoder, java.util.function.Consumer<M> handler, Executor executor)
      Adds a new subscriber for the specified message subject.
      Type Parameters:
      M - incoming message type
      Parameters:
      subject - message subject
      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
    • subscribe

      <M> CompletableFuture<Void> subscribe​(String subject, java.util.function.Function<byte[],​M> decoder, java.util.function.BiConsumer<MemberId,​M> handler, Executor executor)
      Adds a new subscriber for the specified message subject.
      Type Parameters:
      M - incoming message type
      Parameters:
      subject - message subject
      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
    • unsubscribe

      void unsubscribe​(String subject)
      Removes a subscriber for the specified message subject.
      Parameters:
      subject - message subject