Package io.atomix.cluster.messaging
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-
sends a unicast message directly to one of the subscribers registered for the topic; unicast messages are generally delivered in round-robin fashion
invalid reference
#unicast(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
invalid reference
#send(String, Object)
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 Summary
Modifier and TypeMethodDescriptiondefault <M> voidBroadcasts a message to all subscribers registered for the giventopic.<M> voidBroadcasts a message to all subscribers registered for the giventopic.getSubscribers(String topic) Returns a list of remote members subscribed for the given topic.getSubscriptions(String topic) Returns a list of subscriptions for the given topic.default <M> CompletableFuture<Subscription> Adds a new subscriber for the specified message topic.Adds a new subscriber for the specified message topic.<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.<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.default <M,R> CompletableFuture <Subscription> subscribe(String topic, Function<M, CompletableFuture<R>> handler) Adds a new subscriber for the specified message topic.default <M,R> CompletableFuture <Subscription> Adds a new subscriber for the specified message topic.
-
Method Details
-
broadcast
Broadcasts a message to all subscribers registered for the giventopic.- Type Parameters:
M- message type- Parameters:
topic- message topicmessage- message to send
-
broadcast
Broadcasts a message to all subscribers registered for the giventopic.- Type Parameters:
M- message type- Parameters:
topic- message topicmessage- message to sendencoder- 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 typeR- reply message type- Parameters:
topic- message topichandler- handler function that processes the incoming message and produces a replyexecutor- 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 typeR- reply message type- Parameters:
topic- message topicdecoder- decoder for resurrecting incoming messagehandler- handler function that processes the incoming message and produces a replyencoder- encoder for serializing replyexecutor- 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 typeR- reply message type- Parameters:
topic- message topichandler- 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 typeR- reply message type- Parameters:
topic- message topicdecoder- decoder for resurrecting incoming messagehandler- handler function that processes the incoming message and produces a replyencoder- 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 topichandler- handler for handling messageexecutor- 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 topicdecoder- decoder to resurrecting incoming messagehandler- handler for handling messageexecutor- executor to run this handler on- Returns:
- future to be completed once the subscription has been propagated
-
getSubscriptions
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
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
-