Caching Data using Spring Data GemFire

In my previous tutorial, I have explained about the accessing data using Spring Data GemFire. GemFire is the in-memory data system used in the distributed environments. The performance and scalability achieved by GemFire by using the efficient caching techniques for storing in the in-memory. In this tutorial I will explain the purpose, in that connections will be made for for transferring RSS Feed for different counties. After that the elapsed time should be computed by subtract of the end time from the start time.

Also read:

1. Business Scenario

To ensure that the Spring GemFire framework does provide a proper solution for the Cache, we created a MoneyRSS spring bean that contains a loadRss service that accept a country as an enum value. After that an HTTP connection got opened for reading the rss for the provided country. The implementation of the whole bean is provided below:



import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

public class MoneyRSS {
	public Object loadRss(Country country){
		try {
			URL url = new URL(""+country.code+"/rss.xml");
			try {
				return url.openConnection().getContent();
			} catch (IOException e) {
		} catch (MalformedURLException e) {
		return null;

	public static enum Country {
		INDIA("INDIA","INR"), UK("United Kingdom","GBP"), USA("United States","USD");

		Country(String name, String code){
			this.code = code;

		private String code;
		private String name;

		public String getCode() {
			return code;

		public void setCode(String code) {
			this.code = code;

		public String getName() {
			return name;

		public void setName(String name) { = name;


2. Spring Bean Configurations

It’s the spring container configuration file that used for defining spring beans, repositories if there is one, GemFire cache, Regions, cache managers and etc.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
	xmlns:xsi="" xmlns:context=""
	<!-- Search for spring components -->
	<context:component-scan base-package="net.javabeat"></context:component-scan>
	<!-- Declare GemFire Cache -->
	<!-- Local region for being used by the Message -->
	<gfe:local-region id="moneyRss"/>
	<!-- It's mandatory for enabling the Cache -->
	<bean id="cacheManager" class="" p:cache-ref="gemfireCache"/>
	<!-- Dummy Bean for EnableCaching -->
	<bean id="enableCachingBean" class="net.javabeat.springdata.executable.Executable"></bean>

3. Maven Build File

It’s the maven build file that contains the required libraries for getting the example up and running.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


	<name>Spring Data</name>
	<!-- Just for including the Spring framework necessary libraries inone shot -->


		<!-- Google List API -->



			<name>Spring Snapshots</name>

			<name>Spring Snapshots</name>


4. Spring Data GemFire Example Application

It’s the Spring Data GemFire Sample Application that will be used for starting up the Spring framework and for initiating an rss HTTP request through using of GemFireBean.

package net.javabeat.springdata.executable;

import net.javabeat.springdata.bean.GemFireBean;

import org.springframework.cache.annotation.EnableCaching;

public class Executable {
	// Getting spring context
	public static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new ClassPathResource(&quot;SpringContext.xml&quot;).getPath());

	// GemFireBean declaration, it's just a normal spring bean
	public static GemFireBean gfb;

	public static void main(String[] args) {
		// Acquiring the GemFireBean
		gfb = (GemFireBean)context.getBean(&quot;gemFireBean&quot;);

	private static void loadAndTimeIt(int tryIndex,GemFireBean bean,MoneyRSS.Country country){
		// Start Time
		long start = System.currentTimeMillis();
		// Load document
		Object o = gfb.getMoneyRss().loadRss(country);
		// End Time Computation
		long elapsed = System.currentTimeMillis() - start;
		// Print out the time message
        System.out.println("Try # :: "+tryIndex+" :: For :: "+country+" :: Found " + o + ", and it only took " + elapsed + " ms to find out!\n");

5. GemFire Cache Demo

It’s the demonstration of using GemFire for caching the request. This output shows the how much time taken for each request. If you look at it closely, for the first look up of the data, it took longer time, but later requests it is much faster. It shows that if you have a performance issues for fetching the data, GemFire could be good option to try out.

[info 2014/05/24 01:00:24.765 EEST  &lt;main&gt; tid=0x1] Defaulting license-working-dir to current working directory &quot;D:\Krishna\EclipseLink\Workspace\SpringData-GemFire-Cache&quot;.

[info 2014/05/24 01:00:24.775 EEST  &lt;main&gt; tid=0x1] Acquiring default evaluation license.

[info 2014/05/24 01:00:26.554 EEST  &lt;main&gt; tid=0x1] vFabric Licensing Client activation of license required 1778 milliseconds.

[info 2014/05/24 01:00:26.618 EEST  &lt;main&gt; tid=0x1] Using the default evaluation license.

[info 2014/05/24 01:00:26.619 EEST  &lt;main&gt; tid=0x1] Licensing required 1856 milliseconds.

[info 2014/05/24 01:00:26.758 EEST  &lt;main&gt; tid=0x1] GemFire P2P Listener started on  tcp:///

[info 2014/05/24 01:00:30.966 EEST  &lt;main&gt; tid=0x1] Membership: lead member is now mohammad-amr-lt:49779

[config 2014/05/24 01:00:30.974 EEST  &lt;main&gt; tid=0x1] This member, mohammad-amr-lt:49779, is becoming group coordinator.

[info 2014/05/24 01:00:31.011 EEST  &lt;main&gt; tid=0x1] Entered into membership in group GF70 with ID mohammad-amr-lt:49779.

[info 2014/05/24 01:00:31.013 EEST  &lt;main&gt; tid=0x1] Starting DistributionManager mohammad-amr-lt:49779.

[info 2014/05/24 01:00:31.014 EEST  &lt;main&gt; tid=0x1] Initial (membershipManager) view =  [mohammad-amr-lt:49779{lead}]

[info 2014/05/24 01:00:31.015 EEST  &lt;main&gt; tid=0x1] Admitting member &lt;mohammad-amr-lt:49779&gt;. Now there are 1 non-admin member(s).

[info 2014/05/24 01:00:31.015 EEST  &lt;main&gt; tid=0x1] mohammad-amr-lt:49779 is the elder and the only member.

[info 2014/05/24 01:00:31.130 EEST  &lt;main&gt; tid=0x1] Did not hear back from any other system. I am the first one.

[info 2014/05/24 01:00:31.130 EEST  &lt;main&gt; tid=0x1] DistributionManager mohammad-amr-lt:49779 started on[10334]. There were 0 other DMs. others: []

[info 2014/05/24 01:00:31.153 EEST  &lt;Thread-3 StatSampler&gt; tid=0x21] Disabling statistic archival.

[info 2014/05/24 01:00:31.394 EEST  &lt;main&gt; tid=0x1] Initializing region _monitoringRegion_mohammad-amr-lt49779

[config 2014/05/24 01:00:31.422 EEST  &lt;main&gt; tid=0x1] Command Service could not be initialized. Could not find Spring Shell library which is needed for CLI/gfsh in classpath. Internal support for CLI &amp; gfsh is not enabled.

[info 2014/05/24 01:00:31.477 EEST  &lt;main&gt; tid=0x1] Initializing region PdxTypes
Try # :: 1 :: For :: INDIA :: Found$HttpInputStream@629a99eb, and it only took 741 ms to find out!

Try # :: 2 :: For :: INDIA :: Found$HttpInputStream@629a99eb, and it only took 3 ms to find out!

Try # :: 3 :: For :: INDIA :: Found$HttpInputStream@629a99eb, and it only took 0 ms to find out!

Try # :: 1 :: For :: USA :: Found$HttpInputStream@2af4ca49, and it only took 685 ms to find out!

Try # :: 2 :: For :: USA :: Found$HttpInputStream@2af4ca49, and it only took 1 ms to find out!

Try # :: 3 :: For :: USA :: Found$HttpInputStream@2af4ca49, and it only took 0 ms to find out!

Try # :: 1 :: For :: UK :: Found$HttpInputStream@64ad97f5, and it only took 599 ms to find out!

Try # :: 2 :: For :: UK :: Found$HttpInputStream@64ad97f5, and it only took 1 ms to find out!

Try # :: 3 :: For :: UK :: Found$HttpInputStream@64ad97f5, and it only took 0 ms to find out!

[info 2014/05/24 01:00:33.729 EEST  &lt;Distributed system shutdown hook&gt; tid=0xa] VM is exiting - shutting down distributed system

6. Summary

You’ve just developed an application that uses the GemFire cache feature. The example provided here makes multiple requests for getting the RSS money feeds for different countries, where the first request for every single country took long time in comparison of those second and third.

Download Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>