Package io.atomix.cluster.messaging
Interface ClusterCommunicationService
- All Known Subinterfaces:
ManagedClusterCommunicationService
- All Known Implementing Classes:
DefaultClusterCommunicationService
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:
broadcast(String, Object, Function, boolean)broadcasts a message to all cluster membersmulticast(String, Object, Function, Set, boolean)sends the message to all provided membersunicast(String, Object, Function, MemberId, boolean)sends a unicast message directly to the given membersend(String, Object, Function, Function, MemberId, Duration)sends a message directly to the given member and awaits a reply
consume(String, Function, Consumer, Executor) methods:
service.consume("test", String::new, message -> {
System.out.println("Received message " + message);
}, executor);
-
Method Summary
Modifier and TypeMethodDescription<M> voidBroadcasts a message to all members.<M> voidconsume(String subject, Function<byte[], M> decoder, BiConsumer<MemberId, M> handler, Executor executor) Adds a new subscriber for the specified message subject which does not return any reply.<M> voidAdds a new subscriber for the specified message subject which does not return any reply.<M> voidmulticast(String subject, M message, Function<M, byte[]> encoder, Set<MemberId> memberIds, boolean reliable) Multicasts a message to a set of members.<M,R> void replyTo(String subject, Function<byte[], M> decoder, BiFunction<MemberId, M, R> handler, Function<R, byte[]> encoder, Executor executor) Adds a new subscriber for the specified message subject which must return a reply.<M,R> void replyTo(String subject, Function<byte[], M> decoder, Function<M, CompletableFuture<R>> handler, Function<R, byte[]> encoder) Adds a new subscriber for the specified message subject, which must return a reply.<M,R> void replyToAsync(String subject, Function<byte[], M> decoder, Function<M, CompletableFuture<R>> handler, Function<R, byte[]> encoder, Executor executor) Adds a new subscriber for the specified message subject which must return a reply.<M,R> CompletableFuture <R> send(String subject, M message, Function<M, byte[]> encoder, Function<byte[], R> decoder, MemberId toMemberId, Duration timeout) Sends a message and expects a reply.<M> voidSends a message to a member.voidunsubscribe(String subject) Removes a subscriber for the specified message subject.
-
Method Details
-
broadcast
Broadcasts a message to all members.- Type Parameters:
M- message type- Parameters:
subject- message subjectmessage- message to sendencoder- function for encoding message to byte[]reliable- whether to perform a reliable (TCP) unicast or not (UDP)
-
multicast
<M> void multicast(String subject, M message, 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 subjectmessage- message to sendencoder- function for encoding message to byte[]memberIds- recipient node identifiersreliable- whether to perform a reliable (TCP) unicast or not (UDP)
-
unicast
<M> void unicast(String subject, M message, Function<M, byte[]> encoder, MemberId memberId, boolean reliable) Sends a message to a member.- Type Parameters:
M- message type- Parameters:
subject- message subjectmessage- message to sendencoder- function for encoding message to byte[]memberId- recipient node identifierreliable- whether to perform a reliable (TCP) unicast or not (UDP)
-
send
<M,R> CompletableFuture<R> send(String subject, M message, Function<M, byte[]> encoder, Function<byte[], R> decoder, MemberId toMemberId, Duration timeout) Sends a message and expects a reply.The returned future may be completed exceptionally with any exceptions listed by
MessagingService.sendAndReceive(Address, String, byte[], boolean, Duration, Executor), as well as:MessagingException.NoSuchMemberException- indicates that the local membership protocol cannot resolve the given member ID to a node address
- Type Parameters:
M- request typeR- reply type- Parameters:
subject- message subjectmessage- message to sendencoder- function for encoding request to byte[]decoder- function for decoding response from byte[]toMemberId- recipient node identifiertimeout- response timeout- Returns:
- reply future
-
replyTo
<M,R> void replyTo(String subject, Function<byte[], M> decoder, Function<M, CompletableFuture<R>> handler, Function<R, byte[]> encoder) Adds a new subscriber for the specified message subject, which must return a reply.- Type Parameters:
M- incoming message typeR- reply message type- Parameters:
subject- message subjectdecoder- decoder for deserialize incoming messagehandler- handler function that processes the incoming message and produces a replyencoder- encoder for serializing reply
-
consume
<M> void consume(String subject, Function<byte[], M> decoder, Consumer<M> handler, Executor executor) Adds a new subscriber for the specified message subject which does not return any reply.- Type Parameters:
M- incoming message type- Parameters:
subject- message subjectdecoder- decoder to deserialize incoming messagehandler- handler for handling messageexecutor- executor to run this handler on
-
consume
<M> void consume(String subject, Function<byte[], M> decoder, BiConsumer<MemberId, M> handler, Executor executor) Adds a new subscriber for the specified message subject which does not return any reply. If the sender is not a known member, the handler is not called (but no error is returned to the sender).- Type Parameters:
M- incoming message type- Parameters:
subject- message subjectdecoder- decoder to deserialize incoming messagehandler- handler for handling message, receiving the sender's member ID and the decoded messageexecutor- executor to run this handler on
-
replyTo
<M,R> void replyTo(String subject, Function<byte[], M> decoder, BiFunction<MemberId, M, R> handler, Function<R, byte[]> encoder, Executor executor) Adds a new subscriber for the specified message subject which must return a reply. If the sender is not a known member, the handler is not called, and aMessagingException.NoSuchMemberExceptionis returned to the sender.- Type Parameters:
M- incoming message type- Parameters:
subject- message subjectdecoder- decoder to deserializing incoming messagehandler- handler for handling message, receiving the sender's member ID and the decoded messageencoder- to serialize the outgoing replyexecutor- executor to run this handler on
-
replyToAsync
<M,R> void replyToAsync(String subject, Function<byte[], M> decoder, Function<M, CompletableFuture<R>> handler, Function<R, byte[]> encoder, Executor executor) Adds a new subscriber for the specified message subject which must return a reply. If the sender is not a known member, the handler is not called, and aMessagingException.NoSuchMemberExceptionis returned to the sender.- Type Parameters:
M- incoming message type- Parameters:
subject- message subjectdecoder- decoder to deserializing incoming messagehandler- handler receives the decoded message and returns a future which is completed with the reply (which will be encoded using the given encoder)encoder- to serialize the outgoing replyexecutor- executor to run this handler on
-
unsubscribe
Removes a subscriber for the specified message subject.- Parameters:
subject- message subject
-