0

I am using Pivotal GemFire 9.0.0 with 1 Locator and 1 Server. The Server has a Region called "submissions", like below -

<gfe:replicated-region id="submissionsRegion" name="submissions" 
statistics="true" template="replicateRegionTemplate">
...
</gfe:replicated-region>

I am getting Region as null when executing the following code -

Region<K, V> region = clientCache.getRegion("submissions");

Surprisingly, the same ClientCache returns all the records when I query using OQL and QueryService as shown below -

String queryString = "SELECT * FROM /submissions";
QueryService queryService = clientCache.getQueryService();
Query query = queryService.newQuery(queryString);
SelectResults results = (SelectResults) query.execute();

I am initializing my ClientCache like this -

ClientCache clientCache = new ClientCacheFactory()
            .addPoolLocator("localhost", 10479)
            .set("name", "MyClientCache")
            .set("log-level", "error")
            .create();

I am really baffled by this. Any pointer or help would be great.

John Blum
  • 7,381
  • 1
  • 20
  • 30
Vini
  • 313
  • 1
  • 7
  • 21

2 Answers2

2

You need to configure your ClientCache (either through a cache.xml or pure GemFire API) with the regions as well. Using your example:

ClientRegionFactory regionFactory = clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY);
Region region = regionFactory.create("submissions");

The ClientRegionShortcut.PROXY is used just for the sake of simplicity, you should use the shortcut that meets your needs.

The OQL works as expected because you are obtaining the QueryService through the ClientCache.getQueryService() method (instead of ClientCache.getLocalQueryService()), so the query is actually executed on Server Side.

You can get more information about how to configure the Client/Server topology in Client/Server Configuration.

Hope this helps.

Cheers.

Juan Ramos
  • 1,421
  • 1
  • 8
  • 13
  • 1
    Thanks Urizen, it worked. However, I did understand what `clientCache.getRegion("submissions")` was doing. Since, I connected to the loactor using `clientCache`, it should have fetched me the region. – Vini Jul 06 '17 at 05:30
  • Hi. Did you figure out how to solve it? I'm experiencing same behaviour. getRegion is returning null and ClientCacheFactory RegionExistsException. – korro Sep 04 '17 at 15:26
1

Yes, you need to "define" the corresponding client-side Region, matching the server-side REPLICATE Region by name (i.e. "submissions"). Actually this is a requirement independent of the server Regions' DataPolicy type (e.g. REPLICATE or PARTITION).

This is necessary since not every client wants to know about or even needs have data/events from every possible server Region. Of course, this is also configurable through subscription and "Interests Registration" (with Client/Server Event Messaging, or alternatively, CQs).

Anyway, you can completely avoid the use of the GemFire API directly or even GemFire's native cache.xml (highly recommend avoiding) by using either SDG's XML namespace...

<gfe:client-cache properties-ref="gemfireProperties" ... />

<gfe:client-region id="submissions" shortcut="PROXY"/>

Or by using Spring JavaConfig with SDG's API...

@Configuration
class GemFireConfiguration {

  Properties gemfireProperties() {

    Properties gemfireProperties = new Properties();

    gemfireProperties.setProperty("log-level", "config");
    ...

    return gemfireProperties;
  }

  @Bean
  ClientCacheFactoryBean gemfireCache() {

    ClientCacheFactoryBean gemfireCache = new ClientCacheFactoryBean();

    gemfireCache.setClose(true);
    gemfireCache.setProperties(gemfireProperties());
    ...

    return gemfireCache;
  }

  @Bean(name = "submissions");
  ClientRegionFactoryBean submissionsRegion(GemFireCache gemfireCache) {

    ClientRegionFactoryBean submissions = new ClientRegionFactoryBean();

    submissions.setCache(gemfireCache);
    submissions.setClose(false);
    submissions.setShortcut(ClientRegionShortcut.PROXY);
    ...

    return submissions;
  }

  ...
}

The "submissions" Region can be wrapped with SDG's GemfireTemplate, which will handle getting the "correct" QueryService on your behalf when running queries using the find(..) method.

Of course, you may be interested in making your client "submissions" Region a CACHING_PROXY" too. Of course, you will then need to register "interests" in the keys or data of interests. CQs are the best way to do this as it uses query criteria to define the data of "interests".

CACHING_PROXY is exactly as it sounds, caching data locally in the client based on the interests policies. This also gives you the ability to use the "local" QueryService to query data locally, avoiding the network hop.

Anyway, many options here.

Cheers, John

John Blum
  • 7,381
  • 1
  • 20
  • 30