public class DistributedMessageBus extends AbstractResource
Transport.
The distributed message bus resource provides a simple interface for asynchronous messaging in an Atomix cluster.
Message buses handle management of location information and connections and use Atomix's underlying
Transport to communicate across the cluster.
To create a message bus resource, use the DistribtuedMessageBus class or constructor:
atomix.get("bus", DistributedMessageBus.class).thenAccept(bus -> {
...
});
Once a message bus instance has been created, it's not immediately opened. The message bus instance must be explicitly
opened by calling open(Address), providing an Address to which to bind the message bus server. Because
each message bus instance runs on a separate server, it's recommended that nodes use a singleton instance of this
resource by using io.atomix.Atomix#get(String, Class) rather than io.atomix.Atomix#create(String, Class)
to get a reference to the resource.
Messages are produced and consumed by producers and consumers respectively.
Each producer and consumer is associated with a string message bus topic.
bus.consumer("test", message -> {
return "world!";
});
bus.producer("test").send("Hello");
The distributed message bus does not provide reliability guarantees. Messaging is implemented directly on
top of the Transport layer with no additional coordination aside from managing
a distributed list of topics and their consumers.| Constructor and Description |
|---|
DistributedMessageBus(RaftClient client) |
| Modifier and Type | Method and Description |
|---|---|
CompletableFuture<Void> |
close()
Closes the message bus.
|
<T> CompletableFuture<MessageConsumer<T>> |
consumer(String topic)
Creates a message consumer.
|
<T> CompletableFuture<MessageConsumer<T>> |
consumer(String topic,
Function<T,?> consumer)
Creates a message consumer.
|
boolean |
isClosed()
Returns a boolean value indicating whether the message bus is closed.
|
boolean |
isOpen()
Returns a boolean value indicating whether the message bus is open.
|
CompletableFuture<DistributedMessageBus> |
open(Address address)
Opens the message bus.
|
<T> CompletableFuture<MessageProducer<T>> |
producer(String topic)
Creates a message producer.
|
DistributedMessageBus |
with(Consistency consistency) |
public DistributedMessageBus(RaftClient client)
public DistributedMessageBus with(Consistency consistency)
with in interface Resourcewith in class AbstractResourcepublic CompletableFuture<DistributedMessageBus> open(Address address)
When the message bus is opened, this instance will bind to the provided Address.
This method returns a CompletableFuture which can be used to block until the server is opened
or to be notified in a separate thread once the operation completes. To block until the operation completes,
use the CompletableFuture.join() method to block the calling thread:
bus.open(new Address("123.456.789.0", 5000)).join();
Alternatively, to execute the operation asynchronous and be notified once the lock is acquired in a different
thread, use one of the many completable future callbacks:
bus.open(new Address("123.456.789.0", 5000)).thenRun(() -> System.out.println("Message bus open!"));
address - The address on which to listen.public boolean isOpen()
public <T> CompletableFuture<MessageProducer<T>> producer(String topic)
The topic is a cluster-wide identifier. Messages will be distributed to MessageConsumers
registered for the given topic in round-robin order.
The producer will be created asynchronously. Once the returned CompletableFuture is
completed, the producer will be prepared to send messages.
T - The message type.topic - The topic to which to produce.public <T> CompletableFuture<MessageConsumer<T>> consumer(String topic)
The consumer will be created asynchronously. Once the consumer has been registered and all other
instances of the message bus across the cluster have been notified of the consumer, the returned
CompletableFuture will be completed.
T - The message type.topic - The topic from which to consume.public <T> CompletableFuture<MessageConsumer<T>> consumer(String topic, Function<T,?> consumer)
The consumer will be created asynchronously. Once the consumer has been registered and all other
instances of the message bus across the cluster have been notified of the consumer, the returned
CompletableFuture will be completed.
T - The message type.topic - The topic from which to consume.consumer - The message consumer.public CompletableFuture<Void> close()
IllegalStateException - if the message bus is not openpublic boolean isClosed()
Copyright © 2013–2015. All rights reserved.