In this part we learn how to use Ignite messaging & event system.
A node can subscribe to messages published on a topic from local or remote nodes.
Subscribing and publishing messages to a topic is as simple as:
import org.apache.ignite.*;
public class App {
public static void main(String[] args) {
Ignite ignite = Ignition.start();
ignite.message().remoteListen("my-topic", (nodeId, message) -> {
System.out.println("Received message: " + message);
return true; // keep on listening to new messages
});
ignite.message().send("my-topic", "Hello");
}
}
Complete TODOs in Step1_Messaging to fix all tests in Step1_MessagingTest.
A node can also subscribe to events triggered on local or remote nodes.
Events can be things such as: value put in cache, job execution end or failure, node leaving or joining cluster...
Subscribing to events is pretty easy:
import org.apache.ignite.*;
public class App {
public static void main(String[] args) {
Ignite ignite = Ignition.start();
ignite.events().remoteListen(
(nodeId, event) -> {
System.out.println("Received event: " + event);
return true; // keep on listening to new events
},
event -> event.type() == EventType.EVT_CACHE_OBJECT_PUT
);
}
}
Here put cache
events are filtered on remote nodes before being sent to listener.
/!\ A lot of events can be generated by application or internally by Ignite Kernel. A special care has to be taken when listening to events, and that's why they are disabled by default. For this hands on, only 2 events were enabled in Config class to illustrate event system.
Complete TODOs in Step2_Event to fix all tests in Step2_EventTest.