Spring 4 + GemFire + Gradle Integration Annotation Example
December 21, 2014
This page will provide the integration of Spring 4 and GemFire which is a distributed memory oriented data management platform. GemFire gets memory from CPU, network and local disk. It has very high performance. Here in page we will create an end to end application for the demo. We need to create an entity using @Region and then create a repository class that will define methods to fetch data using OQL queries. Region plays an important role because entity is saved in the in region defined in configuration class in the bean LocalRegionFactoryBean.
Software Required to Run Example
To run the example we need the following software.1. JDK 6
2. Gradle
3. Eclipse
Project Structure in Eclipse
Find the project structure of or demo in eclipse.Gradle for Spring GemFire Boot
Find the build.gradle file that will use spring-boot-starter-data-gemfire.build.gradle
apply plugin: 'java' apply plugin: 'eclipse' archivesBaseName = 'Concretepage' version = '1.0-SNAPSHOT' repositories { maven { url "https://repo.spring.io/libs-release" } mavenLocal() mavenCentral() } dependencies { compile 'org.springframework.boot:spring-boot-starter-data-gemfire:1.2.0.RELEASE' }
Create Entity for a Region Using @Region
We need to create an entity with some properties. Here we have created an Employee entity that will be annotated with @Region. Define a region name that will be set in LocalRegionFactoryBean in configuration class.@Region: Defines a region in which entity will be stored.
@Id: Represents an identifier.
@PersistenceConstructor: This annotation is uses with entity constructor.
Find the Employee entity class with region employee.
Employee.java
package com.concretepage.gemfire.entity; import org.springframework.data.annotation.Id; import org.springframework.data.annotation.PersistenceConstructor; import org.springframework.data.gemfire.mapping.Region; @Region("Employee") public class Employee { @Id public Integer id; public String name; public Integer age; @PersistenceConstructor public Employee(Integer id, String name, Integer age){ this.id = id; this.name=name; this.age=age; } }
Repository Class with OQL queries
Find the EmployeeRepository in which we have created methods to fetch data from GemFire. Method is annotated with @Query that is used for OQL queries to define the data to be returned.EmployeeRepository.java
package com.concretepage.gemfire; import org.springframework.data.gemfire.repository.Query; import org.springframework.data.repository.CrudRepository; import com.concretepage.gemfire.entity.Employee; public interface EmployeeRepository extends CrudRepository<Employee, String> { @Query("SELECT DISTINCT e FROM /Employee e WHERE e.name = $1;") Employee getEmployeeByName(String name); @Query("SELECT e FROM /Employee e WHERE e.age > $1;") Iterable<Employee> getEmployeeByAgeGreaterThan(int age); }
GemFire Configuration Class: CacheFactoryBean, LocalRegionFactoryBean
We need to create a configuration class that will contain GemFire bean. We will also declare here repository class. In my case, this is employee repository.CacheFactoryBean: Provides Cache Manager. CacheFactoryBean returns opened cache manager and if not available, then creates new one.
LocalRegionFactoryBean: Provides the region factory. We need to specify here our region by setName or setRegionName method.
Now find the configuration class.
GemFireConfig.java
package com.concretepage.gemfire.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.gemfire.CacheFactoryBean; import org.springframework.data.gemfire.LocalRegionFactoryBean; import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories; import com.concretepage.gemfire.EmployeeRepository; import com.concretepage.gemfire.entity.Employee; import com.gemstone.gemfire.cache.GemFireCache; @Configuration @EnableGemfireRepositories(basePackages = "com.concretepage.gemfire") public class GemFireConfig { @Bean CacheFactoryBean cacheFactoryBean() { return new CacheFactoryBean(); } @Bean LocalRegionFactoryBeanlocalRegionFactory(final GemFireCache cache) { return new LocalRegionFactoryBean () { { setCache(cache); setName("Employee"); setClose(false); } }; } @Autowired EmployeeRepository employeeRepository; }
Main Class: Use Repository Bean to run Repository Methods
Find the main class. Here we have some employee that will be added to GemFire and using repository method, we will fetch the data.Main.java
package com.concretepage.gemfire; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.concretepage.gemfire.config.GemFireConfig; import com.concretepage.gemfire.entity.Employee; public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(GemFireConfig.class); ctx.refresh(); EmployeeRepository employeeRepository = ctx.getBean(EmployeeRepository.class); Employee ram = new Employee(1,"Ram",20); Employee shyam = new Employee(2,"Shyam",19); Employee mohan = new Employee(3,"Mohan",21); Employee krishn = new Employee(4,"Krishn",21); //Delete if exists already employeeRepository.deleteAll(); //Save employee employeeRepository.save(ram); employeeRepository.save(shyam); employeeRepository.save(mohan); employeeRepository.save(krishn); //Get employee By Name Employee s = employeeRepository.getEmployeeByName(ram.name); System.out.println(s.name); //Fetch all employee for the age Iterableemployees = employeeRepository.getEmployeeByAgeGreaterThan(20); System.out.println("----employee for the age greater than 20----"); for (Employee employee : employees) { System.out.println(employee.name); } } }
Ram ----employee for the age greater than 20---- Krishn Mohan