0

I am using Pivotal GemFire 9.1.1 and Spring Data GemFire 2.0.7.RELEASE.

I have a token that will be stored in a GemFire Region with a String Key and a Map<String,String> Value. The expiration of the token (i.e. entry in the GemFire Region) should be dynamic dependent on a few business scenarios.

I could find Pivotal GemFire documentation for CustomExpiry whereas I could not find any proper example/documentation on Spring Data GemFire (<gfe:custom-entry-ttl>).

Please share if there is a resource which instructs how to enable custom data expiration in Spring Data GemFire.

John Blum
  • 7,381
  • 1
  • 20
  • 30
Sudharsan
  • 207
  • 1
  • 2
  • 12

1 Answers1

0

There are actually 3 different ways a developer can configure a custom expiration policy for a Region using Pivotal GemFire's o.a.g.cache.CustomExpiry interface in SDG

Given an application-specific implementation of o.a.g.cacheCustomExpiry...

package example.app.gemfire.cache;

import org.apache.geode.cache.CustomExpiry;
import org.apache.geode.cache.ExpirationAttributes;
import ...;

class MyCustomExpiry implements CustomExpiry<String, Object> {

  ExpirationAttributes getExpiry(Region.Entry<String, Object> entry) {
    ...
  }
}

First, the XML approach.

<bean id="customTimeToLiveExpiration" 
      class="example.app.gemfire.cache.MyCustomExpiry"/>

<gfe:partitioned-region id="Example" persistent="false">
  <gfe:custom-entry-ttl ref="customTimeToLiveExpiration"/>
  <gfe:custom-entry-tti>
    <bean class="example.app.gemfire.cache.MyCustomExpiry"/>
  </gfe:custom-entry-tti>
</gfe:partitioned-region>

As you can see in the example above, you can define "custom" Expiration policies using either a bean reference, as in the nested Time-To-Live (TTL) Expiration Policy declaration or by using a anonymous bean definition, as in the nested Idle Timeout (TTI) Expiration Policy of the "Example" PARTITION Region bean definition.

Refer to the SDG XML schema for precise definitions.

Second, you can achieve the same thing Java Config...

@Configuration
class GemFireConfiguration {

  @Bean
  MyCustomExpiry customTimeToLiveExpiration() {
    return new MyCustomExpiry();
  }

  @Bean("Example")
  PartitionedRegionFactoryBean<String, Object> exampleRegion(
      GemFireCache gemfireCache) {

    PartitionedRegionFactoryBean<String, Object> exampleRegion =
      new PartitionedRegionFactoryBean<>();

    exampleRegion.setCache(gemfireCache);
    exampleRegion.setClose(false);
    exampleRegion.setPersistent(false);
    exampleRegion.setCustomEntryTimeToLive(customTimeToLiveExpiration());
    exampleRegion.setCustomEntryIdleTimeout(new MyCustomExpiry());

    return exampleRegion;
  }
}

Finally, you configure both TTL and TTI Expiration Policies using SDG Annotation-based Expiration configuration, as defined here. There is a test class along with the configuration in the SDG test suite demonstrating this capability.

Additional information about Annotation-based Expiration configuration in SDG can be found here.

Hope this helps!

-John

John Blum
  • 7,381
  • 1
  • 20
  • 30
  • Thanks for the reply John. It is clear now. I have another question. We have two independent GF cluster (8.2.7 and 9.1.1). Our use case is to configure client cache in an application using 9.1.1 gemfire jars for both the clusters. Cluster-8.2.7 will be used only for read whereas cluster-9.1.1 will be used for CRUD. is it feasible? – Sudharsan Jun 05 '18 at 13:59
  • Your welcome. GemFire 9.x cache clients cannot talk to a 8.2.x cluster (of servers). However, 8.2.2+ cache clients can talk to 8.2.x and 9.x servers. – John Blum Jun 05 '18 at 16:23
  • Ok. So it is possible to have two different client caches pointing to diff GF cluster in same application. Thanks for the confirmation John – Sudharsan Jun 05 '18 at 20:13
  • Yes, that is correct. You can configure 1 or more client Pools for the 9.x cluster and 1 or more client Pools to the other 8.x cluster. I could not tell you what gotchas to look out for, but it should work. Cheers. – John Blum Jun 05 '18 at 20:56
  • John. But the core gemfire library itself is different between 8.X and 9.X right. I mean 8.2.7 has gemfire.jar from gemstone and 9.1.1 has geode-core from apache. How could 8.2.2 client jars can work with 9.x server cache?. Just wanted to understand before attempting – Sudharsan Jun 06 '18 at 02:27
  • They built support in Pivotal GemFire 8.2.2 for clients to be able to connect to Pivotal GemFire 9.x servers. See "**What's New in Pivotal GemFire 8.2.2**", here... http://gemfire82.docs.pivotal.io/docs-gemfire/relnotes/release_notes82.html – John Blum Jun 06 '18 at 02:55
  • Ok Thanks. my gemfire cluster is using SDGF&mappingPDXSerializer and i have 3 clients who will be interacting with GF cluster. One of my client application is a EJB listener component which is not using Spring. Can i connect to GF cluster from EJB component using native GF lib?. If so, how to configure PDX serialization in client cache? – Sudharsan Jun 07 '18 at 19:29
  • Yes. But, you could also connect to the GF cluster in the EJB listener using SDG too. Up to you. However, if you are not using Spring and instead are using GemFire's public Java API directly in your EJB listener you can always still use SDG's MappingPdxSerializer simply by `ClientCache clientCache = new ClientCacheFactory(gemfireProperties).setPdxSerializer(new MappingPdxSerializer()).create();` Technically, you can register any `PdxSerializer` of your choosing, such as `o.a.g.pdx.ReflectionBasedAutoSerializer` or even a custom `PdxSerializer` impl. Again, up to you. – John Blum Jun 07 '18 at 19:35