Skip to content

Commit 26b4a55

Browse files
committed
Add starter and sample for Apache Geode.
Closes spring-projectsgh-5438.
1 parent cfbe28f commit 26b4a55

File tree

11 files changed

+573
-0
lines changed

11 files changed

+573
-0
lines changed

spring-boot-dependencies/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<freemarker.version>2.3.23</freemarker.version>
7676
<elasticsearch.version>2.2.2</elasticsearch.version>
7777
<gemfire.version>8.2.0</gemfire.version>
78+
<geode.version>1.0.0-incubating.M2</geode.version>
7879
<glassfish-el.version>3.0.0</glassfish-el.version>
7980
<gradle.version>1.12</gradle.version>
8081
<groovy.version>2.4.6</groovy.version>
@@ -145,6 +146,7 @@
145146
<spring-amqp.version>1.6.0.M2</spring-amqp.version>
146147
<spring-cloud-connectors.version>1.2.2.RELEASE</spring-cloud-connectors.version>
147148
<spring-batch.version>3.0.7.RELEASE</spring-batch.version>
149+
<spring-data-geode.version>1.0.0.APACHE-GEODE-INCUBATING-M2</spring-data-geode.version>
148150
<spring-data-releasetrain.version>Hopper-SR1</spring-data-releasetrain.version>
149151
<spring-hateoas.version>0.19.0.RELEASE</spring-hateoas.version>
150152
<spring-integration.version>4.3.0.M2</spring-integration.version>
@@ -319,6 +321,11 @@
319321
<artifactId>spring-boot-starter-data-gemfire</artifactId>
320322
<version>1.4.0.BUILD-SNAPSHOT</version>
321323
</dependency>
324+
<dependency>
325+
<groupId>org.springframework.boot</groupId>
326+
<artifactId>spring-boot-starter-data-geode</artifactId>
327+
<version>1.4.0.BUILD-SNAPSHOT</version>
328+
</dependency>
322329
<dependency>
323330
<groupId>org.springframework.boot</groupId>
324331
<artifactId>spring-boot-starter-data-jpa</artifactId>
@@ -705,6 +712,21 @@
705712
<artifactId>gemfire</artifactId>
706713
<version>${gemfire.version}</version>
707714
</dependency>
715+
<dependency>
716+
<groupId>org.apache.geode</groupId>
717+
<artifactId>geode-core</artifactId>
718+
<version>${geode.version}</version>
719+
</dependency>
720+
<dependency>
721+
<groupId>org.apache.geode</groupId>
722+
<artifactId>geode-cq</artifactId>
723+
<version>${geode.version}</version>
724+
</dependency>
725+
<dependency>
726+
<groupId>org.apache.geode</groupId>
727+
<artifactId>geode-wan</artifactId>
728+
<version>${geode.version}</version>
729+
</dependency>
708730
<dependency>
709731
<groupId>com.github.ben-manes.caffeine</groupId>
710732
<artifactId>caffeine</artifactId>
@@ -2153,6 +2175,11 @@
21532175
<artifactId>spring-session-data-redis</artifactId>
21542176
<version>${spring-session.version}</version>
21552177
</dependency>
2178+
<dependency>
2179+
<groupId>org.springframework.shell</groupId>
2180+
<artifactId>spring-shell</artifactId>
2181+
<version>${spring-shell.version}</version>
2182+
</dependency>
21562183
<dependency>
21572184
<groupId>org.springframework.social</groupId>
21582185
<artifactId>spring-social-config</artifactId>

spring-boot-samples/pom.xml

