How to start Embedded Gemfire Application with SpringBoot

Gemfire is a memory caching solution for scaling software system, JavaSampleApproach will guide you how to create an Embedded Gemfire Application with SpringBoot.

Related article:
Infinispan Cache Solution | Spring Cache | Spring Boot
How to create a SpringBoot Gemfire RestfulApi

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE

II. Overview
1. Project Structure

embedded gemfire application

2. Step to do

– Create Spring Boot project
– Add needed dependencies
– Create a Gemfire Region Model
– Config Gemfire in Local model
– Implement Gemfire Repository
– Implement client for check Gemfire’s working
– Run SpringBoot project and result

III. Practices
1. Create Spring Boot project

Open Spring Tool Suite, on Menu bar, choose: File->New->Spring Starter Project, input needed project’s info:
embedded gemfire application project info

Press: Next then Finish, Spring boot project will be created

2. Add needed dependencies

– Add gemfire & spring-shell dependencies:



	4.0.0

	com.javasampleapproach.gemfire
	gemfire
	0.0.1
	jar

	SpringBootGemfire
	SpringBoot Gemfire Config

	
		org.springframework.boot
		spring-boot-starter-parent
		1.4.3.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-data-gemfire
		

		
		
		    org.springframework.shell
		    spring-shell
		    1.0.0.RELEASE
		
			

	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

	
        
            spring-releases
            http://repo.spring.io/libs-release
        
    



3. Create a Gemfire Region Model

@Region: Annotation to define the region an entity will be stored in.

package com.javasampleapproach.gemfire.model;

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

@Region("customer")
public class Customer {
	@Id
    private String firstname;
    private String lastname;
    private int age;
    
