Ignite distributed messaging allows for topic based cluster-wide communication between all nodes. Messages with a specified message topic can be distributed to all or sub-group of nodes that have subscribed to that topic.
Ignite messaging is based on publish-subscribe paradigm where publishers and subscribers are connected together by 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.
Any new node joining the cluster automatically gets subscribed to all the topics that other nodes in the cluster (or cluster group) are subscribed to.
Distributed Messaging functionality in Ignite is provided via 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());
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
sendOrdered(...) method can be used if you want to receive messages in the order they were sent. A 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
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.
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
localListen(...) method registers a message listener with specified topic only on the local node and listens for messages from any node in this cluster group.
Remote Listen
remoteListen(...) method registers message listeners with specified topic on all nodes in this cluster group and listens for messages from any node in this cluster group .
Following example shows message exchange between remote nodes.
Ignite ignite = Ignition.ignite();
IgniteMessaging rmtMsg = ignite.message(ignite.cluster().forRemotes());
// Add listener for unordered 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));
Ignite ignite = Ignition.ignite();
IgniteMessaging rmtMsg = ignite.message(ignite.cluster().forRemotes());
// Add listener for unordered messages on all remote nodes.
rmtMsg.remoteListen("MyUnOrderedTopic", (nodeId, msg) -> {
System.out.println("Received unordered message [msg=" + msg + ", from=" + nodeId + ']');
return true; // Return true to continue listening.
});
// Send unordered messages to remote nodes.
for (int i = 0; i < 10; i++)
rmtMsg.send("MyUnOrderedTopic", Integer.toString(i));
Ignite ignite = Ignition.ignite();
// Get cluster group of remote nodes.
ClusterGroup rmtPrj = ignite.cluster().forRemotes();
// Get messaging instance over remote nodes.
IgniteMessaging msg = ignite.message(rmtPrj);
// Add message listener for specified topic on all remote nodes.
msg.remoteListen("myOrderedTopic", new IgniteBiPredicate<UUID, String>() {
public boolean apply(UUID nodeId, String msg) {
System.out.println("Received ordered message [msg=" + msg + ", from=" + nodeId + ']');
return true; // Return true to continue listening.
}
});
// Send ordered messages to all remote nodes.
for (int i = 0; i < 10; i++)
msg.sendOrdered("myOrderedTopic", Integer.toString(i), 0);