public interface GridCacheContinuousQuery<K,V> extends AutoCloseable
Continuous queries are executed as follows:
LOCAL
and REPLICATED caches query will be always executed
locally.
'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.
qry = cache.createContinuousQuery();
// Callback that is called locally when update notifications are received.
// It simply prints out information about all created persons.
qry.callback(new GridPredicate2<UUID, Collection<Map.Entry<UUID, Person>>>() {
@Override public boolean apply(UUID uuid, Collection<Map.Entry<UUID, Person>> entries) {
for (Map.Entry<UUID, Person> e : entries) {
Person p = e.getValue();
X.println(">>>");
X.println(">>> " + p.getFirstName() + " " + p.getLastName() +
"'s salary is " + p.getSalary());
X.println(">>>");
}
return true;
}
});
// This query will return persons with salary above 1000.
qry.filter(new GridPredicate2<UUID, Person>() {
@Override public boolean apply(UUID uuid, Person person) {
return person.getSalary() > 1000;
}
});
// Execute query.
qry.execute();
This will execute query on all nodes that have cache you are working with and notify callback
with both data that already exists in cache and further updates.
To stop receiving updates call close() method:
qry.cancel();Note that one query instance can be executed only once. After it's cancelled, it's non-operational. If you need to repeat execution, use
GridCacheQueries.createContinuousQuery() method to create
new query.| Modifier and Type | Field and Description |
|---|---|
static boolean |
DFLT_AUTO_UNSUBSCRIBE
Default value for automatic unsubscription flag.
|
static int |
DFLT_BUF_SIZE
Default buffer size.
|
static long |
DFLT_TIME_INTERVAL
Maximum default time interval after which buffer will be flushed (if buffering is enabled).
|
| Modifier and Type | Method and Description |
|---|---|
void |
autoUnsubscribe(boolean autoUnsubscribe)
Sets automatic unsubscribe flag.
|
int |
bufferSize()
Gets buffer size.
|
void |
bufferSize(int bufSize)
Sets buffer size.
|
GridBiPredicate<UUID,Collection<Map.Entry<K,V>>> |
callback()
Gets local callback.
|
void |
callback(GridBiPredicate<UUID,Collection<Map.Entry<K,V>>> cb)
Sets mandatory local callback.
|
void |
close()
Stops continuous query execution.
|
void |
execute()
Starts continuous query execution on the whole grid.
|
void |
execute(GridProjection prj)
Starts continuous query execution on provided set of nodes.
|
GridBiPredicate<K,V> |
filter()
Gets key-value filter.
|
void |
filter(GridBiPredicate<K,V> filter)
Sets optional key-value filter.
|
boolean |
isAutoUnsubscribe()
Gets automatic unsubscribe flag.
|
long |
timeInterval()
Gets time interval.
|
void |
timeInterval(long timeInterval)
Sets time interval.
|
static final int DFLT_BUF_SIZE
1 means that all entries
will be sent to master node immediately (buffering is disabled).static final long DFLT_TIME_INTERVAL
static final boolean DFLT_AUTO_UNSUBSCRIBE
void callback(GridBiPredicate<UUID,Collection<Map.Entry<K,V>>> cb)
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.
cb - Local callback.GridBiPredicate<UUID,Collection<Map.Entry<K,V>>> callback()
callback(GridBiPredicate) for more information.void filter(@Nullable GridBiPredicate<K,V> filter)
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.
filter - Key-value filter.@Nullable GridBiPredicate<K,V> filter()
filter(GridBiPredicate) for more information.void bufferSize(int bufSize)
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 or time provided via timeInterval(long)
method is exceeded.
Default buffer size is 1 which means that entries will
be sent immediately (buffering is disabled).
bufSize - Buffer size.int bufferSize()
bufferSize(int) for more information.void timeInterval(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 bufferSize(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.long timeInterval()
timeInterval(long) for more information.void autoUnsubscribe(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.boolean isAutoUnsubscribe()
autoUnsubscribe(boolean)
for more information.void execute()
throws GridException
Note that if grid contains nodes without appropriate cache, these nodes will be filtered out.
Also note that for LOCAL
and REPLICATED caches
query will be always executed locally.
GridException - In case of error.void execute(@Nullable GridProjection prj) throws GridException
Note that if provided projection contains nodes without appropriate cache, these nodes will be filtered out.
Also note that for LOCAL
and REPLICATED caches
query will be always executed locally.
prj - Grid projection.GridException - In case of error.void close()
throws GridException
Note that one query instance can be executed only once.
After it's cancelled, it's non-operational.
If you need to repeat execution, use GridCacheQueries.createContinuousQuery()
method to create new query.
close in interface AutoCloseableGridException - In case of error.Copyright © 2014. All rights reserved.