If 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.
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.
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.
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
@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
Spring Bean Configuration File:
Now we can use this in a 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:
- Pivotal’s website has more information and resources on GemFire
- VMware’s website has additional resources on GemFire
- The vFabric Blog has over 50 articles on GemFire, ranging from mainframe modernization to active/active WAN-based replication to map-reduce style querying in real time.
- An overview of using Spring Data GemFire with Pivotal GemFire
- SpringSource’s Spring Data Project
About the AuthorMore Content by Bijoy Choudhury