Skip to content

Commit 5fb6b64

Browse files
committed
Add starter and sample for Apache Geode.
Closes gh-5438.
1 parent b8bc4f6 commit 5fb6b64

File tree

11 files changed

+506
-0
lines changed

11 files changed

+506
-0
lines changed

spring-boot-dependencies/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
<spring-amqp.version>1.6.0.M1</spring-amqp.version>
136136
<spring-cloud-connectors.version>1.2.1.RELEASE</spring-cloud-connectors.version>
137137
<spring-batch.version>3.0.6.RELEASE</spring-batch.version>
138+
<spring-data-geode.version>1.7.0.APACHE-GEODE-EA-M1</spring-data-geode.version>
138139
<spring-data-releasetrain.version>Hopper-RC1</spring-data-releasetrain.version>
139140
<spring-hateoas.version>0.19.0.RELEASE</spring-hateoas.version>
140141
<spring-integration.version>4.3.0.M1</spring-integration.version>
@@ -303,6 +304,11 @@
303304
<artifactId>spring-boot-starter-data-gemfire</artifactId>
304305
<version>1.4.0.BUILD-SNAPSHOT</version>
305306
</dependency>
307+
<dependency>
308+
<groupId>org.springframework.boot</groupId>
309+
<artifactId>spring-boot-starter-data-geode</artifactId>
310+
<version>1.4.0.BUILD-SNAPSHOT</version>
311+
</dependency>
306312
<dependency>
307313
<groupId>org.springframework.boot</groupId>
308314
<artifactId>spring-boot-starter-data-jpa</artifactId>