Lines changed: 1 addition & 0 deletions
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-neo4j</module>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-configuration-processor</artifactId>
25+
<optional>true</optional>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-data-geode</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-test</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
</dependencies>
37+
<build>
38+
<plugins>
39+
<plugin>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-maven-plugin</artifactId>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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.Autowired;
22+
import org.springframework.beans.factory.annotation.Qualifier;
23+
import org.springframework.boot.SpringApplication;
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.config.SampleDataGeodeApplicationProperties;
40+
import sample.data.geode.service.Calculator;
41+
import sample.data.geode.service.factory.CalculatorFactory;
42+
43+
/**
44+
* SampleDataGeodeApplication class is a Spring Boot, Apache Geode peer cache application
45+
* using Apache Geode to perform and store mathematical calculations.
46+
*
47+
* @author John Blum
48+
* @see org.springframework.boot.SpringApplication
49+
* @see org.springframework.boot.autoconfigure.SpringBootApplication
50+
* @see org.springframework.boot.context.properties.ConfigurationProperties
51+
* @see org.springframework.context.ApplicationContext
52+
* @see org.springframework.context.annotation.Bean
53+
* @see org.springframework.data.gemfire.CacheFactoryBean
54+
* @see org.springframework.data.gemfire.GemfireTemplate
55+
* @see org.springframework.data.gemfire.PartitionedRegionFactoryBean
56+
* @see com.gemstone.gemfire.cache.Cache
57+
* @see com.gemstone.gemfire.cache.CacheLoader
58+
* @see sample.data.geode.service.Calculator
59+
* @see sample.data.geode.service.factory.CalculatorFactory
60+
* @since 1.4.0
61+
*/
62+
@SpringBootApplication
63+
@EnableConfigurationProperties(SampleDataGeodeApplicationProperties.class)
64+
@SuppressWarnings("unused")
65+
public class SampleDataGeodeApplication {
66+
67+
protected static final String CALCULATIONS_REGION_NAME = "Calculations";
68+
69+
public static void main(final String[] args) {
70+
SpringApplication.run(SampleDataGeodeApplication.class, args);
71+
}
72+
73+
@Autowired
74+
SampleDataGeodeApplicationProperties applicationProperties;
75+
76+
Properties gemfireProperties() {
77+
Properties gemfireProperties = new Properties();
78+
79+
gemfireProperties.setProperty("name", applicationName());
80+
gemfireProperties.setProperty("mcast-port", "0");
81+
gemfireProperties.setProperty("locators", "");
82+
gemfireProperties.setProperty("log-level", logLevel());
83+
84+
return gemfireProperties;
85+
}
86+
87+
String applicationName() {
88+
return SampleDataGeodeApplication.class.getSimpleName();
89+
}
90+
91+
String logLevel() {
92+
return applicationProperties.getLogLevel();
93+
}
94+
95+
@Bean
96+
CacheFactoryBean gemfireCache() {
97+
CacheFactoryBean gemfireCache = new CacheFactoryBean();
98+
99+
gemfireCache.setClose(true);
100+
gemfireCache.setProperties(gemfireProperties());
101+
102+
return gemfireCache;
103+
}
104+
105+
@Bean(name = CALCULATIONS_REGION_NAME)
106+
PartitionedRegionFactoryBean<Long, Long> calculationsRegion(Cache gemfireCache,
107+
RegionAttributes<Long, Long> calculationsRegionAttributes,
108+
@Qualifier("calculationsRegionLoader") CacheLoader<Long, Long> calculationsLoader) {
109+
110+
PartitionedRegionFactoryBean<Long, Long> calculationsRegion =
111+
new PartitionedRegionFactoryBean<Long, Long>();
112+
113+
calculationsRegion.setAttributes(calculationsRegionAttributes);
114+
calculationsRegion.setCache(gemfireCache);
115+
calculationsRegion.setCacheLoader(calculationsLoader);
116+
calculationsRegion.setClose(false);
117+
calculationsRegion.setName(CALCULATIONS_REGION_NAME);
118+
calculationsRegion.setPersistent(false);
119+
120+
return calculationsRegion;
121+
}
122+
123+
@Bean
124+
@SuppressWarnings("unchecked")
125+
RegionAttributesFactoryBean calculationsRegionAttributes() {
126+
RegionAttributesFactoryBean calculationsRegionAttributes =
127+
new RegionAttributesFactoryBean();
128+
129+
calculationsRegionAttributes.setKeyConstraint(Long.class);
130+
calculationsRegionAttributes.setKeyConstraint(Long.class);
131+
132+
return calculationsRegionAttributes;
133+
}
134+
135+
@Bean
136+
CacheLoader calculationsRegionLoader(
137+
@Qualifier("calculatorResolver") Calculator<Long, Long> calculator) {
138+
139+
return new CacheLoader<Long, Long>() {
140+
@Override
141+
public Long load(LoaderHelper<Long, Long> loaderHelper)
142+
throws CacheLoaderException {
143+
Long operand = loaderHelper.getKey();
144+
return calculator.calculate(operand);
145+
}
146+
147+
@Override public void close() {
148+
}
149+
};
150+
}
151+
152+
@Bean
153+
GemfireTemplate calculationsRegionTemplate(Cache gemfireCache) {
154+
return new GemfireTemplate(gemfireCache.getRegion(CALCULATIONS_REGION_NAME));
155+
}
156+
157+
@Bean
158+
@SuppressWarnings("unchecked")
159+
Calculator<Long, Long> calculatorResolver(ApplicationContext context) {
160+
return context.getBean(applicationProperties.getCalculator(), Calculator.class);
161+
}
162+
163+
@Bean
164+
Calculator<Long, Long> addition() {
165+
return CalculatorFactory.addition();
166+
}
167+
168+
@Bean
169+
Calculator<Long, Long> factorial() {
170+
return CalculatorFactory.factorial();
171+
}
172+
173+
@Bean
174+
Calculator<Long, Long> multiplication() {
175+
return CalculatorFactory.multiplication();
176+
}
177+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.config;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
import org.springframework.util.StringUtils;
21+
22+
/**
23+
* The SampleDataGeodeApplicationProperties class encapsulates properties used to
24+
* configure the {@link sample.data.geode.SampleDataGeodeApplication}.
25+
*
26+
* @author John Blum
27+
* @see org.springframework.boot.context.properties.ConfigurationProperties
28+
* @since 1.0.0
29+
*/
30+
@ConfigurationProperties(prefix = "sample.data.geode")
31+
@SuppressWarnings("unused")
32+
public class SampleDataGeodeApplicationProperties {
33+
34+
protected static final String DEFAULT_CALCULATOR = "factorial";
35+
protected static final String DEFAULT_LOG_LEVEL = "config";
36+
37+
private String calculator;
38+
private String logLevel;
39+
40+
protected String defaultIfEmpty(String value, String defaultValue) {
41+
return (StringUtils.hasText(value) ? value : defaultValue);
42+
}
43+
44+
public String getCalculator() {
45+
return defaultIfEmpty(calculator, DEFAULT_CALCULATOR);
46+
}
47+
48+
public void setCalculator(String calculator) {
49+
this.calculator = calculator;
50+
}
51+
52+
public String getLogLevel() {
53+
return defaultIfEmpty(logLevel, DEFAULT_LOG_LEVEL);
54+
}
55+
56+
public void setLogLevel(String logLevel) {
57+
this.logLevel = logLevel;
58+
}
59+
}
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+
}

0 commit comments

Comments
 (0)