    @PersistenceConstructor
    public Customer(String firstname, String lastname, int age) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.age = age;
    }

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	
	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	
	public String toString(){
		return "firstname: " + firstname + " ,lastname: " + lastname + " ,age: " + age; 
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

4. Config Gemfire in Local model

Config Gemfire cache via @Bean: CacheFactoryBean gemfireCache()
Config Gemfire Region via @Bean: LocalRegionFactoryBean customerRegion(final GemFireCache cache)

package com.javasampleapproach.gemfire.config;

import java.util.Properties;

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 com.gemstone.gemfire.cache.GemFireCache;
import com.javasampleapproach.gemfire.model.Customer;

@Configuration
public class GemfireConfig {
    @Bean
    Properties gemfireProperties() {
        Properties gemfireProperties = new Properties();
        gemfireProperties.setProperty("name", "EmbeddedGemfireApplication");
        gemfireProperties.setProperty("mcast-port", "0");
        return gemfireProperties;
    }
    
    @Bean
    CacheFactoryBean gemfireCache() {
        CacheFactoryBean gemfireCache = new CacheFactoryBean();
        gemfireCache.setProperties(gemfireProperties());
        return gemfireCache;
    }
    
    @Bean
    LocalRegionFactoryBean customerRegion(final GemFireCache cache) {
        LocalRegionFactoryBean customerRegion = new LocalRegionFactoryBean<>();
        customerRegion.setCache(cache);
        customerRegion.setName("customer");
        customerRegion.setPersistent(false);
        return customerRegion;
    }
}

In a main class of Spring Boot, use @EnableGemfireRepositories for enable Gemfire Caching.

5. Implement Gemfire Repository

Spring Data provides an interface: CrudRepository extends Repository for implementation a repository to retrieve entities from Gemfire caching:

package com.javasampleapproach.gemfire.repository;

import org.springframework.data.repository.CrudRepository;

import com.javasampleapproach.gemfire.model.Customer;

public interface CustomerRepository extends CrudRepository {
	Customer findByFirstname(String firstname);
	Customer findByLastname(String lastname);
	Iterable findByAgeGreaterThan(int age);
	Iterable findByAgeLessThan(int age);
}

Above sourcecode, we defines 4 methods to search gemfire entities base on: firstname, lastname and age.

6. Implement client for check Gemfire’s working

For a sample client, in main class, we implements CommandLineRunner for saving some customer entities then retrieving them.

package com.javasampleapproach.gemfire;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;

import com.javasampleapproach.gemfire.model.Customer;
import com.javasampleapproach.gemfire.repository.CustomerRepository;

@EnableGemfireRepositories
@SpringBootApplication
public class SpringBootGemfireApplication implements CommandLineRunner{

	@Autowired
    CustomerRepository customerRepository;
	
	public static void main(String[] args) {
		SpringApplication.run(SpringBootGemfireApplication.class, args);
	}

	@Override
	public void run(String... arg0) throws Exception {
		Customer peter = new Customer("Peter", "Williams", 20);
		Customer mary = new Customer("Mary", "Diaz", 25);
		
		// SAVE customer to Gemfire
		customerRepository.save(peter);
		customerRepository.save(mary);
		
		// FindAll Customers
		System.out.println("-------Find All Customers-------");
		Iterable custList = customerRepository.findAll();
		for(Customer c: custList){
			System.out.println(c.toString());
		}
		
		// Find a Customer by firstname
		System.out.println("-------Find Customer by FirstName-------");
		Customer c1 = customerRepository.findByFirstname("Peter");
		System.out.println(c1.toString());
		
		// Find a Customer by lastname
		System.out.println("-------Find Customer by LastName-------");
		Customer c2 = customerRepository.findByLastname("Diaz");
		System.out.println(c2.toString());
		
		
		// find customer by age
		System.out.println("-------Find Customers have age greater than 22-------");
		Iterable custLstByAgeGreaterThan22 = customerRepository.findByAgeGreaterThan(22);
		for(Customer c: custLstByAgeGreaterThan22){
			System.out.println(c.toString());
		}
		
		
		System.out.println("-------Find Customers have age less than 30-------");
		Iterable custLstByAgeLessThan30 = customerRepository.findByAgeLessThan(30);
		for(Customer c: custLstByAgeLessThan30){
			System.out.println(c.toString());
		}
	}
}

7. Run SpringBoot project and result

Gemfire Logs:

System Properties:
    PID = 3276
    awt.toolkit = sun.awt.windows.WToolkit
    file.encoding = UTF-8
    file.encoding.pkg = sun.io
    file.separator = \
    java.awt.graphicsenv = sun.awt.Win32GraphicsEnvironment
    java.awt.headless = true
    java.awt.printerjob = sun.awt.windows.WPrinterJob
    java.class.version = 52.0
    java.endorsed.dirs = C:\Program Files\Java\jdk1.8.0_101\jre\lib\endorsed
    java.ext.dirs = C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
    java.home = C:\Program Files\Java\jdk1.8.0_101\jre
    java.io.tmpdir = C:\Users\User\AppData\Local\Temp\
    java.runtime.name = Java(TM) SE Runtime Environment
    java.runtime.version = 1.8.0_101-b13
    java.specification.name = Java Platform API Specification
    java.specification.vendor = Oracle Corporation
    java.specification.version = 1.8
    java.vendor = Oracle Corporation
    java.vendor.url = http://java.oracle.com/
    java.vendor.url.bug = http://bugreport.sun.com/bugreport/
    java.version = 1.8.0_101
    java.vm.info = mixed mode
    java.vm.name = Java HotSpot(TM) 64-Bit Server VM
    java.vm.specification.name = Java Virtual Machine Specification
    java.vm.specification.vendor = Oracle Corporation
    java.vm.specification.version = 1.8
    java.vm.vendor = Oracle Corporation
    java.vm.version = 25.101-b13
    line.separator = 

    log4j.configurationFile = jar:file:/C:/Users/User/.m2/repository/com/gemstone/gemfire/gemfire/8.2.0/gemfire-8.2.0.jar!/com/gemstone/gemfire/internal/logging/log4j/log4j2-default.xml
    org.jboss.logging.provider = slf4j
    os.version = 10.0
    p2p.useSSL = false
    path.separator = ;
    spring.beaninfo.ignore = true
    sun.arch.data.model = 64
    sun.boot.class.path = C:\Program Files\Java\jdk1.8.0_101\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_101\jre\classes
    sun.boot.library.path = C:\Program Files\Java\jdk1.8.0_101\jre\bin
    sun.cpu.endian = little
    sun.cpu.isalist = amd64
    sun.desktop = windows
    sun.io.unicode.encoding = UnicodeLittle
    sun.java.command = com.javasampleapproach.gemfire.SpringBootGemfireApplication
    sun.java.launcher = SUN_STANDARD
    sun.jnu.encoding = Cp1252
    sun.management.compiler = HotSpot 64-Bit Tiered Compilers
    sun.nio.ch.bugLevel = 
    sun.os.patch.level = 
    user.country = US
    user.language = en
    user.script = 
    user.timezone = Asia/Bangkok
    user.variant = 

Result:


-------Find All Customers-------
firstname: Mary ,lastname: Diaz ,age: 25
firstname: Peter ,lastname: Williams ,age: 20
-------Find Customer by FirstName-------
firstname: Peter ,lastname: Williams ,age: 20
-------Find Customer by LastName-------
firstname: Mary ,lastname: Diaz ,age: 25
-------Find Customers have age greater than 22-------
firstname: Mary ,lastname: Diaz ,age: 25
-------Find Customers have age less than 30-------
firstname: Mary ,lastname: Diaz ,age: 25
firstname: Peter ,lastname: Williams ,age: 20

IV. Sourcecode

SpringBootGemfire



By grokonez | December 25, 2016.

Last updated on June 4, 2017.



Related Posts


1 thought on “How to start Embedded Gemfire Application with SpringBoot”

  1. No qualifying bean of type ‘CustomerRepository’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)

Got Something To Say:

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

*