BAEL-750 Added Java configuration (#1494)

* BAEL-750 A guide to gemfire with spring data

* BAEL-750 A guide to gemfire with spring data

* BAEL-750 A guide to gemfire with spring data
This commit is contained in:
baljeet20 2017-03-26 01:53:04 +05:30 committed by maibin
parent e62f8fa3d8
commit de29cd5632
9 changed files with 374 additions and 1 deletions

View File

@ -211,7 +211,8 @@
<module>rabbitmq</module>
<module>vertx</module>
</modules>
<module>spring-data-gemfire</module>
</modules>
<build>
<plugins>
<plugin>

View File

@ -0,0 +1,97 @@
<?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>com.baeldung</groupId>
<artifactId>spring-data-gemfire</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<spring-data-gemfire-version>1.7.4.RELEASE</spring-data-gemfire-version>
<gemfire-version>7.0.1</gemfire-version>
<google-collections-version>1.0</google-collections-version>
<junit-version>4.12</junit-version>
<hamcrest-library-version>1.3</hamcrest-library-version>
<spring-context-version>4.3.0.RELEASE</spring-context-version>
<spring-test-version>4.3.0.RELEASE</spring-test-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-gemfire</artifactId>
<version>${spring-data-gemfire-version}</version>
</dependency>
<dependency>
<groupId>com.gemstone.gemfire</groupId>
<artifactId>gemfire</artifactId>
<version>${gemfire-version}</version>
</dependency>
<!-- Google List API -->
<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
<version>${google-collections-version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${hamcrest-library-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-context-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-test-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</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>
</project>

View File

@ -0,0 +1,16 @@
package com.baeldung.spring.data.gemfire.function;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.gemfire.function.annotation.FunctionId;
import org.springframework.data.gemfire.function.annotation.OnRegion;
import org.springframework.data.gemfire.function.annotation.RegionData;
import java.util.Map;
import java.util.Set;
@OnRegion(region="employee")
public interface FunctionExecution {
@FunctionId("greeting")
public void execute(String message);
}

View File

@ -0,0 +1,18 @@
package com.baeldung.spring.data.gemfire.function;
import org.springframework.data.gemfire.function.annotation.GemfireFunction;
import org.springframework.stereotype.Component;
@Component
public class FunctionImpl {
@GemfireFunction
public void greeting(String message){
System.out.println("Message "+message);
}
@GemfireFunction
public String sayHello(String message){
return "Hello "+message;
}
}

View File

@ -0,0 +1,65 @@
package com.baeldung.spring.data.gemfire.function;
import com.baeldung.spring.data.gemfire.model.Employee;
import com.baeldung.spring.data.gemfire.repository.EmployeeRepository;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.GemFireCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.gemfire.CacheFactoryBean;
import org.springframework.data.gemfire.LocalRegionFactoryBean;
import org.springframework.data.gemfire.function.config.EnableGemfireFunctionExecutions;
import org.springframework.data.gemfire.function.config.EnableGemfireFunctions;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
import java.util.Properties;
@Configuration
@ComponentScan
@EnableGemfireRepositories(basePackages = "com.baeldung.spring.data.gemfire.repository")
@EnableGemfireFunctions
@EnableGemfireFunctionExecutions(basePackages = "com.baeldung.spring.data.gemfire.function")
public class GemfireConfiguration {
@Autowired
EmployeeRepository employeeRepository;
@Autowired
FunctionExecution functionExecution;
@Bean
Properties gemfireProperties() {
Properties gemfireProperties = new Properties();
gemfireProperties.setProperty("name", "SpringDataGemFireApplication");
gemfireProperties.setProperty("mcast-port", "0");
gemfireProperties.setProperty("log-level", "config");
return gemfireProperties;
}
@Bean
@Autowired
CacheFactoryBean gemfireCache() {
CacheFactoryBean gemfireCache = new CacheFactoryBean();
gemfireCache.setClose(true);
gemfireCache.setProperties(gemfireProperties());
return gemfireCache;
}
@Bean(name="employee")
@Autowired
LocalRegionFactoryBean<String, Employee> getEmployee(final GemFireCache cache) {
LocalRegionFactoryBean<String, Employee> employeeRegion = new LocalRegionFactoryBean<String, Employee>();
employeeRegion.setCache(cache);
employeeRegion.setClose(false);
employeeRegion.setName("employee");
employeeRegion.setPersistent(false);
employeeRegion.setDataPolicy(DataPolicy.PRELOADED);
return employeeRegion;
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.spring.data.gemfire.model;
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 String name;
public double salary;
@PersistenceConstructor
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return name + " is " + salary + " years old.";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.spring.data.gemfire.repository;
import com.baeldung.spring.data.gemfire.model.Employee;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EmployeeRepository extends CrudRepository<Employee, String> {
Employee findByName(String name);
Iterable<Employee> findBySalaryGreaterThan(double salary);
Iterable<Employee> findBySalaryLessThan(double salary);
Iterable<Employee> findBySalaryGreaterThanAndSalaryLessThan(double salary1, double salary2);
}

View File

@ -0,0 +1,20 @@
<?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">
<context:component-scan base-package="com.baeldung.spring.data.gemfire"></context:component-scan>
<gfe:annotation-driven/>
<gfe-data:function-executions base-package="com.baeldung.spring.data.gemfire.function"/>
<!-- Declare GemFire Cache -->
<gfe:cache/>
<!-- Local region for being used by the Message -->
<gfe:local-region id="employee" value-constraint="com.baeldung.spring.data.gemfire.model.Employee" data-policy="PRELOADED"/>
<!-- Search for GemFire repositories -->
<gfe-data:repositories base-package="com.baeldung.spring.data.gemfire.repository"/>
</beans>

View File

@ -0,0 +1,98 @@
package com.baeldung.spring.data.gemfire.repository;
import com.baeldung.spring.data.gemfire.function.FunctionExecution;
import com.baeldung.spring.data.gemfire.function.GemfireConfiguration;
import com.baeldung.spring.data.gemfire.model.Employee;
import com.google.common.collect.Lists;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import java.util.List;
import static junit.framework.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=GemfireConfiguration.class, loader=AnnotationConfigContextLoader.class)
public class EmployeeRepositoryTest {
@Autowired
EmployeeRepository employeeRepository;
@Autowired
FunctionExecution execution;
@Test
public void whenEmployeeIsSaved_ThenAbleToRead(){
Employee employee=new Employee("John Davidson",4550.00);
employeeRepository.save(employee);
List<Employee> employees= Lists.newArrayList(employeeRepository.findAll());
assertEquals(1, employees.size());
}
@Test
public void whenSalaryGreaterThan_ThenEmployeeFound(){
Employee employee=new Employee("John Davidson",4550.00);
Employee employee1=new Employee("Adam Davidson",3500.00);
Employee employee2=new Employee("Chris Davidson",5600.00);
employeeRepository.save(employee);
employeeRepository.save(employee1);
employeeRepository.save(employee2);
List<Employee> employees= Lists.newArrayList(employeeRepository.findBySalaryGreaterThan(4000.00));
assertEquals(2,employees.size());
}
@Test
public void whenSalaryLessThan_ThenEmployeeFound(){
Employee employee=new Employee("John Davidson",4550.00);
Employee employee1=new Employee("Adam Davidson",3500.00);
Employee employee2=new Employee("Chris Davidson",5600.00);
employeeRepository.save(employee);
employeeRepository.save(employee1);
employeeRepository.save(employee2);
List<Employee> employees= Lists.newArrayList(employeeRepository.findBySalaryLessThan(4000));
assertEquals(1,employees.size());
}
@Test
public void whenSalaryBetween_ThenEmployeeFound(){
Employee employee=new Employee("John Davidson",4550.00);
Employee employee1=new Employee("Adam Davidson",3500.00);
Employee employee2=new Employee("Chris Davidson",5600.00);
employeeRepository.save(employee);
employeeRepository.save(employee1);
employeeRepository.save(employee2);
List<Employee> employees= Lists.newArrayList(employeeRepository.findBySalaryGreaterThanAndSalaryLessThan(3500,5000));
assertEquals(1,employees.size());
}
@Test
public void whenFunctionExecutedFromClient_ThenFunctionExecutedOnServer(){
execution.execute("Hello World");
}
@After
public void cleanup(){
employeeRepository.deleteAll();
}
}