spring-boot-samples/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<module>spring-boot-sample-data-couchbase</module>
3737
<module>spring-boot-sample-data-elasticsearch</module>
3838
<module>spring-boot-sample-data-gemfire</module>
39+
<module>spring-boot-sample-data-geode</module>
3940
<module>spring-boot-sample-data-jpa</module>
4041
<module>spring-boot-sample-data-mongodb</module>
4142
<module>spring-boot-sample-data-redis</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
apachegeode-maven-dependency-exclusions.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<!-- Your own application should inherit from spring-boot-starter-parent -->
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-samples</artifactId>
8+
<version>1.4.0.BUILD-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>spring-boot-sample-data-geode</artifactId>
11+
<name>Spring Boot Data Geode Sample</name>
12+
<description>Spring Boot Data Geode Sample</description>
13+
<url>http://projects.spring.io/spring-boot/</url>
14+
<organization>
15+
<name>Pivotal Software, Inc.</name>
16+
<url>http://www.spring.io</url>
17+
</organization>
18+
<properties>
19+
<main.basedir>${basedir}/../..</main.basedir>
20+
<spring-shell.version>1.1.0.RELEASE</spring-shell.version>
21+
</properties>
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter</artifactId>
26+
<exclusions>
27+
<exclusion>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-logging</artifactId>
30+
</exclusion>
31+
</exclusions>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-data-geode</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-test</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.shell</groupId>
44+
<artifactId>spring-shell</artifactId>
45+
<version>${spring-shell.version}</version>
46+
<scope>runtime</scope>
47+
</dependency>
48+
</dependencies>
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-maven-plugin</artifactId>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* Copyright 2010-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.data.geode;
18+
19+
import java.util.Properties;
20+
21+
import org.springframework.beans.factory.annotation.Qualifier;
22+
import org.springframework.beans.factory.annotation.Value;
23+
import org.springframework.boot.SpringApplication;
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
import org.springframework.boot.context.properties.ConfigurationProperties;
26+
import org.springframework.context.ApplicationContext;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.data.gemfire.CacheFactoryBean;
29+
import org.springframework.data.gemfire.GemfireTemplate;
30+
import org.springframework.data.gemfire.PartitionedRegionFactoryBean;
31+
import org.springframework.data.gemfire.RegionAttributesFactoryBean;
32+
33+
import com.gemstone.gemfire.cache.Cache;
34+
import com.gemstone.gemfire.cache.CacheLoader;
35+
import com.gemstone.gemfire.cache.CacheLoaderException;
36+
import com.gemstone.gemfire.cache.LoaderHelper;
37+
import com.gemstone.gemfire.cache.RegionAttributes;
38+
39+
import sample.data.geode.service.Calculator;
40+
import sample.data.geode.service.factory.CalculatorFactory;
41+
42+
/**
43+
* SampleDataGeodeApplication class is a Spring Boot, Apache Geode peer cache application using Geode
44+
* to perform and store mathematical calculations.
45+
*
46+
* @author John Blum
47+
* @see org.springframework.boot.SpringApplication
48+
* @see org.springframework.boot.autoconfigure.SpringBootApplication
49+
* @see org.springframework.boot.context.properties.ConfigurationProperties
50+
* @see org.springframework.context.ApplicationContext
51+
* @see org.springframework.context.annotation.Bean
52+
* @see org.springframework.data.gemfire.CacheFactoryBean
53+
* @see org.springframework.data.gemfire.GemfireTemplate
54+
* @see org.springframework.data.gemfire.PartitionedRegionFactoryBean
55+
* @see com.gemstone.gemfire.cache.Cache
56+
* @see com.gemstone.gemfire.cache.CacheLoader
57+
* @see sample.data.geode.service.Calculator
58+
* @see sample.data.geode.service.factory.CalculatorFactory
59+
* @since 1.4.0
60+
*/
61+
@SpringBootApplication
62+
@ConfigurationProperties
63+
@SuppressWarnings("unused")
64+
public class SampleDataGeodeApplication {
65+
66+
protected static final String CALCULATIONS_REGION_NAME = "Calculations";
67+
68+
public static void main(final String[] args) {
69+
SpringApplication.run(SampleDataGeodeApplication.class, args);
70+
}
71+
72+
@Bean
73+
Properties gemfireProperties(@Value("${sample.data.geode.log.level:config}") String logLevel) {
74+
Properties gemfireProperties = new Properties();
75+
76+
gemfireProperties.setProperty("name", SampleDataGeodeApplication.class.getSimpleName());
77+
gemfireProperties.setProperty("mcast-port", "0");
78+
gemfireProperties.setProperty("log-level", logLevel);
79+
80+
return gemfireProperties;
81+
}
82+
83+
@Bean
84+
CacheFactoryBean gemfireCache(@Qualifier("gemfireProperties") Properties gemfireProperties) {
85+
CacheFactoryBean gemfireCache = new CacheFactoryBean();
86+
87+
gemfireCache.setClose(true);
88+
gemfireCache.setProperties(gemfireProperties);
89+
90+
return gemfireCache;
91+
}
92+
93+
@Bean(name = CALCULATIONS_REGION_NAME)
94+
PartitionedRegionFactoryBean<Long, Long> calculationsRegion(Cache gemfireCache,
95+
RegionAttributes<Long, Long> calculationsRegionAttributes,
96+
@Qualifier("calculationsRegionLoader") CacheLoader<Long, Long> calculationsLoader) {
97+
98+
PartitionedRegionFactoryBean<Long, Long> calculationsRegion = new PartitionedRegionFactoryBean<Long, Long>();
99+
100+
calculationsRegion.setAttributes(calculationsRegionAttributes);
101+
calculationsRegion.setCache(gemfireCache);
102+
calculationsRegion.setCacheLoader(calculationsLoader);
103+
calculationsRegion.setClose(false);
104+
calculationsRegion.setName(CALCULATIONS_REGION_NAME);
105+
calculationsRegion.setPersistent(false);
106+
107+
return calculationsRegion;
108+
}
109+
110+
@Bean
111+
@SuppressWarnings("unchecked")
112+
RegionAttributesFactoryBean calculationsRegionAttributes() {
113+
RegionAttributesFactoryBean calculationsRegionAttributes = new RegionAttributesFactoryBean();
114+
115+
calculationsRegionAttributes.setKeyConstraint(Long.class);
116+
calculationsRegionAttributes.setKeyConstraint(Long.class);
117+
118+
return calculationsRegionAttributes;
119+
}
120+
121+
@Bean
122+
CacheLoader calculationsRegionLoader(@Qualifier("calculatorResolver") final Calculator<Long, Long> calculator) {
123+
return new CacheLoader<Long, Long>() {
124+
@Override public Long load(LoaderHelper<Long, Long> loaderHelper) throws CacheLoaderException {
125+
Long operand = loaderHelper.getKey();
126+
return calculator.calculate(operand);
127+
}
128+
129+
@Override public void close() {
130+
}
131+
};
132+
}
133+
134+
@Bean
135+
GemfireTemplate calculationsRegionTemplate(Cache gemfireCache) {
136+
return new GemfireTemplate(gemfireCache.getRegion(CALCULATIONS_REGION_NAME));
137+
}
138+
139+
@Bean
140+
@SuppressWarnings("unchecked")
141+
Calculator<Long, Long> calculatorResolver(ApplicationContext context,
142+
@Value("${sample.data.geode.calculator:factorial}") String beanName) {
143+
return context.getBean(beanName, Calculator.class);
144+
}
145+
146+
@Bean
147+
Calculator<Long, Long> addition() {
148+
return CalculatorFactory.addition();
149+
}
150+
151+
@Bean
152+
Calculator<Long, Long> factorial() {
153+
return CalculatorFactory.factorial();
154+
}
155+
156+
@Bean
157+
Calculator<Long, Long> multiplication() {
158+
return CalculatorFactory.multiplication();
159+
}
160+
161+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2010-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.data.geode.service;
18+
19+
/**
20+
* The Calculator interface represents a mathematical computation of an operand with itself.
21+
*
22+
* @author John Blum
23+
* @since 1.4.0
24+
*/
25+
public interface Calculator<S, T> {
26+
27+
/**
28+
* Calculates the value of the operand with itself.
29+
*
30+
* @param operand the operand to use in the calculation.
31+
* @return the result of the operand calculated with itself.
32+
*/
33+
T calculate(S operand);
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2010-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.data.geode.service.factory;
18+
19+
import org.springframework.util.Assert;
20+
21+
import sample.data.geode.service.Calculator;
22+
23+
/**
24+
* CalculatorFactory is an abstract factory class that constructs different instances of {@link Calculator} interface
25+
* to compute the mathematical operation of an operand applied to itself. In other words, given an operand (n),
26+
* then f(n) = some calculation on (n).
27+
*
28+
* For example, addition(n) = n + n = 2n, multiplication(n) = n * n = n**2 and factorial(n) = n!, etc.
29+
*
30+
* @author John Blum
31+
* @see sample.data.geode.service.Calculator
32+
* @since 1.4.0
33+
*/
34+
@SuppressWarnings("unused")
35+
public abstract class CalculatorFactory {
36+
37+
/* (non-Javadoc) */
38+
public static Calculator<Long, Long> addition() {
39+
return new Calculator<Long, Long>() {
40+
@Override
41+
public Long calculate(Long operand) {
42+
Assert.notNull(operand, "operand must not be null");
43+
return (operand + operand);
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return "addition";
49+
}
50+
};
51+
}
52+
53+
/* (non-Javadoc) */
54+
public static Calculator<Long, Long> factorial() {
55+
return new Calculator<Long, Long>() {
56+
@Override
57+
public Long calculate(Long operand) {
58+
Assert.notNull(operand, "operand must not be null");
59+
Assert.isTrue(operand > -1, String.format("operand [%1$s] must be greater than equal to 0", operand));
60+
61+
if (operand <= 2l) {
62+
return (operand < 2l ? 1l : 2l);
63+
}
64+
65+
long result = operand;
66+
67+
while (--operand > 1) {
68+
result *= operand;
69+
}
70+
71+
return result;
72+
}
73+
74+
@Override
75+
public String toString() {
76+
return "factorial";
77+
}
78+
};
79+
}
80+
81+
/* (non-Javadoc) */
82+
public static Calculator<Long, Long> multiplication() {
83+
return new Calculator<Long, Long>() {
84+
@Override
85+
public Long calculate(Long operand) {
86+
Assert.notNull(operand, "operand must not be null");
87+
return (operand * operand);
88+
}
89+
90+
@Override
91+
public String toString() {
92+
return "multiplication";
93+
}
94+
};
95+
}
96+
97+
}

0 commit comments

Comments
 (0)