public interface MessageService extends MessageClient
Each member of a group has a reference to a message service through
which the member can receive messages from other nodes in the cluster. Messages can be sent to a member
of a group either directly through the GroupMember API or indirectly via the
group-wide MessageClient.
When messages are received by a MessageService instance, the consumer(s) listeners for the
associated message queue will be called. Clients can register multiple consumers for a single queue. If
multiple consumers are registered, a single consumer will be called for each message using a round-robin
pattern.
LocalMember localMember = group.join("foo").join();
MessageConsumer<String> consumer = localMember.messaging().consumer("bar");
consumer.onMessage(message -> {
message.ack();
});
Messages can be handled by consumers in several ways. The message service can be used to receive
and acknowledge tasks or to receive messages for which the producer is awaiting a reply. The behavior
expected of a consumer is dependent on the producer configuration. For example, if the producer expects
a reply then consumers should reply. Consumers must always call either Message.ack() or
Message.reply(Object) on each message to complete the processing of the message.
localMember.messaging().consumer("foo").onMessage(message -> {
message.reply("Hello world!");
});
| Modifier and Type | Method and Description |
|---|---|
default <T> MessageConsumer<T> |
consumer(String name)
Creates a new named message consumer.
|
<T> MessageConsumer<T> |
consumer(String name,
MessageConsumer.Options options)
Creates a new named message consumer.
|
producer, producerdefault <T> MessageConsumer<T> consumer(String name)
The returned consumer will consume messages sent to the service using a producer of the same name.
When a message is received by the member that owns this service, a single MessageConsumer
instance for the message will be called. If multiple consumers were created for the same queue on
the same MessageService instance, consumers will be called using a round-robin pattern.
LocalMember localMember = group.join("foo").join();
MessageConsumer<String> consumer = localMember.messaging().consumer("bar");
consumer.onMessage(message -> {
message.ack();
});
Messages can be handled by consumers in several ways. The message service can be used to receive
and acknowledge tasks or to receive messages for which the producer is awaiting a reply. The behavior
expected of a consumer is dependent on the producer configuration. For example, if the producer expects
a reply then consumers should reply. Consumers must always call either Message.ack() or
Message.reply(Object) on each message to complete the processing of the message.T - The message type.name - The consumer name.NullPointerException - if the consumer name is null<T> MessageConsumer<T> consumer(String name, MessageConsumer.Options options)
The returned consumer will consume messages sent to the service using a producer of the same name.
When a message is received by the member that owns this service, a single MessageConsumer
instance for the message will be called. If multiple consumers were created for the same queue on
the same MessageService instance, consumers will be called using a round-robin pattern.
LocalMember localMember = group.join("foo").join();
MessageConsumer<String> consumer = localMember.messaging().consumer("bar");
consumer.onMessage(message -> {
message.ack();
});
Messages can be handled by consumers in several ways. The message service can be used to receive
and acknowledge tasks or to receive messages for which the producer is awaiting a reply. The behavior
expected of a consumer is dependent on the producer configuration. For example, if the producer expects
a reply then consumers should reply. Consumers must always call either Message.ack() or
Message.reply(Object) on each message to complete the processing of the message.T - The message type.name - The consumer name.options - The consumer options.NullPointerException - if the consumer name is nullCopyright © 2013–2017. All rights reserved.