Apache Ignite

GridGain Developer Hub - Apache Ignitetm

Welcome to the Apache Ignite developer hub run by GridGain. Here you'll find comprehensive guides and documentation to help you start working with Apache Ignite as quickly as possible, as well as support if you get stuck.

 

GridGain also provides Community Edition which is a distribution of Apache Ignite made available by GridGain. It is the fastest and easiest way to get started with Apache Ignite. The Community Edition is generally more stable than the Apache Ignite release available from the Apache Ignite website and may contain extra bug fixes and features that have not made it yet into the release on the Apache website.

 

Let's jump right in!

 

Documentation     Ask a Question     Download

 

Javadoc     Scaladoc     Examples

Local and Remote Events

Get notified of any state change or event occurring cluster-wide.

Overview

Ignite distributed events functionality allows applications to receive notifications when a variety of events occur in the distributed grid environment. You can automatically get notified for task executions, read, write or query operations occurring on local or remote nodes within the cluster.

IgniteMessaging

Distributed events functionality is provided via IgniteEvents interface. You can get an instance of IgniteEvents from Ignite as follows:

Ignite ignite = Ignition.ignite();

IgniteEvents evts = ignite.events();

Subscribe for Events

Listen methods can be used to receive notification for specified events happening in the cluster. These methods register a listener on local or remotes nodes for the specified events. Whenever the event occurs on the node, the listener is notified.

Local Events

localListen(...) method registers event listeners with specified events on local node only.

Remote Events

remoteListen(...) method registers event listeners with specified events on all nodes within the cluster or cluster group. Following is an example of each method:

Ignite ignite = Ignition.ignite();

// Local listener that listenes to local events.
IgnitePredicate<CacheEvent> locLsnr = evt -> {
  System.out.println("Received event [evt=" + evt.name() + ", key=" + evt.key() + 
    ", oldVal=" + evt.oldValue() + ", newVal=" + evt.newValue());

  return true; // Continue listening.
};

// Subscribe to specified cache events occuring on local node.
ignite.events().localListen(locLsnr,
  EventType.EVT_CACHE_OBJECT_PUT,
  EventType.EVT_CACHE_OBJECT_READ,
  EventType.EVT_CACHE_OBJECT_REMOVED);

// Get an instance of named cache.
final IgniteCache<Integer, String> cache = ignite.cache("cacheName");

// Generate cache events.
for (int i = 0; i < 20; i++)
  cache.put(i, Integer.toString(i));
Ignite ignite = Ignition.ignite();

// Get an instance of named cache.
final IgniteCache<Integer, String> cache = ignite.jcache("cacheName");

// Sample remote filter which only accepts events for keys
// that are greater than or equal to 10.
IgnitePredicate<CacheEvent> rmtLsnr = evt -> evt.<Integer>key() >= 10;

// Subscribe to specified cache events on all nodes that have cache running.
ignite.events(ignite.cluster().forCacheNodes("cacheName")).remoteListen(null, rmtLsnr,                                                                 EventType.EVT_CACHE_OBJECT_PUT,
  EventType.EVT_CACHE_OBJECT_READ,
  EventType.EVT_CACHE_OBJECT_REMOVED);

// Generate cache events.
for (int i = 0; i < 20; i++)
  cache.put(i, Integer.toString(i));
Ignite ignite = Ignition.ignite();
 
// Get an instance of named cache.
final IgniteCache<Integer, String> cache = ignite.jcache("cacheName");
 
// Sample remote filter which only accepts events for keys
// that are greater than or equal to 10.
IgnitePredicate<CacheEvent> rmtLsnr = new IgnitePredicate<CacheEvent>() {
    @Override public boolean apply(CacheEvent evt) {
        System.out.println("Cache event: " + evt);
 
        int key = evt.key();
 
        return key >= 10;
    }
};
 
// Subscribe to specified cache events occuring on 
// all nodes that have the specified cache running.
ignite.events(ignite.cluster().forCacheNodes("cacheName")).remoteListen(null, rmtLsnr,                                                                 EVT_CACHE_OBJECT_PUT,                                      		    		   EVT_CACHE_OBJECT_READ,                                                     EVT_CACHE_OBJECT_REMOVED);
 
// Generate cache events.
for (int i = 0; i < 20; i++)
    cache.put(i, Integer.toString(i));

In the above example EVT_CACHE_OBJECT_PUT,EVT_CACHE_OBJECT_READ, and EVT_CACHE_OBJECT_REMOVED are pre-defined event type constants defined in EventType interface.

EventType interface defines various event type constants that can be used with listen methods. Refer to javadoc for complete list of these event types.

Event types passed in as parameter in localListen(...) and remoteListen(...) methods must also be configured in IgniteConfiguration. See configuration example below.

Query for Events

All events generated in the system are kept locally on the local node. IgniteEvents API provides methods to query for these events.

Local Query

localQuery(...) method queries for events on the local node using the passed in predicate filter. If all predicates are satisfied, a collection of events happening on the local node is returned.

Remote Query

remoteQuery(...) method asynchronously queries for events on remote nodes in this projection using the passed in predicate filter. This operation is distributed and hence can fail on communication layer and generally can take much longer than local event notifications. Note that this method will not block and will return immediately with future.

Configuration

To get notified of any tasks or cache events occurring within the cluster, includeEventTypes property of IgniteConfiguration must be enabled.

<bean class="org.apache.ignite.configuration.IgniteConfiguration">
 		... 
    <!-- Enable cache events. -->
    <property name="includeEventTypes">
        <util:constant static-field="org.apache.ignite.events.EventType.EVTS_CACHE"/>
    </property>
  	...
</bean>
IgniteConfiguration cfg = new IgniteConfiguration();

// Enable cache events.
cfg.setIncludeEventTypes(EVTS_CACHE);

// Start Ignite node.
Ignition.start(cfg);

By default, event notifications are turned off for performance reasons.

Since thousands of events per second are generated, it creates an additional load on the system. This can lead to significant performance degradation. Therefore, it is highly recommended to enable only those events that your application logic requires.

Local and Remote Events

Get notified of any state change or event occurring cluster-wide.