static void Main(string[] args) { try { CacheFactory cacheFactory = CacheFactory.CreateCacheFactory().SetSubscriptionEnabled(true); Cache cache = cacheFactory.Create(); Console.WriteLine("Created the Geode Cache"); RegionFactory regionFactory = cache.CreateRegionFactory(RegionShortcut.CACHING_PROXY); IRegion <string, string> region = regionFactory.Create <string, string>("exampleRegion"); region.GetSubscriptionService().RegisterAllKeys(); Console.WriteLine("RegisterAllKeys to exampleRegion"); QueryService <string, string> qrySvc = cache.GetQueryService <string, string>(); CqAttributesFactory <string, string> cqFac = new CqAttributesFactory <string, string>(); ICqListener <string, string> cqLstner = new MyCqListener <string, string>(); cqFac.AddCqListener(cqLstner); CqAttributes <string, string> cqAttr = cqFac.Create(); CqQuery <string, string> qry = qrySvc.NewCq("MyCq", "select * from /exampleRegion p where p.toString = '1'", cqAttr, false); ICqResults <string> results = qry.ExecuteWithInitialResults(); Console.WriteLine("Execute CQ: Initial ResultSet returned {0} rows", results.Size); while (true) { System.Threading.Thread.Sleep(5000); string val1 = null; string val2 = null; region.TryGetValue("1", ref val1); region.TryGetValue("2", ref val2); Console.WriteLine("exampleRegion['1']: {0}", val1); Console.WriteLine("exampleRegion['2']: {0}", val2); } } catch (GeodeException gfex) { Console.WriteLine("Geode Exception: {0}", gfex.Message); } }
static void Main(string[] args) { bool verbose = false; if (args.Length == 1 && args[0] == "-v") { verbose = true; } try { // Connect to the GemFire Distributed System using the settings from the gfcpp.properties file by default. Properties prop = Properties.Create(); prop.Insert("cache-xml-file", "clientCqQuery.xml"); CacheFactory cacheFactory = CacheFactory.CreateCacheFactory(prop); Cache cache = cacheFactory.SetSubscriptionEnabled(true) .Create(); Console.WriteLine("Created the GemFire Cache"); // Get the Portfolios Region from the Cache which is declared in the Cache XML file. Region region = cache.GetRegion("Portfolios"); Console.WriteLine("Obtained the Region from the Cache"); region.GetAttributesMutator().SetCacheListener(new MyCacheListener(verbose)); // Register our Serializable/Cacheable Query objects, viz. Portfolio and Position. Serializable.RegisterType(Portfolio.CreateDeserializable); Serializable.RegisterType(Position.CreateDeserializable); //Register all keys region.RegisterAllKeys(); Console.WriteLine("Registered Serializable Query Objects"); // Populate the Region with some Portfolio objects. Portfolio port1 = new Portfolio(1 /*ID*/, 10 /*size*/); Portfolio port2 = new Portfolio(2 /*ID*/, 20 /*size*/); Portfolio port3 = new Portfolio(3 /*ID*/, 30 /*size*/); region.Put("Key1", port1); region.Put("Key2", port2); region.Put("Key3", port3); Console.WriteLine("Populated some Portfolio Objects"); // Get the QueryService from the Cache. QueryService qrySvc = cache.GetQueryService(); Console.WriteLine("Got the QueryService from the Cache"); //create CqAttributes with listener CqAttributesFactory cqFac = new CqAttributesFactory(); ICqListener cqLstner = new MyCqListener(0, verbose); cqFac.AddCqListener(cqLstner); CqAttributes cqAttr = cqFac.Create(); //create a new cqQuery CqQuery qry = qrySvc.NewCq(cqNames[0], queryStrings[0], cqAttr, true); // Execute a CqQuery with Initial Results ICqResults results = qry.ExecuteWithInitialResults(); Console.WriteLine("ResultSet Query returned {0} rows", results.Size); SelectResultsIterator iter = results.GetIterator(); while (iter.HasNext) { IGFSerializable item = iter.Next(); if (item != null) { Struct st = item as Struct; CacheableString key = st["key"] as CacheableString; Console.WriteLine("Got key " + key.Value); Portfolio port = st["value"] as Portfolio; if (port == null) { Position pos = st["value"] as Position; if (pos == null) { CacheableString cs = st["value"] as CacheableString; if (cs == null) { Console.WriteLine("Query got other/unknown object."); } else { Console.WriteLine("Query got string : {0}.", cs.Value); } } else { Console.WriteLine("Query got Position object with secId {0}, shares {1}.", pos.SecId, pos.SharesOutstanding); } } else { Console.WriteLine("Query got Portfolio object with ID {0}, pkid {1}.", port.ID, port.Pkid); } } } //Stop the cq qry.Stop(); //Restart the cq qry.Execute(); for (int i = 1; i < cqNames.Length; i++) { ICqListener cqLstner1 = new MyCqListener(i, verbose); cqFac.AddCqListener(cqLstner1); cqAttr = cqFac.Create(); qry = qrySvc.NewCq(cqNames[i], queryStrings[i], cqAttr, true); } qry = qrySvc.GetCq(cqNames[6]); cqAttr = qry.GetCqAttributes(); ICqListener[] vl = cqAttr.getCqListeners(); Console.WriteLine("number of listeners for cq[{0}] is {1}", cqNames[6], vl.Length); qry = qrySvc.GetCq(cqNames[0]); CqAttributesMutator cqam = qry.GetCqAttributesMutator(); for (int i = 0; i < vl.Length; i++) { cqam.AddCqListener(vl[i]); } //Stop the cq qry.Stop(); //Start all Cq Query qrySvc.ExecuteCqs(); for (int i = 0; i < cqNames.Length; i++) { Console.WriteLine("get info for cq[{0}]:", cqNames[i]); CqQuery cqy = qrySvc.GetCq(cqNames[i]); CqStatistics cqStats = cqy.GetStatistics(); Console.WriteLine("Cq[{0}]: CqStatistics: numInserts[{1}], numDeletes[{2}], numUpdates[{3}], numEvents[{4}]", cqNames[i], cqStats.numInserts(), cqStats.numDeletes(), cqStats.numUpdates(), cqStats.numEvents()); } CqServiceStatistics serviceStats = qrySvc.GetCqStatistics(); Console.WriteLine("numCqsActive={0}, numCqsCreated={1}, numCqsClosed={2}, numCqsStopped={3}, numCqsOnClient={4}", serviceStats.numCqsActive(), serviceStats.numCqsCreated(), serviceStats.numCqsClosed(), serviceStats.numCqsStopped(), serviceStats.numCqsOnClient()); while (true) { Console.WriteLine("*******Type \'q\' to quit !!!! ******"); ConsoleKeyInfo ckey; ckey = Console.ReadKey(true); if (ckey.Key == ConsoleKey.Q) { break; } } //Stop all cqs qrySvc.StopCqs(); for (int i = 0; i < cqNames.Length; i++) { Console.WriteLine("get info for cq[{0}]:", cqNames[i]); CqQuery cqy = qrySvc.GetCq(cqNames[i]); cqAttr = qry.GetCqAttributes(); vl = cqAttr.getCqListeners(); Console.WriteLine("number of listeners for cq[{0}] is {1}", cqNames[i], vl.Length); CqStatistics cqStats = cqy.GetStatistics(); Console.WriteLine("Cq[{0}]: CqStatistics: numInserts[{1}], numDeletes[{2}], numUpdates[{3}], numEvents[{4}]", cqNames[i], cqStats.numInserts(), cqStats.numDeletes(), cqStats.numUpdates(), cqStats.numEvents()); } //Close all cqs qrySvc.CloseCqs(); Console.WriteLine("numCqsActive={0}, numCqsCreated={1}, numCqsClosed={2}, numCqsStopped={3}, numCqsOnClient={4}", serviceStats.numCqsActive(), serviceStats.numCqsCreated(), serviceStats.numCqsClosed(), serviceStats.numCqsStopped(), serviceStats.numCqsOnClient()); // Close the GemFire Cache. cache.Close(); Console.WriteLine("Closed the GemFire Cache"); } // An exception should not occur catch (GemFireException gfex) { Console.WriteLine("CqQuery GemFire Exception: {0}", gfex.Message); } }
static void Main(string[] args) { try { // Create a Geode Cache Programmatically. CacheFactory cacheFactory = CacheFactory.CreateCacheFactory(); Cache cache = cacheFactory.SetSubscriptionEnabled(true) .AddServer("localhost", 50505) .Create(); Console.WriteLine("Created the Geode Cache"); RegionFactory regionFactory = cache.CreateRegionFactory(RegionShortcut.PROXY); // Create the example Region programmatically. IRegion <string, Portfolio> region = regionFactory.Create <string, Portfolio>("Portfolios"); Console.WriteLine("Created the Region Programmatically."); // Register our Serializable/Cacheable Query objects, viz. Portfolio and Position. Serializable.RegisterTypeGeneric(Portfolio.CreateDeserializable); Serializable.RegisterTypeGeneric(Position.CreateDeserializable); Console.WriteLine("Registered Serializable Query Objects"); // Populate the Region with some Portfolio objects. Portfolio port1 = new Portfolio(1 /*ID*/, 10 /*size*/); Portfolio port2 = new Portfolio(2 /*ID*/, 20 /*size*/); Portfolio port3 = new Portfolio(3 /*ID*/, 30 /*size*/); region["Key1"] = port1; region["Key2"] = port2; region["Key3"] = port3; Console.WriteLine("Populated some Portfolio Objects"); // Get the QueryService from the Cache. QueryService <string, object> qrySvc = cache.GetQueryService <string, object>(); Console.WriteLine("Got the QueryService from the Cache"); //create CqAttributes with listener CqAttributesFactory <string, object> cqFac = new CqAttributesFactory <string, object>(); ICqListener <string, object> cqLstner = new MyCqListener <string, object>(); cqFac.AddCqListener(cqLstner); CqAttributes <string, object> cqAttr = cqFac.Create(); //create a new cqQuery CqQuery <string, object> qry = qrySvc.NewCq("MyCq", "select * from /Portfolios" + " p where p.ID!=2", cqAttr, false); // Execute a CqQuery with Initial Results ICqResults <object> results = qry.ExecuteWithInitialResults(); Console.WriteLine("ResultSet Query returned {0} rows", results.Size); //make changes to generate cq events region["Key2"] = port1; region["Key3"] = port2; region["Key1"] = port3; SelectResultsIterator <object> iter = results.GetIterator(); while (iter.HasNext) { object item = iter.Next(); if (item != null) { Struct st = item as Struct; string key = st["key"] as string; Console.WriteLine("Got key " + key); Portfolio port = st["value"] as Portfolio; if (port == null) { Position pos = st["value"] as Position; if (pos == null) { string cs = st["value"] as string; if (cs == null) { Console.WriteLine("Query got other/unknown object."); } else { Console.WriteLine("Query got string : {0}.", cs); } } else { Console.WriteLine("Query got Position object with secId {0}, shares {1}.", pos.SecId, pos.SharesOutstanding); } } else { Console.WriteLine("Query got Portfolio object with ID {0}, pkid {1}.", port.ID, port.Pkid); } } } //Stop the cq qry.Stop(); //Close the cq qry.Close(); // Close the Geode Cache. cache.Close(); Console.WriteLine("Closed the Geode Cache"); } // An exception should not occur catch (GeodeException gfex) { Console.WriteLine("CqQuery Geode Exception: {0}", gfex.Message); } }
static void Main(string[] args) { try { //Create CacheFactory using the user specified properties or from the gfcpp.properties file by default. Properties <string, string> prp = Properties <string, string> .Create <string, string>(); prp.Insert("cache-xml-file", "XMLs/clientPoolCqQuery.xml"); CacheFactory cacheFactory = CacheFactory.CreateCacheFactory(prp); Console.WriteLine("Created CacheFactory"); // Create a Geode Cache with the "clientPoolCqQuery.xml" Cache XML file. Cache cache = cacheFactory.Create(); Console.WriteLine("Created the Geode Cache"); // Get the Portfolios Region from the Cache which is declared in the Cache XML file. IRegion <string, Portfolio> region = cache.GetRegion <string, Portfolio>("Portfolios"); Console.WriteLine("Obtained the Region from the Cache"); // Register our Serializable/Cacheable Query objects, viz. Portfolio and Position. Serializable.RegisterTypeGeneric(Portfolio.CreateDeserializable); Serializable.RegisterTypeGeneric(Position.CreateDeserializable); Console.WriteLine("Registered Serializable Query Objects"); // Populate the Region with some Portfolio objects. Portfolio port1 = new Portfolio(1 /*ID*/, 10 /*size*/); Portfolio port2 = new Portfolio(2 /*ID*/, 20 /*size*/); Portfolio port3 = new Portfolio(3 /*ID*/, 30 /*size*/); region["Key1"] = port1; region["Key2"] = port2; region["Key3"] = port3; Console.WriteLine("Populated some Portfolio Objects"); Pool pp = PoolManager.Find("examplePool"); // Get the QueryService from the Pool QueryService <string, object> qrySvc = pp.GetQueryService <string, object>(); Console.WriteLine("Got the QueryService from the Cache"); //create CqAttributes with listener CqAttributesFactory <string, object> cqFac = new CqAttributesFactory <string, object>(); ICqListener <string, object> cqLstner = new MyCqListener <string, object>(); cqFac.AddCqListener(cqLstner); CqAttributes <string, object> cqAttr = cqFac.Create(); //create a new cqQuery CqQuery <string, object> qry = qrySvc.NewCq("MyCq", "select * from /Portfolios" + " p where p.ID!=2", cqAttr, false); // Execute a CqQuery with Initial Results ICqResults <object> results = qry.ExecuteWithInitialResults(); Console.WriteLine("ResultSet Query returned {0} rows", results.Size); //make changes to generate cq events region["Key2"] = port1; region["Key3"] = port2; region["Key1"] = port3; SelectResultsIterator <object> iter = results.GetIterator(); while (iter.HasNext) { object item = iter.Next(); if (item != null) { Struct st = item as Struct; string key = st["key"] as string;; Console.WriteLine("Got key " + key); Portfolio port = st["value"] as Portfolio; if (port == null) { Position pos = st["value"] as Position; if (pos == null) { string cs = st["value"] as string; if (cs == null) { Console.WriteLine("Query got other/unknown object."); } else { Console.WriteLine("Query got string : {0}.", cs); } } else { Console.WriteLine("Query got Position object with secId {0}, shares {1}.", pos.SecId, pos.SharesOutstanding); } } else { Console.WriteLine("Query got Portfolio object with ID {0}, pkid {1}.", port.ID, port.Pkid); } } } //Stop the cq qry.Stop(); //Close the cq qry.Close(); // Close the Geode Cache. cache.Close(); Console.WriteLine("Closed the Geode Cache"); } // An exception should not occur catch (GeodeException gfex) { Console.WriteLine("PoolCqQuery Geode Exception: {0}", gfex.Message); } }
public void StepOneQE(string locators) { CacheHelper.CreateTCRegion_Pool <object, object>(QERegionName, true, true, null, locators, "__TESTPOOL1_", true); IRegion <object, object> region = CacheHelper.GetVerifyRegion <object, object>(QERegionName); Portfolio p1 = new Portfolio(1, 100); Portfolio p2 = new Portfolio(2, 100); Portfolio p3 = new Portfolio(3, 100); Portfolio p4 = new Portfolio(4, 100); region["1"] = p1; region["2"] = p2; region["3"] = p3; region["4"] = p4; var qs = CacheHelper.DCache.GetPoolManager().Find("__TESTPOOL1_").GetQueryService(); CqAttributesFactory <object, object> cqFac = new CqAttributesFactory <object, object>(); ICqListener <object, object> cqLstner = new MyCqListener <object, object>(); cqFac.AddCqListener(cqLstner); CqAttributes <object, object> cqAttr = cqFac.Create(); CqQuery <object, object> qry = qs.NewCq(CqName, "select * from /" + QERegionName + " p where p.ID!=2", cqAttr, false); ICqResults <object> results = qry.ExecuteWithInitialResults(); Thread.Sleep(18000); // sleep 0.3min to allow server c query to complete region["4"] = p1; region["3"] = p2; region["2"] = p3; region["1"] = p4; Thread.Sleep(18000); // sleep 0.3min to allow server c query to complete Util.Log("Results size {0}.", results.Size); foreach (var item in results) { if (item != null) { Struct st = item as Struct; string key = st["key"] as string; Assert.IsNotNull(key, "key is null"); Portfolio port = st["value"] as Portfolio; if (port == null) { Position pos = st["value"] as Position; if (pos == null) { string cs = item as string; if (cs == null) { Assert.Fail("value is null"); Util.Log("Query got other/unknown object."); } else { Util.Log("Query got string : {0}.", cs); } } else { Util.Log("Query got Position object with secId {0}, shares {1}.", pos.SecId, pos.SharesOutstanding); } } else { Util.Log("Query got Portfolio object with ID {0}, pkid {1}.", port.ID, port.Pkid); } } } qry = qs.GetCq <object, object>(CqName); qry.Stop(); qry.Close(); // Bring down the region region.GetLocalView().DestroyRegion(); }