Hi Paul- Yes, the later...
> Or may be there is another solution I completely missed? A Geode cache instance is a "Singleton" in the Geode JVM process (technically, by ClassLoader, however, you only ever create 1 instance per JVM). So... You can use the CacheFactory.getAnyInstance() <http://data-docs-samples.cfapps.io/docs-gemfire/latest/javadocs/japi/com/gemstone/gemfire/cache/CacheFactory.html#getAnyInstance()> [1] to return the "single" instance of a Geode "peer" cache (server-side) and the ClientCacheFactory.getAnyInstance() <http://data-docs-samples.cfapps.io/docs-gemfire/latest/javadocs/japi/com/gemstone/gemfire/cache/client/ClientCacheFactory.html#getAnyInstance()> [2] to get the "single" instance of a Geode "client" cache. Typically, you always create Geode cache applications that are 1 or the other... i.e. either a peer (embedded) cache application (i.e. as in an actual peer member in the cluster (distributed system)) or more typically, as a cache client application. However, if you really don't know, then you can inspect your cache instance using logic similar to this <https://github.com/spring-projects/spring-data-gemfire/blob/1.9.0.RC1/src/main/java/org/springframework/data/gemfire/util/CacheUtils.java#L54-L62> [3] (for a client), and this <https://github.com/spring-projects/spring-data-gemfire/blob/1.9.0.RC1/src/main/java/org/springframework/data/gemfire/util/CacheUtils.java#L77-L85> [4] (for a peer), and/or even combine this logic in some way, as needed, since both *Cache <http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/Cache.html>* [5] (peer) and *ClientCache <http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/client/ClientCache.html>* [6] (client) extends *GemFireCache <http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/GemFireCache.html>* [7]. The reason why the logic in [3] & [4] is more elaborate then just the instanceof check is because there is only 1 implementation of all these interfaces, GemFireCacheImpl <https://github.com/apache/geode/blob/rel/v1.0.0-incubating/geode-core/src/main/java/org/apache/geode/internal/cache/GemFireCacheImpl.java#L241> [8]. In some Geode callback APIs, they give you a reference to the *Region*. >From that, you can get the cache instance. For example.. AsyncEventListener <http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/asyncqueue/AsyncEventListener.html> [9], processEvents(:List<AsyncEvent <http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/asyncqueue/AsyncEvent.html> >) [10], which extends GatewayQueueEvent <http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/wan/GatewayQueueEvent.html> [11], which as the method getRegion():Region<K, V> <http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/wan/GatewayQueueEvent.html#getRegion--> [12], and the *Region* interface has the method, getRegionService() <http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/Region.html#getRegionService--> [13], which is exactly what the cache instance is (GemFireCache <http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/GemFireCache.html> [7] implements the RegionService <http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/RegionService.html> interface [14], and therefore can be cast accordingly. Anyway, there are typically multiple ways to get a reference to the cache instance. In *Spring*, it is much more elegant since I can inject/auto-wire a reference to the cache instance in my AsyncEventListener implementation (defined as a bean in the *Spring* context). However, you must be very careful what Geode objects you inject into a Geode callback, like AsyncEventListener, since you could create a circular reference (*Region* A -> AEQ -> Listener -> *Region* (?), which is decidedly bad, particularly if ? == A. Anyway, hope this helps. Cheers, John [1] http://data-docs-samples.cfapps.io/docs-gemfire/latest/javadocs/japi/com/gemstone/gemfire/cache/CacheFactory.html#getAnyInstance() [2] http://data-docs-samples.cfapps.io/docs-gemfire/latest/javadocs/japi/com/gemstone/gemfire/cache/client/ClientCacheFactory.html#getAnyInstance() [3] https://github.com/spring-projects/spring-data-gemfire/blob/1.9.0.RC1/src/main/java/org/springframework/data/gemfire/util/CacheUtils.java#L54-L62 [4] https://github.com/spring-projects/spring-data-gemfire/blob/1.9.0.RC1/src/main/java/org/springframework/data/gemfire/util/CacheUtils.java#L77-L85 [5] http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/Cache.html [6] http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/client/ClientCache.html [7] http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/GemFireCache.html [8] https://github.com/apache/geode/blob/rel/v1.0.0-incubating/geode-core/src/main/java/org/apache/geode/internal/cache/GemFireCacheImpl.java#L241 [9] http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/asyncqueue/AsyncEventListener.html [10] http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/asyncqueue/AsyncEvent.html [11] http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/wan/GatewayQueueEvent.html [12] http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/wan/GatewayQueueEvent.html#getRegion-- [13] http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/Region.html#getRegionService-- [14] http://geode.apache.org/releases/latest/javadoc/org/apache/geode/cache/RegionService.html On Sun, Jan 22, 2017 at 3:45 AM, Paul Perez <[email protected]> wrote: > Hello All, > > > > I hope you have a good weekend. > > I have some questions regarding the code to write in a Listener such as an > AsyncEventListener to get a reference to a cache when the listener is > deployed in a server. > > > > I tried to deployed a simple AsyncEventlistener on a server. So I Create a > event queue from gfsh > > > > *Create an Asynchronous Event Queue: *create async-event-queue --id=myAEQ > --listener=pymma.testgeode01.MyFirstAsynchronousEventListener > --parallel=true > > *Create a region* :create region --name=customerRegion > --type=PARTITION --group=groupA --async-event-queue-id=myAEQ > > > > In the example given in the Java doc http://geode.apache.org/ > releases/latest/javadoc/index.html the access to the cache is provided > by a CacheHelper: > > final Region duplicateRegion = > CacheHelper.getCache().getRegion(originalRegionName > + "_DUP"); > > > > Since I did not find any reference to what a Cache Helper is and from > where it comes from, at the first glance, in my current POC, I created > my own cache helper with the static method getCache. > > public static Cache getCache () { > > Properties properties = new Properties(); > > properties.setProperty("locators","localhost[10334]"); > > Cache cache = new CacheFactory(properties).create(); > > return cache; > > } > > It could be also a Clientcache. > > In my example I duplicated the entries from customerRegion to another > region and it works well. > > > > Nevertheless, since the Listener is deployed in a server itself, I though > that a reference to the cache could be given to the event by a more direct > way. The interface AsyncEventListener is outside the scope of this topic. I > tried to search if there was not a special class CacheHelper and I found > one similar named CacheRegionHelper. But without documentation in the Java > API I did not want to use it. > > > > So if Geode community has few minutes to reply, I would like to ask these > questions: > > > > In the example provided in the API, what is The CacheHelper? Is it a class > provided in Geode Libraries or do we need to develop by ourself. > > In that case, Do we consider the CacheHelper as a regular client and use > the cache factory to create it?. > > Or does it exist a direct way to access to a cache reference through the > Server the listener is deploy in? > > > > Or may be there is another solution I completely missed? > > > > Thank you for your help. > > > > Best regards > > > > Paul > > > -- -John john.blum10101 (skype)
