Topic-Based Messaging with GridGain
Overview
GridGain distributed messaging enables topic-based cluster-wide communication between all nodes. Messages via a specified message topic can be distributed to all or sub-group of nodes that have subscribed to that topic.
GridGain messaging is based on the publish-subscribe paradigm where publishers and subscribers are tethered together with
a common topic. When one of the nodes sends a message A
for topic T
, it is published on all nodes that have subscribed to T
.
IgniteMessaging
Distributed messaging functionality in GridGain is available via the IgniteMessaging
interface. You can get an instance
of IgniteMessaging
like so:
Ignite ignite = Ignition.ignite();
// Messaging instance over this cluster.
IgniteMessaging msg = ignite.message();
// Messaging instance over given cluster group (in this case, remote nodes).
IgniteMessaging rmtMsg = ignite.message(ignite.cluster().forRemotes());
Publish Messages
Send methods help sending/publishing messages with a specified message topic to all nodes. Messages can be sent in ordered or unordered manner.
Ordered Messages
The sendOrdered(…)
method can be used if you want to receive messages in the order they were sent. The timeout parameter
is passed to specify how long a message will stay in the queue to wait for messages that are supposed to be sent before
this message. If the timeout expires, then all the messages that have not yet arrived for a given topic on that node will be ignored.
Unordered Messages
The send(…)
methods do not guarantee message ordering. This means that, when you sequentially send message A
and
message B
, you are not guaranteed that the target node first receives A
and then B
.
Subscribe for Messages
Listen methods help to listen/subscribe for messages. When these methods are called, a listener with specified message topic is registered on all (or sub-group of ) nodes to listen for new messages. With listen methods, a predicate is passed that returns a boolean value which tells the listener to continue or stop listening for new messages.
Local Listen
The localListen(…)
method registers a message listener with specified topic only on the local node and listens for
messages from any node in the given cluster group.
Remote Listen
The remoteListen(…)
method registers message listeners with specified topic on all nodes in the given cluster group
and listens for messages from any node in this cluster group.
Example
Ignite ignite = Ignition.ignite();
IgniteMessaging rmtMsg = ignite.message(ignite.cluster().forRemotes());
// Add listener for ordered messages on all remote nodes.
rmtMsg.remoteListen("MyOrderedTopic", (nodeId, msg) -> {
System.out.println("Received ordered message [msg=" + msg + ", from=" + nodeId + ']');
return true; // Return true to continue listening.
});
// Send ordered messages to remote nodes.
for (int i = 0; i < 10; i++)
rmtMsg.sendOrdered("MyOrderedTopic", Integer.toString(i),0);
© 2024 GridGain Systems, Inc. All Rights Reserved. Privacy Policy | Legal Notices. GridGain® is a registered trademark of GridGain Systems, Inc.
Apache, Apache Ignite, the Apache feather and the Apache Ignite logo are either registered trademarks or trademarks of The Apache Software Foundation.