Alachisoft NCache 4.1 - Online Documentation

Using Continuous Query

Following is a list of methods needs to be defined by the class for implementing Continuous Query and a brief description of their purpose.
  • Instance of ContinuousQuery class and notification registration:
An instance of a ContinuousQuery class is required for all methods. It holds information regarding a unique String query i.e. "command text" and HashMap values i.e. "command parameters". ContinuousQuery class also contains event notifications for Add, Update and Remove operations. To activate a Continuous Query, you need to register event notifications for that particular Continuous Query instance.
Note: For every continuous query instances, at least one Notification must be registered.
Following event handler procedures should be created which will be called by Add, Update and Remove callbacks whenever a result set changes. If user is not interested in some callback, "null" can be passed for that particular callback:
ContinuousQuery query = new ContinuousQuery(queryString, values);
query.RegisterAddNotification(new ContinuousQueryItemAddedCallback()
    public void continuousQueryItemAdded(String key)
        System.out.println("Item Added");
query.RegisterUpdateNotification(new ContinuousQueryItemUpdatedCallback()
    public void continuousQueryItemUpdated(String key)
        System.out.println("Item Updated");
query.RegisterRemoveNotification(new ContinuousQueryItemRemovedCallback()
    public void continuousQueryItemRemoved(String key)
        System.out.print("Item Removed");
  • void registerCQ(ContinuousQuery query);
Register continuous query method is used to activate "Continuous Query" on server.  This method can be called many times for the same query for registering/un-registering various callbacks. cache.registerCQ method is used only for registering continuous query and it does not return any result set to the client application.
  • void unRegisterCQ(ContinuousQuery query);
Unregister Continuous Query method is used to unregister the callbacks from cache when a user is no more interested in notifications for changes in a query result set.
  • Collection searchCQ(ContinuousQuery query);
Search Continuous Query method performs two functionalities, ‘registerCQ’ which activates continuous query on server (if it is not already activated) and ‘searchCQ’ which searches the cache and returns a collection of keys to the client application. The benefit of this approach is that it returns keys only. Then, the client application can determine the keys to be fetched. The drawback of this approach is that if user is going to fetch most of the items from the cache anyway, he will end up making lots of trips to the cache. And, if the cache is distributed, this might eventually become costly.
Collection keys = cache.searchCQ(query);
if (keys.size() > 0)
    Iterator ie = keys.iterator();
    while (ie.hasNext())
        String key = (String);
        Person prs = (Person)cache.get(key);
        System.out.print("Age: {0}", prs.Age);
  • HashMap searchEntriesCQ(ContinuousQuery query);
Search Entries Continuous Query method is almost similar to the cache.searchCQ method but it searches the cache and returns a hashmap containing both, keys and values. It will keep pre-fetched data locally and will immediately return data on demand. It runs query on the dataset and returns a HashMap containing both, keys and values. This way, all the data based on the query is fetched in one call. This is much more efficient way of fetching the data from the cache.
HashMap dict = cache.searchEntriesCQ(query);
if (dict.size() > 0) {
    Iterator ide = dict.keySet().iterator();
    while (ide.hasNext()) {
        String key = (String);
        Person prs = (Person) cache.get(key);
        System.out.print("Key = {0}, Age: {1}", key, prs.Age);
Example Continuous Query:
Here is how your class should look like:
Cache cache = NCache.initializeCache("mycache");
String queryString = "SELECT ClassLibrary1.Person WHERE this.Age >= ?";
HashMap values = new HashMap();
values.put("Age", 10);
ContinuousQuery query = new ContinuousQuery(queryString, values);
query.RegisterAddNotification(new ContinuousQueryItemAddedCallback(continuousQueryItemAdded);     
query.RegisterUpdateNotification(new ContinuousQueryItemUpdatedCallback(continuousQueryItemUpdated);     
query.RegisterRemoveNotification(new ContinuousQueryItemRemovedCallback(continuousQueryItemRemoved);
The following operators are supported in NCache Continuous queries:
= , == , != , <> , < , > , <=, >=, IN, LIKE , NOT LIKE
Logical Operators
Aggregate Functions
Sum, Count, AVG, Min, Max
DateTime.Now , DateTime ("any date time compatible string")
See Also

Copyright © 2005-2012 Alachisoft. All rights reserved.