public void RegisterCollector <TResult>(IResultCollector collector) { List <IResultCollector> collectors; var resultType = typeof(TResult); if (!_collectorMap.TryGetValue(resultType, out collectors)) { collectors = new List <IResultCollector>(); } collectors.Add(collector); _collectorMap[resultType] = collectors; }
static void Main(string[] args) { try { CacheFactory cacheFactory = CacheFactory.CreateCacheFactory(); Cache cache = cacheFactory.SetSubscriptionEnabled(true).AddServer("localhost", 50505).AddServer("localhost", 40404).Create(); Console.WriteLine("Created the Geode Cache"); IRegion <string, string> region = cache.CreateRegionFactory(RegionShortcut.CACHING_PROXY).Create <string, string>("partition_region"); Console.WriteLine("Created the Region"); region.GetSubscriptionService().RegisterAllKeys(); for (int i = 0; i < 34; i++) { region["KEY--" + i] = "VALUE--" + i; } object[] routingObj = new object[17]; int j = 0; for (int i = 0; i < 34; i++) { if (i % 2 == 0) { continue; } routingObj[j] = "KEY--" + i; j++; } Console.WriteLine("routingObj count= {0}.", routingObj.Length); bool args0 = true; //test data dependant function execution //test get function with result Execution <object> exc = FunctionService <object> .OnRegion <string, string>(region); IResultCollector <object> rc = exc.WithArgs <bool>(args0).WithFilter <object>(routingObj).Execute(getFuncName); ICollection <object> executeFunctionResult = rc.GetResult(); List <object> resultList = new List <object>(); foreach (List <object> item in executeFunctionResult) { foreach (object subitem in item) { resultList.Add(subitem); } } Console.WriteLine("on region: result count= {0}.", resultList.Count); for (int i = 0; i < resultList.Count; i++) { Console.WriteLine("on region:get:result[{0}]={1}.", i, (string)resultList[i]); } //test date independant fucntion execution on one server //test get function with result exc = FunctionService <object> .OnServer(cache); ArrayList args1 = new ArrayList(); for (int i = 0; i < routingObj.Length; i++) { Console.WriteLine("routingObj[{0}]={1}.", i, (string)routingObj[i]); args1.Add(routingObj[i]); } rc = exc.WithArgs <ArrayList>(args1).Execute(getFuncIName); executeFunctionResult = rc.GetResult(); Console.WriteLine("on one server: result count= {0}.", executeFunctionResult.Count); List <object> resultList1 = new List <object>(); foreach (List <object> item in executeFunctionResult) { foreach (object subitem in item) { resultList1.Add(subitem); } } if (resultList1.Count != 17) { Console.WriteLine("result count check failed on one server:get:"); } for (int i = 0; i < resultList1.Count; i++) { Console.WriteLine("on one server:get:result[{0}]={1}.", i, (string)resultList1[i]); } //test date independant fucntion execution on all servers //test get function with result exc = FunctionService <object> .OnServers(cache); rc = exc.WithArgs <ArrayList>(args1).Execute(getFuncIName); executeFunctionResult = rc.GetResult(); Console.WriteLine("on all servers: result count= {0}.", executeFunctionResult.Count); List <object> resultList2 = new List <object>(); foreach (List <object> item in executeFunctionResult) { foreach (object subitem in item) { resultList2.Add(subitem); } } if (resultList2.Count != 34) { Console.WriteLine("result count check failed on all servers"); } for (int i = 0; i < resultList2.Count; i++) { Console.WriteLine("on all servers:result[{0}]={1}.", i, (string)resultList2[i]); } // Close the Geode Cache. cache.Close(); Console.WriteLine("Closed the Geode Cache"); } // An exception should not occur catch (GeodeException gfex) { Console.WriteLine("ExecuteFunctions Geode Exception: {0}", gfex.Message); } }
void runWithUserRoot(Cache cache) { Console.WriteLine("Created the Region Programmatically. 0"); //user "root" 's credential Properties <string, object> credentials = Properties <string, object> .Create <string, object>(); //user = "******" has permission to do put/get both credentials.Insert("security-username", "root"); credentials.Insert("security-password", "root"); Console.WriteLine("Created the Region Programmatically. 1"); // Create user cache by passing credentials IRegionService userCache1 = cache.CreateAuthenticatedView(credentials); Console.WriteLine("Created the Region Programmatically. 2"); // Create region using usercache IRegion <string, string> userRegion1 = userCache1.GetRegion <string, string>("partition_region"); //doing operation on behalf of user "root" userRegion1["key-1"] = "val-1"; string result = userRegion1["key-1"]; //to execute function on server Execution <object> exc = FunctionService <object> .OnServer(userCache1); ArrayList args1 = new ArrayList(); args1.Add("key-1"); IResultCollector <object> rc = exc.WithArgs <ArrayList>(args1).Execute(getFuncIName); ICollection <object> executeFunctionResult = rc.GetResult(); Console.WriteLine("on one server: result count= {0}.", executeFunctionResult.Count); List <object> resultList1 = new List <object>(); foreach (List <object> item in executeFunctionResult) { foreach (object subitem in item) { resultList1.Add(subitem); } } for (int i = 0; i < resultList1.Count; i++) { Console.WriteLine("on one server:get:result[{0}]={1}.", i, (string)resultList1[i]); } //to execute Query // Get the QueryService from the Cache. QueryService <string, string> qrySvc = userCache1.GetQueryService <string, string>(); Console.WriteLine("Got the QueryService from the user Cache"); // Execute a Query which returns a ResultSet. Query <string> qry = qrySvc.NewQuery("SELECT DISTINCT * FROM /partition_region"); ISelectResults <string> results = qry.Execute(); Console.WriteLine("ResultSet Query returned {0} rows", results.Size); userCache1.Close(); Console.WriteLine("User root done put/get ops successfully"); }
public void OnServerHAStepOne() { Region region = CacheHelper.GetVerifyRegion <object, object>(QERegionName); for (int i = 0; i < 34; i++) { region["KEY--" + i] = "VALUE--" + i; } object[] routingObj = new object[17]; ArrayList args1 = new ArrayList(); int j = 0; for (int i = 0; i < 34; i++) { if (i % 2 == 0) { continue; } routingObj[j] = "KEY--" + i; j++; } Util.Log("routingObj count= {0}.", routingObj.Length); for (int i = 0; i < routingObj.Length; i++) { Console.WriteLine("routingObj[{0}]={1}.", i, (string)routingObj[i]); args1.Add(routingObj[i]); } //test data independant function execution with result onServer Pool /*<TKey, TValue>*/ pool = CacheHelper.DCache.GetPoolManager().Find(poolName); Apache.Geode.Client.Execution <object> exc = FunctionService <object> .OnServer(pool); Assert.IsTrue(exc != null, "onServer Returned NULL"); IResultCollector <object> rc = exc.WithArgs <ArrayList>(args1).Execute(OnServerHAExceptionFunction, TimeSpan.FromSeconds(15)); ICollection <object> executeFunctionResult = rc.GetResult(); List <object> resultList = new List <object>(); Console.WriteLine("executeFunctionResult.Length = {0}", executeFunctionResult.Count); foreach (List <object> item in executeFunctionResult) { foreach (object item2 in item) { resultList.Add(item2); } } Util.Log("on region: result count= {0}.", resultList.Count); Assert.IsTrue(resultList.Count == 17, "result count check failed"); for (int i = 0; i < resultList.Count; i++) { Util.Log("on region:get:result[{0}]={1}.", i, (string)resultList[i]); Assert.IsTrue(((string)resultList[i]) != null, "onServer Returned NULL"); } rc = exc.WithArgs <ArrayList>(args1).Execute(OnServerHAShutdownFunction, TimeSpan.FromSeconds(15)); ICollection <object> executeFunctionResult1 = rc.GetResult(); List <object> resultList1 = new List <object>(); foreach (List <object> item in executeFunctionResult1) { foreach (object item2 in item) { resultList1.Add(item2); } } Util.Log("on region: result count= {0}.", resultList1.Count); Console.WriteLine("resultList1.Count = {0}", resultList1.Count); Assert.IsTrue(resultList1.Count == 17, "result count check failed"); for (int i = 0; i < resultList1.Count; i++) { Util.Log("on region:get:result[{0}]={1}.", i, (string)resultList1[i]); Assert.IsTrue(((string)resultList1[i]) != null, "onServer Returned NULL"); } // Bring down the region //region.LocalDestroyRegion(); }
static void Main(string[] args) { try { // Create CacheFactory using the settings from the gfcpp.properties file by default. CacheFactory cacheFactory = CacheFactory.CreateCacheFactory(); Cache cache = cacheFactory.SetSubscriptionEnabled(true) .AddServer("localhost", 40404) .AddServer("localhost", 50505) .Create(); Console.WriteLine("Created the GemFire Cache"); Region region = cache.CreateRegionFactory(RegionShortcut.CACHING_PROXY) .Create("partition_region"); Console.WriteLine("Created the partition_region."); for (int i = 0; i < 34; i++) { region.Put("KEY--" + i, "VALUE--" + i); } IGFSerializable[] routingObj = new IGFSerializable[17]; int j = 0; for (int i = 0; i < 34; i++) { if (i % 2 == 0) { continue; } routingObj[j] = new CacheableString("KEY--" + i); j++; } Console.WriteLine("routingObj count= {0}.", routingObj.Length); //test data dependant function execution // test get function with result Boolean getResult = true; IGFSerializable args0 = new CacheableBoolean(true); Execution exc = FunctionService.OnRegion(region); IResultCollector rc = exc.WithArgs(args0).WithFilter(routingObj).Execute( getFuncName, getResult); IGFSerializable[] executeFunctionResult = rc.GetResult(); Console.WriteLine("on region: result count= {0}.", executeFunctionResult.Length); List <IGFSerializable> resultList = new List <IGFSerializable>(); for (int pos = 0; pos < executeFunctionResult.Length; pos++) { CacheableArrayList resultItem = executeFunctionResult[pos] as CacheableArrayList; foreach (IGFSerializable item in resultItem) { resultList.Add(item); } } Console.WriteLine("on region: result count= {0}.", resultList.Count); for (int i = 0; i < resultList.Count; i++) { Console.WriteLine("on region:get:result[{0}]={1}.", i, (resultList[i] as CacheableString).Value); } getResult = true; //test date independant fucntion execution on one server // test get function with result exc = FunctionService.OnServer(cache); CacheableVector args1 = new CacheableVector(); for (int i = 0; i < routingObj.Length; i++) { Console.WriteLine("routingObj[{0}]={1}.", i, (routingObj[i] as CacheableString).Value); args1.Add(routingObj[i]); } rc = exc.WithArgs(args1).Execute( getFuncIName, getResult); executeFunctionResult = rc.GetResult(); Console.WriteLine("on one server: result count= {0}.", executeFunctionResult.Length); List <IGFSerializable> resultList1 = new List <IGFSerializable>(); for (int pos = 0; pos < executeFunctionResult.Length; pos++) { CacheableArrayList resultItem = executeFunctionResult[pos] as CacheableArrayList; foreach (IGFSerializable item in resultItem) { resultList1.Add(item); } } for (int i = 0; i < resultList1.Count; i++) { Console.WriteLine("on one server:get:result[{0}]={1}.", i, (resultList1[i] as CacheableString).Value); } //test date independant fucntion execution on all servers // test get function with result exc = FunctionService.OnServers(cache); rc = exc.WithArgs(args1).Execute(getFuncIName, getResult); executeFunctionResult = rc.GetResult(); Console.WriteLine("on all servers: result count= {0}.", executeFunctionResult.Length); List <IGFSerializable> resultList2 = new List <IGFSerializable>(); for (int pos = 0; pos < executeFunctionResult.Length; pos++) { CacheableArrayList resultItem = executeFunctionResult[pos] as CacheableArrayList; foreach (IGFSerializable item in resultItem) { resultList2.Add(item); } } if (resultList2.Count != 34) { Console.WriteLine("result count check failed on all servers"); } for (int i = 0; i < resultList2.Count; i++) { Console.WriteLine("on all servers:result[{0}]={1}.", i, (resultList2[i] as CacheableString).Value); } //test withCollector MyResultCollector myRC = new MyResultCollector(); rc = exc.WithArgs(args1).WithCollector(myRC).Execute(getFuncIName, getResult); executeFunctionResult = rc.GetResult(); Console.WriteLine("add result count= {0}.", myRC.GetAddResultCount()); Console.WriteLine("get result count= {0}.", myRC.GetGetResultCount()); Console.WriteLine("end result count= {0}.", myRC.GetEndResultCount()); Console.WriteLine("on all servers with collector: result count= {0}.", executeFunctionResult.Length); // Close the GemFire Cache. cache.Close(); Console.WriteLine("Closed the GemFire Cache"); } // An exception should not occur catch (GemFireException gfex) { Console.WriteLine("ExecuteFunctions GemFire Exception: {0}", gfex.Message); } }