Accessing Data with Spring Data GemFire

VFabric GemFire (GemFire) is a commercially licensed data management platform that provides access to data from the distributed architectures. It is available as a standalone product and as a component of VMware vFabric Suite.

Based on the Nutshell, GemFire provides an in-memory data grid that offers extremely high throughput, low latency data access, and scalability. Beyond a distributed cache, GemFire provides advanced features including:

  • Event notification
  • OQL (Object Query Language) query syntax
  • Continuous queries
  • Transaction support
  • Remote function execution
  • WAN communications
  • Efficient and portable object serialization (PDX)
  • Tools to aid system administrators in managing and configuring the GemFire distributed

This tutorial intended to provide the readers the basic idea of using GemFire for accessing the Data by reading entities from writing into memory. Before any further explanation, it’s important to know a concept that’s used when it comes to discuss the GemFire, it is Regions.

Also read:

By following this tutorial, feel free to write your comments if you had have any question or help. Also I have attached source code download at the end of this tutorial.

1. Region

A region is required to store and retrieve data from the cache. Region is an interface that extends java.util.Map to perform basic data access using familiar key/value semantics. The Region interface is wired into classes that require it, so the actual region type is decoupled from the programming model. Typically, each region is associated with one domain object, similar to a table in a relational database. Looking at the sample Message, you’ll see a defined @Region.

It’s very important for you to know that GemFire doesn’t manage the associations or enforce relational integrity among regions.

2. Java Beans (Business Domain)

As mentioned earlier, GemFire will operate against data that will be saved, retrieved and deleted from the memory. These data is identical to what already have been defined in the Spring Data/JPA as an example.



public class Message {
	private String messageId;
	private String message;

	public Message() {

	public Message(String id, String message) {
		this.messageId = id;
		this.message = message;

	public String getMessageId() {
		return messageId;

	public void setMessageId(String messageId) {
		this.messageId = messageId;

	public String getMessage() {
		return message;

	public void setMessage(String message) {
		this.message = message;

3. GemFire Repository

It’s the same concept of the spring data repository that discussed in the previous tutorials, but this time the repository is defined using the GemFire library.

package net.javabeat.springdata.repo;


import org.springframework.stereotype.Repository;

public interface MessageRepository extends CrudRepository<Message,Integer>{


4. Spring Bean Definition

It’s the Spring bean definition, that will be used for configuring the Spring context. Where in the beans, repositories, properties and several other configurable objects get defined. Here, we’re using that XML document for defining and locating the GemFire repositories. Lets look at the below configurations to know how the gemfire repository is configured in the spring configuration file.


<?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="messages" value-constraint=""/>
	<!-- Search for GemFire repositories -->
	<gfe-data:repositories base-package="net.javabeat.springdata.repo"/>

5. Spring Data + GemFire Example Application

It’s the executable Java application that contains a main implementation that does communicate directly into GemFire engine.

package net.javabeat.springdata.executable;

import java.util.ArrayList;
import java.util.List;

import net.javabeat.springdata.bean.GemFireBean;



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

	// GemFireBean declaration, it's just a normal spring bean that contains a reference for MessageRepository
	public static GemFireBean gfb;

	public static void main(String[] args) {
		// Acquiring the GemFireBean
		gfb = (GemFireBean)context.getBean("gemFireBean");
		System.out.println("Before Linking GemFire ... Initialized Message Objects");
		Message mess1 = new Message("1","Hello JavaBeat !");
		Message mess2 = new Message("2","Hello Spring Data !");
		Message mess3 = new Message("3","Hello Spring Data - GemFire !");
		Message mess4 = new Message("4","Just Hello !");
		List<Message> messages = new ArrayList<Message>();
		System.out.println("Print out all Created Messages");
		for(Message m : messages){
			System.out.println("Created Message ID :: "+m.getMessageId()+" :: With Content :: "+m.getMessage());
		System.out.println("Linking GemFire for Persisting Messages ...");
		System.out.println("Persisting Process finsihed succesfully ...");
		// Reset the messages list
		messages = null;
		System.out.println("Linking GemFire for Retrieving Messages ...");
		messages = Lists.newArrayList(gfb.getMessageRepository().findAll());
		for(Message m : messages){
			System.out.println("Retrieved Message ID :: "+m.getMessageId()+" :: With Content :: "+m.getMessage());

		System.out.println("End ::");

6. GemFire Maven Dependencies

Using of GemFire can be done either by using GemFire vFabric product that can be installed from the link mentioned above or by adding the GemFire dependency into your pom.xml. Once, you’ve added the GemFire dependency and defined the cacheFactoryBean and LocalRegionFactoryBean, the GemFire is ready for use.


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


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


		<!-- Google List API -->



			<name>Spring Snapshots</name>

			<name>Spring Snapshots</name>


7. Accessing Data By GemFire Demo

By executing the main application, you’ve got a vast amount of GemFire output in the console. Your application messages are shown below:

[info 2014/05/23 12:27:29.268 EEST  <main> tid=0x1] Initializing region PdxTypes
12:27:29.279 [main] INFO  o.s.d.g.CacheFactoryBean - Connected to Distributed System [] as Member [mohammad-amr-lt:4894] on Host [mohammad-amr-lt].
12:27:29.279 [main] INFO  o.s.d.g.CacheFactoryBean - Created new GemFire v.7.0.1 Cache [].
12:27:29.331 [main] INFO  n.j.s.e.Executable$1 - Created new cache region [messages]
12:27:29.440 [main] INFO  o.s.c.s.DefaultLifecycleProcessor - Starting beans in phase 2147483647
Before Linking GemFire ... Initialized Message Objects
Print out all Created Messages
Created Message ID :: 1 :: With Content :: Hello JavaBeat !
Created Message ID :: 2 :: With Content :: Hello Spring Data !
Created Message ID :: 3 :: With Content :: Hello Spring Data - GemFire !
Created Message ID :: 4 :: With Content :: Just Hello !
Linking GemFire for Persisting Messages ...
Persisting Process finsihed succesfully ...
Linking GemFire for Retrieving Messages ...
Retrieved Message ID :: 4 :: With Content :: Just Hello !
Retrieved Message ID :: 2 :: With Content :: Hello Spring Data !
Retrieved Message ID :: 3 :: With Content :: Hello Spring Data - GemFire !
Retrieved Message ID :: 1 :: With Content :: Hello JavaBeat !
End ::
12:27:29.557 [main] INFO  n.j.s.e.Executable - Started Executable in 7.911 seconds (JVM running for 8.36)

8. Summary

GemFire is a Spring framework that used for manipulating the data in the memory. It’s used as cache framework as you’ll be seeing in the next tutorials. In this tutorial, you’ve learnt the basic use of GemFrie by persisting a messages object inside a defined region called messages. The persisted messages, got read from printing process on the JVM console.

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>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!