Using Continuous Queries
How to use continuous queries in GridGain to monitor cache modifications, including local listeners, initial queries, remote filters and transformers.
A continuous query is a query that monitors data modifications occurring in a cache. Once a continuous query is started, you get notified of all the data changes that fall into your query filter.
All update events are propagated to the local listener. Continuous query implementation guarantees exactly once delivery of an event to the local listener.
You can also specify a remote filter to narrow down the range of entries that are monitored for updates.
Local Listener
When you modify a cache (insert, update, or delete an entry), an event is sent to the continuous query's local listener so that your application can react accordingly. This event is sent regardless of how the modification is made, including data center replication. The local listener is executed on the node that initiated the query.
Note that the continuous query throws an exception if none of a local listener, a remote filter, or a remote filter factory is specified.
IgniteCache<Integer, String> cache = ignite.getOrCreateCache("myCache");
ContinuousQuery<Integer, String> query = new ContinuousQuery<>();
query.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> events)
throws CacheEntryListenerException {
// react to the update events here
}
});
cache.query(query);
class LocalListener : ICacheEntryEventListener<int, string>
{
public void OnEvent(IEnumerable<ICacheEntryEvent<int, string>> evts)
{
foreach (var cacheEntryEvent in evts)
{
//react to update events here
}
}
}
public static void ContinuousQueryListenerDemo()
{
var ignite = Ignition.Start(new IgniteConfiguration
{
DiscoverySpi = new TcpDiscoverySpi
{
LocalPort = 48500,
LocalPortRange = 20,
IpFinder = new TcpDiscoveryStaticIpFinder
{
Endpoints = new[]
{
"127.0.0.1:48500..48520"
}
}
}
});
var cache = ignite.GetOrCreateCache<int, string>("myCache");
var query = new ContinuousQuery<int, string>(new LocalListener());
var handle = cache.QueryContinuous(query);
cache.Put(1, "1");
cache.Put(2, "2");
}Avoid using operations that could block execution thread inside the listener. This can lead to cluster-wide issues with cache processing. Cache operations such as put, putAll, get, etc., belong to the above category. Therefore, the following example is unsafe:
To make the above operation safe, you can use the @IgniteAsyncCallback annotation:
Initial Query
You can specify an initial query that is executed before the continuous query gets registered in the cluster and before you start to receive updates. To specify an initial query, use the ContinuousQuery.setInitialQuery(...) method.
Just like scan queries, a continuous query is executed via the query() method that returns a cursor. When an initial query is set, you can use that cursor to iterate over the results of the initial query.
Remote Filter
This filter is executed for each updated key and evaluates whether the update should be propagated to the query's local listener. If the filter returns true, then the local listener is notified about the update.
For redundancy reasons, the filter is executed for both primary and backup versions (if backups are configured) of the key. Because of this, a remote filter can be used as a remote listener for update events.
Remote Transformer
By default, continuous queries send the whole updated object to the local listener. This can lead to excessive network usage, especially if the object is very large. Moreover, applications often need only a subset of fields of the object.
To address these cases, you can use a continuous query with a transformer. A transformer is a function that is executed on remote nodes for every updated object and sends back only the results of the transformation.
unsupported
unsupported
Asynchronous Continuous Queries (.NET)
The .NET client can expose a continuous query as an IAsyncEnumerable<T>, so you consume cache update events with await foreach instead of registering a local listener. This gives you idiomatic, non-blocking async iteration over the event stream.
Use one of the ICache.QueryContinuousAsync(...) overloads. The overload without an initial query returns the event stream directly:
The query starts lazily when enumeration begins. Ending the await foreach loop, disposing the enumerator, or cancelling the supplied CancellationToken stops the query and releases the associated server-side resources. The stream can be enumerated only once; a second attempt throws InvalidOperationException.
You can narrow the monitored entries with a server-side filter and tune server-side batching with ContinuousQueryOptions:
Query Handle
To also iterate over the entries that already exist when the query starts, pass an initial query: ScanQuery, SqlQuery, TextQuery, or SqlFieldsQuery.
These overloads return an IContinuousQueryHandleAsync handle instead of a stream. The handle exposes two methods:
GetInitialQueryCursor()reads the pre-existing entries.GetEvents()returns the subsequent updates as anIAsyncEnumerable.
You can call each method only once. Disposing the handle stops the query.
Events Delivery Guarantees
Continuous queries ensure the exactly-once semantic for the delivery of events to the clients' local listeners.
Both primary and backup nodes maintain an update queue that holds events that are processed by continuous queries on the server side but yet to be delivered to the clients. Suppose a primary node crashes or the cluster topology changes for any reason. In that case, every backup node flushes the content of its update queue to the client, making sure that every event is delivered to the client's local listener.
GridGain manages a special per-partition update counter that helps to avoid duplicate notifications. Once an entry in some partition is updated, a counter for this partition is incremented on both primary and backup nodes. The value of this counter is also sent along with the event notification to the client. Thus, the client can skip already-processed events. Once the client confirms that an event is received, the primary and backup nodes remove the record for this event from their backup queues.
See Also
This page is: Copyright © 2026 MariaDB. All rights reserved.
Last updated
Was this helpful?

