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
    system

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.

Message.java

package net.javabeat.springdata.data;

import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.gemfire.mapping.Region;

@Region("messages")
public class Message {
	@Id
	private String messageId;
	private String message;

	public Message() {
	}

	@PersistenceConstructor
	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.

MessageReposirory.java

package net.javabeat.springdata.repo;

import net.javabeat.springdata.data.Message;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@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.

SpringContxt.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:gfe-data="http://www.springframework.org/schema/data/gemfire"
	xmlns:gfe="http://www.springframework.org/schema/gemfire"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/data/gemfire http://www.springframework.org/schema/data/gemfire/spring-data-gemfire.xsd http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd">
	<!-- Search for spring components -->
	<context:component-scan base-package="net.javabeat"></context:component-scan>
	<!-- Declare GemFire Cache -->
	<gfe:cache/>
	<!-- Local region for being used by the Message -->
	<gfe:local-region id="messages" value-constraint="net.javabeat.springdata.data.Message"/>
	<!-- Search for GemFire repositories -->
	<gfe-data:repositories base-package="net.javabeat.springdata.repo"/>
</beans>

5. Spring Data + GemFire Example Application

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

Executable.java

package net.javabeat.springdata.executable;

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

import net.javabeat.springdata.bean.GemFireBean;
import net.javabeat.springdata.data.Message;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

import com.google.common.collect.Lists;

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>();
		messages.add(mess1);
		messages.add(mess2);
		messages.add(mess3);
		messages.add(mess4);
		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 ...");
		gfb.getMessageRepository().save(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.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>net.javabeat.springdata</groupId>
	<artifactId>SpringData-GemFire</artifactId>
	<version>1.0</version>
	<packaging>jar</packaging>

	<name>Spring Data</name>
	<!-- Just for including the Spring framework necessary libraries in one shot -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.0.2.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-gemfire</artifactId>
			<version>1.3.4.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>com.gemstone.gemfire</groupId>
			<artifactId>gemfire</artifactId>
			<version>7.0.1</version>
		</dependency>
		<!-- Google List API -->
		<dependency>
			<groupId>com.google.collections</groupId>
			<artifactId>google-collections</artifactId>
			<version>1.0</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
			</plugin>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>http://repo.spring.io/libs-snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>gemstone</id>
			<url>http://dist.gemstone.com.s3.amazonaws.com/maven/release/</url>
		</repository>
	</repositories>

	<pluginRepositories>
		<pluginRepository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>http://repo.spring.io/libs-snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>

</project>

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

Comments

comments

About Amr Mohammed

Speak Your Mind

*