3 Steps to Help Migrate Spring Apps to Real Time Data Grids

August 5, 2013 Bijoy Choudhury

header-graphic-set-up-spring-gemfire-v02If you are a Java or Spring developer who is keeping up with Hadoop, Storm, Spark, and YARN, then you are looking at some type of real-time data processing need—perhaps a derivative of these 20+ big data examples. As many of our customers know, Pivotal GemFire is used for some of the toughest real-time data problems in the world and can help run Spring applications 60x faster. In this article, we show you how simple it is to add GemFire to a Spring application and also provide example bits on GitHub.

Getting Started with Spring Data GemFire

The Spring Data GemFire project allows developers to use the Spring Framework and Pivotal GemFire to create and configure a distributed system for data-centric applications. In particular it allows developers to create and configure Java applications that use GemFire distributed caches and regions for high-performance enterprise data access. For more information, this article provides additional explanation about how developers can benefit from Spring and GemFire, where developers gain advantages, and what deployment models look like.

Below, we will explain how to use Spring semantics and incorporate GemFire configurations so that you can integrate a GemFire local caching strategy into a Spring Web application. After configuration, the @Cachable annotation is applied inside a POJO to cache the return value of a method. For full details of Spring caching, look at Costin Leau’s blog post on SpringSource.

We have also created a fork of the sample Spring PetClinic application to work with PivotalGemFire, and you can access it on GitHub.

Step 1: Updating Dependencies

Since the build of the Petclinic app is based on Maven, we need to add the required dependencies and repositories in the pom.xml file. Since GemFire is a commercial licensed product, it is not in the default maven repository. So, you have to specify explicitly the SpringSource Maven repository as shown below.

SpringSource Maven

Importantly, you don’t have to install the evaluation version of GemFire to run this application. The maven dependency downloads a gemfire.jar file that enables you to create caches and regions. This dependency provides a 3 node license to use GemFire but does not allow you to use locators and other GemFire specific tools, such as gfsh or Pulse. If you want to use those features, you have to separately download and install the product. Note, you can download the evaluation version of GemFire from the VMware website.

Step 2: Create the GemFire Cache Configuration

As of release 1.2.0, Spring Data GemFire’s XML namespace supports full configuration of the data grid. GemFire will continue to support its own configuration file (cache.xml), but you can now do everything in Spring XML.

To add the GemFire configurations:

A. Create a Spring XML configuration file, and add the Spring Data GemFire namespace declaration.

Spring XML configuration

B. Create the cache and the region, and optionally specify any CacheListener. You can register Cache Listeners for a region to handle data events such as when data is created, updated, or deleted. The code for the LoggingCacheListener is available here.


Step 3: Create the Cache Abstraction

The @Cacheable annotation allows methods in your application class to trigger cache population. To use GemFire as a cache implementation, simply add GemfireCacheManager to your bean configuration. Make sure you define the <cache:annotation-driven/> annotation to enable scanning your application classes for the @Cacheable annotation.

Spring Bean Configuration File:

Spring Bean Configuration File:

Now we can use this in a Java facade class:

Java facade class

Getting more information

Hopefully, you get a clear picture of how to implement Spring Data GemFire where you use GemFire as a local cache to store the return value of a method by using @Cacheable annotation. Here are some additional resources on Pivotal GemFire and the Spring Data Project:

About the Author


Should I have more than one app?
Should I have more than one app?

To be honest, I hate answering a question with a question – but the first step is to define what you want o...

Find an open source community
Find an open source community

This is the third part in my learn to program series and it deals with finding an open source community to ...

SpringOne 2021

Register Now