public final class ContinuousQuery<K,V> extends Query<javax.cache.Cache.Entry<K,V>>
Continuous queries allow to register a remote filter and a local listener for cache updates. If an update event passes the filter, it will be sent to the node that executed the query and local listener will be notified.
Additionally, you can execute initial query to get currently existing data.
Query can be of any type (SQL, TEXT or SCAN) and can be set via setInitialQuery(Query)
method.
Query can be executed either on all nodes in topology using IgniteCache.query(Query)
method, or only on the local node, if Query.setLocal(boolean)
parameter is set to true
.
Note that in case query is distributed and a new node joins, it will get the remote
filter for the query during discovery process before it actually joins topology,
so no updates will be missed.
'Person'
objects and we need
to query all persons with salary above 1000.
Here is the Person
class:
public class Person { // Name. private String name; // Salary. private double salary; ... }
You can create and execute continuous query like so:
// Create new continuous query. ContinuousQuery<Long, Person> qry = new ContinuousQuery<>(); // Initial iteration query will return all persons with salary above 1000. qry.setInitialQuery(new ScanQuery<>((id, p) -> p.getSalary() > 1000)); // Callback that is called locally when update notifications are received. // It simply prints out information about all created persons. qry.setLocalListener((evts) -> { for (CacheEntryEvent<? extends Long, ? extends Person> e : evts) { Person p = e.getValue(); System.out.println(p.getFirstName() + " " + p.getLastName() + "'s salary is " + p.getSalary()); } }); // Continuous listener will be notified for persons with salary above 1000. qry.setRemoteFilter(evt -> evt.getValue().getSalary() > 1000); // Execute query and get cursor that iterates through initial data. QueryCursor<Cache.Entry<Long, Person>> cur = cache.query(qry);This will execute query on all nodes that have cache you are working with and listener will start to receive notifications for cache updates.
To stop receiving updates call QueryCursor.close()
method:
cur.close();Note that this works even if you didn't provide initial query. Cursor will be empty in this case, but it will still unregister listeners when
QueryCursor.close()
is called.
IgniteAsyncCallback
annotation is supported for CacheEntryEventFilter
(see setRemoteFilterFactory(Factory)
) and CacheEntryUpdatedListener
(see setLocalListener(CacheEntryUpdatedListener)
).
If filter and/or listener are annotated with IgniteAsyncCallback
then annotated callback
is executed in async callback pool (see IgniteConfiguration.getAsyncCallbackPoolSize()
)
and notification order is kept the same as update order for given cache key.
Modifier and Type | Field and Description |
---|---|
static boolean |
DFLT_AUTO_UNSUBSCRIBE
Default value for automatic unsubscription flag.
|
static int |
DFLT_PAGE_SIZE
Default page size.
|
static long |
DFLT_TIME_INTERVAL
Maximum default time interval after which buffer will be flushed (if buffering is enabled).
|
Constructor and Description |
---|
ContinuousQuery()
Creates new continuous query.
|
Modifier and Type | Method and Description |
---|---|
Query<javax.cache.Cache.Entry<K,V>> |
getInitialQuery()
Gets initial query.
|
javax.cache.event.CacheEntryUpdatedListener<K,V> |
getLocalListener()
Gets local listener.
|
CacheEntryEventSerializableFilter<K,V> |
getRemoteFilter()
Gets remote filter.
|
javax.cache.configuration.Factory<? extends javax.cache.event.CacheEntryEventFilter<K,V>> |
getRemoteFilterFactory()
Gets remote filter.
|
long |
getTimeInterval()
Gets time interval.
|
boolean |
isAutoUnsubscribe()
Gets automatic unsubscription flag value.
|
boolean |
isIncludeExpired()
Gets the flag value defining whether to notify about
EventType.EXPIRED events. |
ContinuousQuery<K,V> |
setAutoUnsubscribe(boolean autoUnsubscribe)
Sets automatic unsubscribe flag.
|
void |
setIncludeExpired(boolean includeExpired)
Sets the flag value defining whether to notify about
EventType.EXPIRED events. |
ContinuousQuery<K,V> |
setInitialQuery(Query<javax.cache.Cache.Entry<K,V>> initQry)
Sets initial query.
|
ContinuousQuery<K,V> |
setLocal(boolean loc)
Sets whether this query should be executed on local node only.
|
ContinuousQuery<K,V> |
setLocalListener(javax.cache.event.CacheEntryUpdatedListener<K,V> locLsnr)
Sets local callback.
|
ContinuousQuery<K,V> |
setPageSize(int pageSize)
Sets optional page size, if
0 , then default is used. |
ContinuousQuery<K,V> |
setRemoteFilter(CacheEntryEventSerializableFilter<K,V> rmtFilter)
Deprecated.
Use
setRemoteFilterFactory(Factory) instead. |
ContinuousQuery<K,V> |
setRemoteFilterFactory(javax.cache.configuration.Factory<? extends javax.cache.event.CacheEntryEventFilter<K,V>> rmtFilterFactory)
Sets optional key-value filter factory.
|
ContinuousQuery<K,V> |
setTimeInterval(long timeInterval)
Sets time interval.
|
getPageSize, isLocal, prepare, toString
public static final int DFLT_PAGE_SIZE
1
means that all entries
will be sent to master node immediately (buffering is disabled).public static final long DFLT_TIME_INTERVAL
public static final boolean DFLT_AUTO_UNSUBSCRIBE
public ContinuousQuery<K,V> setInitialQuery(Query<javax.cache.Cache.Entry<K,V>> initQry)
This query will be executed before continuous listener is registered which allows to iterate through entries which already existed at the time continuous query is executed.
initQry
- Initial query.this
for chaining.public Query<javax.cache.Cache.Entry<K,V>> getInitialQuery()
public ContinuousQuery<K,V> setLocalListener(javax.cache.event.CacheEntryUpdatedListener<K,V> locLsnr)
The callback predicate accepts ID of the node from where updates are received and collection
of received entries. Note that for removed entries value will be null
.
If the predicate returns false
, query execution will be cancelled.
WARNING: all operations that involve any kind of JVM-local or distributed locking (e.g., synchronization or transactional cache operations), should be executed asynchronously without blocking the thread that called the callback. Otherwise, you can get deadlocks.
If local listener are annotated with IgniteAsyncCallback
then it is executed in async callback pool
(see IgniteConfiguration.getAsyncCallbackPoolSize()
) that allow to perform a cache operations.
locLsnr
- Local callback.this
for chaining.IgniteAsyncCallback
,
IgniteConfiguration.getAsyncCallbackPoolSize()
public javax.cache.event.CacheEntryUpdatedListener<K,V> getLocalListener()
@Deprecated public ContinuousQuery<K,V> setRemoteFilter(CacheEntryEventSerializableFilter<K,V> rmtFilter)
setRemoteFilterFactory(Factory)
instead.WARNING: all operations that involve any kind of JVM-local or distributed locking (e.g., synchronization or transactional cache operations), should be executed asynchronously without blocking the thread that called the filter. Otherwise, you can get deadlocks.
If remote filter are annotated with IgniteAsyncCallback
then it is executed in async callback
pool (see IgniteConfiguration.getAsyncCallbackPoolSize()
) that allow to perform a cache operations.
rmtFilter
- Key-value filter.this
for chaining.IgniteAsyncCallback
,
IgniteConfiguration.getAsyncCallbackPoolSize()
public CacheEntryEventSerializableFilter<K,V> getRemoteFilter()
public ContinuousQuery<K,V> setRemoteFilterFactory(javax.cache.configuration.Factory<? extends javax.cache.event.CacheEntryEventFilter<K,V>> rmtFilterFactory)
WARNING: all operations that involve any kind of JVM-local or distributed locking (e.g., synchronization or transactional cache operations), should be executed asynchronously without blocking the thread that called the filter. Otherwise, you can get deadlocks.
If remote filter are annotated with IgniteAsyncCallback
then it is executed in async callback
pool (see IgniteConfiguration.getAsyncCallbackPoolSize()
) that allow to perform a cache operations.
rmtFilterFactory
- Key-value filter factory.this
for chaining.IgniteAsyncCallback
,
IgniteConfiguration.getAsyncCallbackPoolSize()
public javax.cache.configuration.Factory<? extends javax.cache.event.CacheEntryEventFilter<K,V>> getRemoteFilterFactory()
public ContinuousQuery<K,V> setTimeInterval(long timeInterval)
When a cache update happens, entry is first put into a buffer. Entries from buffer will
be sent to the master node only if the buffer is full (its size can be provided via setPageSize(int)
method) or time provided via this method is exceeded.
Default time interval is 0
which means that
time check is disabled and entries will be sent only when buffer is full.
timeInterval
- Time interval.this
for chaining.public long getTimeInterval()
public ContinuousQuery<K,V> setAutoUnsubscribe(boolean autoUnsubscribe)
This flag indicates that query filters on remote nodes should be
automatically unregistered if master node (node that initiated the query) leaves topology. If this flag is
false
, filters will be unregistered only when the query is cancelled from master node, and won't ever be
unregistered if master node leaves grid.
Default value for this flag is true
.
autoUnsubscribe
- Automatic unsubscription flag.this
for chaining.public boolean isAutoUnsubscribe()
public void setIncludeExpired(boolean includeExpired)
EventType.EXPIRED
events.
If true
, then the remote listener will get notifications about entries
expired in cache. Otherwise, only EventType.CREATED
, EventType.UPDATED
and EventType.REMOVED
events will be fired in the remote listener.
This flag is false
by default, so EventType.EXPIRED
events are disabled.
includeExpired
- Whether to notify about EventType.EXPIRED
events.public boolean isIncludeExpired()
EventType.EXPIRED
events.EventType.EXPIRED
events.public ContinuousQuery<K,V> setPageSize(int pageSize)
0
, then default is used.setPageSize
in class Query<javax.cache.Cache.Entry<K,V>>
pageSize
- Optional page size.this
for chaining.
Follow @ApacheIgnite
Ignite Fabric : ver. 2.0.0 Release Date : April 30 2017