private void SetCacheListener(string regionName, ICacheListener <string, string> listener) { GIRegion region = CacheHelper.GetVerifyRegion <string, string>(regionName); AttributesMutator <string, string> attrMutator = region.AttributesMutator; attrMutator.SetCacheListener(listener); }
public void RunDurableClient() { // Create durable client's properties using api. Properties <string, string> durableProp = Properties <string, string> .Create <string, string>(); durableProp.Insert("durable-client-id", "DurableClientId"); durableProp.Insert("durable-timeout", "300"); // Create a Geode Cache programmatically. CacheFactory cacheFactory = CacheFactory.CreateCacheFactory(durableProp); Cache cache = cacheFactory.SetSubscriptionEnabled(true) .Create(); Console.WriteLine("Created the Geode Cache"); // Create the example Region programmatically. RegionFactory regionFactory = cache.CreateRegionFactory(RegionShortcut.CACHING_PROXY); IRegion <string, string> region = regionFactory.Create <string, string>("exampleRegion"); Console.WriteLine("Created the generic Region programmatically."); // Plugin the CacheListener with afterRegionLive. "afterRegionLive()" will be called // after all the queued events are recieved by client AttributesMutator <string, string> attrMutator = region.AttributesMutator; attrMutator.SetCacheListener(new DurableCacheListener <string, string>()); Console.WriteLine("DurableCacheListener set to region."); // For durable Clients, Register Intrest can be durable or non durable ( default ), // Unregister Interest APIs remain same. string [] keys = new string[] { "Key-1" }; region.GetSubscriptionService().RegisterKeys(keys, true, true); Console.WriteLine("Called Register Interest for Key-1 with isDurable as true"); //Send ready for Event message to Server( only for Durable Clients ). //Server will send queued events to client after recieving this. cache.ReadyForEvents(); Console.WriteLine("Sent ReadyForEvents message to server"); //wait for some time to recieve events System.Threading.Thread.Sleep(1000); // Close the Geode Cache with keepalive = true. Server will queue events for // durable registered keys and will deliver all events when client will reconnect // within timeout period and send "readyForEvents()" cache.Close(true); Console.WriteLine("Closed the Geode Cache with keepalive as true"); }
void registerClassCl2() { try { CacheHelper.DCache.TypeRegistry.RegisterTypeGeneric(DeltaEx.create); } catch (IllegalStateException) { //do nothing } IRegion<object, object> reg = CacheHelper.GetRegion<object, object>("DistRegionAck"); reg.GetSubscriptionService().RegisterRegex(".*"); AttributesMutator<object, object> attrMutator = reg.AttributesMutator; attrMutator.SetCacheListener(new SimpleCacheListener<object, object>()); }
private static void SetRegion(Region region) { m_region = region; RegionAttributes attrs = m_region.Attributes; if (attrs.ClientNotificationEnabled) { m_region.RegisterAllKeys(); } // Add cache listener callback to region if (attrs.CacheListener == null) { ExampleCacheListenerCallback listenerCallback = new ExampleCacheListenerCallback(); AttributesMutator mutator = m_region.GetAttributesMutator(); mutator.SetCacheListener(listenerCallback); } }
static void Main(string[] args) { try { // Create a Geode Cache Programmatically. CacheFactory cacheFactory = CacheFactory.CreateCacheFactory(); Cache cache = cacheFactory.SetSubscriptionEnabled(true) .Create(); Console.WriteLine("Created the Geode Cache"); RegionFactory regionFactory = cache.CreateRegionFactory(RegionShortcut.CACHING_PROXY); // Create the example Region programmatically. IRegion <string, string> region = regionFactory .SetEntryIdleTimeout(ExpirationAction.Destroy, 10) .Create <string, string>("exampleRegion"); Console.WriteLine("Created the Region with generics support programmatically."); // Plugin the SimpleCacheListener to the Region. AttributesMutator <string, string> attrMutator = region.AttributesMutator; attrMutator.SetCacheListener(new SimpleCacheListener <string, string>()); Console.WriteLine("Set the generic SimpleCacheListener on the Region"); // Put 3 Entries into the Region using the IDictionary interface. region["Key1"] = "Value1"; region["Key2"] = "Value2"; region["Key3"] = "Value3"; Console.WriteLine("Put 3 Entries into the Region"); // Get the Entry Idle Timeout specified in the Cache XML file. int entryIdleTimeout = region.Attributes.EntryIdleTimeout; Console.WriteLine("Got Entry Idle Timeout as {0} seconds", entryIdleTimeout); // Wait for half the Entry Idle Timeout duration. Thread.Sleep(entryIdleTimeout * 1000 / 2); // Get the number of Keys remaining in the Region, should be all 3. ICollection <string> keys = region.GetLocalView().Keys; Console.WriteLine("Got {0} keys before the Entry Idle Timeout duration elapsed", keys.Count); // Get 2 of the 3 Entries from the Region to "reset" their Idle Timeouts. string value1 = region["Key1"]; string value2 = region["Key2"]; Console.WriteLine("The SimpleCacheListener should next report the expiration action"); // Wait for the entire Entry Idle Timeout duration. Thread.Sleep(entryIdleTimeout * 1000); // Get the number of Keys remaining in the Region, should be 0 now. keys = region.GetLocalView().Keys; Console.WriteLine("Got {0} keys after the Entry Idle Timeout duration elapsed", keys.Count); // Close the Geode Cache. cache.Close(); Console.WriteLine("Closed the Geode Cache"); } // An exception should not occur catch (GeodeException gfex) { Console.WriteLine("DataExpiration Geode Exception: {0}", gfex.Message); } }
static void Main(string[] args) { try { // Create a Geode Cache. CacheFactory cacheFactory = CacheFactory.CreateCacheFactory(); Cache cache = cacheFactory.Set("cache-xml-file", "XMLs/clientLoaderListenerWriter.xml") .SetSubscriptionEnabled(true) .Create(); Console.WriteLine("Created the Geode Cache"); // Get the example Region from the Cache which is declared in the Cache XML file. IRegion <string, string> region = cache.GetRegion <string, string>("/exampleRegion"); Console.WriteLine("Obtained the generic Region from the Cache"); // Plugin the SimpleCacheLoader, SimpleCacheListener and SimpleCacheWrite to the Region. AttributesMutator <string, string> attrMutator = region.AttributesMutator; attrMutator.SetCacheLoader(new SimpleCacheLoader <string, string>()); attrMutator.SetCacheListener(new SimpleCacheListener <string, string>()); attrMutator.SetCacheWriter(new SimpleCacheWriter <string, string>()); Console.WriteLine("Attached the simple generic plugins on the Region"); // The following operations should cause the plugins to print the events. // Put 3 Entries into the Region using the IDictionary interface. region["Key1"] = "Value1"; region["Key2"] = "Value2"; region["Key3"] = "Value3"; Console.WriteLine("Put 3 Entries into the Region"); // Update Key3. region["Key3"] = "Value3-Updated"; // Destroy Key3 using the IDictionary interface. region.Remove("Key3"); // Invalidate Key2. region.Invalidate("Key2"); string value = null; try { // Get a new Key. value = region["Key4"]; } catch (KeyNotFoundException knfex) { Console.WriteLine("Got expected KeyNotFoundException: {0}", knfex.Message); } try { // Get a destroyed Key. value = region["Key3"]; } catch (KeyNotFoundException knfex) { Console.WriteLine("Got expected KeyNotFoundException: {0}", knfex.Message); } // Close the Geode Cache. cache.Close(); Console.WriteLine("Closed the Geode Cache"); } // An exception should not occur catch (GeodeException gfex) { Console.WriteLine("LoaderListenerWriter Geode Exception: {0}", gfex.Message); } }