public interface ClusterEventService
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 topicunicast(String, Object) sends a unicast message directly to one of the subscribers registered
for the topic; unicast messages are generally delivered in round-robin fashionsend(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 fashionsubscribe(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.| Modifier and Type | Method and Description |
|---|---|
default <M> void |
broadcast(String topic,
M message)
Broadcasts a message to all subscribers registered for the given
topic. |
<M> void |
broadcast(String topic,
M message,
Function<M,byte[]> encoder)
Broadcasts a message to all subscribers registered for the given
topic. |
List<Subscription> |
getSubscriptions(String topic)
Returns a list of subscriptions for the given topic.
|
default <M,R> CompletableFuture<R> |
send(String topic,
M message)
Sends a direct message to the next registered subscriber for
topic and awaits a reply. |
default <M,R> CompletableFuture<R> |
send(String topic,
M message,
Duration timeout)
Sends a direct message to the next registered subscriber for
topic and awaits a reply. |
default <M,R> CompletableFuture<R> |
send(String topic,
M message,
Function<M,byte[]> encoder,
Function<byte[],R> decoder)
Sends a direct message to the next registered subscriber for
topic and awaits a reply. |
<M,R> CompletableFuture<R> |
send(String topic,
M message,
Function<M,byte[]> encoder,
Function<byte[],R> decoder,
Duration timeout)
Sends a direct message to the next registered subscriber for
topic and awaits a reply. |
default <M> CompletableFuture<Subscription> |
subscribe(String topic,
Consumer<M> handler,
Executor executor)
Adds a new subscriber for the specified message topic.
|
<M> CompletableFuture<Subscription> |
subscribe(String topic,
Function<byte[],M> decoder,
Consumer<M> handler,
Executor executor)
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> |
subscribe(String topic,
Function<M,R> handler,
Executor executor)
Adds a new subscriber for the specified message topic.
|
default <M> CompletableFuture<Void> |
unicast(String topic,
M message)
Unicasts a message to the next registered subscriber for
topic. |
<M> CompletableFuture<Void> |
unicast(String topic,
M message,
Function<M,byte[]> encoder)
Unicasts a message to the next registered subscriber for
topic. |
default <M> void broadcast(String topic, M message)
topic.M - message typetopic - message topicmessage - message to send<M> void broadcast(String topic, M message, Function<M,byte[]> encoder)
topic.M - message typetopic - message topicmessage - message to sendencoder - function for encoding message to byte[]default <M> CompletableFuture<Void> unicast(String topic, M message)
topic.M - message typetopic - message topicmessage - message to send<M> CompletableFuture<Void> unicast(String topic, M message, Function<M,byte[]> encoder)
topic.M - message typemessage - message to sendtopic - message topicencoder - function for encoding message to byte[]default <M,R> CompletableFuture<R> send(String topic, M message)
topic and awaits a reply.M - request typeR - reply typetopic - message topicmessage - message to senddefault <M,R> CompletableFuture<R> send(String topic, M message, Duration timeout)
topic and awaits a reply.M - request typeR - reply typetopic - message topicmessage - message to sendtimeout - reply timeoutdefault <M,R> CompletableFuture<R> send(String topic, M message, Function<M,byte[]> encoder, Function<byte[],R> decoder)
topic and awaits a reply.M - request typeR - reply typetopic - message topicmessage - message to sendencoder - function for encoding request to byte[]decoder - function for decoding response from byte[]<M,R> CompletableFuture<R> send(String topic, M message, Function<M,byte[]> encoder, Function<byte[],R> decoder, Duration timeout)
topic and awaits a reply.M - request typeR - reply typetopic - message topicmessage - message to sendencoder - function for encoding request to byte[]decoder - function for decoding response from byte[]timeout - reply timeoutdefault <M,R> CompletableFuture<Subscription> subscribe(String topic, Function<M,R> handler, Executor executor)
M - incoming message typeR - reply message typetopic - message topichandler - handler function that processes the incoming message and produces a replyexecutor - executor to run this handler on<M,R> CompletableFuture<Subscription> subscribe(String topic, Function<byte[],M> decoder, Function<M,R> handler, Function<R,byte[]> encoder, Executor executor)
M - incoming message typeR - reply message typetopic - 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 ondefault <M,R> CompletableFuture<Subscription> subscribe(String topic, Function<M,CompletableFuture<R>> handler)
M - incoming message typeR - reply message typetopic - message topichandler - handler function that processes the incoming message and produces a reply<M,R> CompletableFuture<Subscription> subscribe(String topic, Function<byte[],M> decoder, Function<M,CompletableFuture<R>> handler, Function<R,byte[]> encoder)
M - incoming message typeR - reply message typetopic - message topicdecoder - decoder for resurrecting incoming messagehandler - handler function that processes the incoming message and produces a replyencoder - encoder for serializing replydefault <M> CompletableFuture<Subscription> subscribe(String topic, Consumer<M> handler, Executor executor)
M - incoming message typetopic - message topichandler - handler for handling messageexecutor - executor to run this handler on<M> CompletableFuture<Subscription> subscribe(String topic, Function<byte[],M> decoder, Consumer<M> handler, Executor executor)
M - incoming message typetopic - message topicdecoder - decoder to resurrecting incoming messagehandler - handler for handling messageexecutor - executor to run this handler onList<Subscription> getSubscriptions(String topic)
topic - the topic for which to return subscriptionsCopyright © 2013–2018. All rights reserved.