Skip to content

Commit fbfef77

Browse files
committed
Polish Pivotal GemFire starter and sample.
Closes gh-5439.
1 parent 2ae1435 commit fbfef77

File tree

9 files changed

+180
-102
lines changed

9 files changed

+180
-102
lines changed

spring-boot-dependencies/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
<flyway.version>3.2.1</flyway.version>
7171
<freemarker.version>2.3.23</freemarker.version>
7272
<elasticsearch.version>1.7.5</elasticsearch.version>
73-
<gemfire.version>8.2.0</gemfire.version>
7473
<glassfish-el.version>3.0.0</glassfish-el.version>
7574
<gradle.version>1.12</gradle.version>
7675
<groovy.version>2.4.6</groovy.version>
@@ -649,17 +648,6 @@
649648
<artifactId>jackson-module-parameter-names</artifactId>
650649
<version>${jackson.version}</version>
651650
</dependency>
652-
<dependency>
653-
<groupId>com.gemstone.gemfire</groupId>
654-
<artifactId>gemfire</artifactId>
655-
<version>${gemfire.version}</version>
656-
<exclusions>
657-
<exclusion>
658-
<groupId>org.springframework.data</groupId>
659-
<artifactId>spring-data-gemfire</artifactId>
660-
</exclusion>
661-
</exclusions>
662-
</dependency>
663651
<dependency>
664652
<groupId>com.github.ben-manes.caffeine</groupId>
665653
<artifactId>caffeine</artifactId>

spring-boot-samples/spring-boot-sample-data-gemfire/pom.xml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,27 @@
88
<version>1.4.0.BUILD-SNAPSHOT</version>
99
</parent>
1010
<artifactId>spring-boot-sample-data-gemfire</artifactId>
11-
<name>Spring Boot Data Gemfire Sample</name>
12-
<description>Spring Boot Data Gemfire Sample</description>
11+
<name>Spring Boot Data GemFire Sample</name>
12+
<description>Spring Boot Data GemFire Sample</description>
1313
<url>http://projects.spring.io/spring-boot/</url>
1414
<organization>
1515
<name>Pivotal Software, Inc.</name>
1616
<url>http://www.spring.io</url>
1717
</organization>
1818
<properties>
1919
<main.basedir>${basedir}/../..</main.basedir>
20+
<spring-shell.version>1.0.0.RELEASE</spring-shell.version>
2021
</properties>
2122
<dependencies>
2223
<dependency>
2324
<groupId>org.springframework.boot</groupId>
2425
<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>
2532
</dependency>
2633
<dependency>
2734
<groupId>org.springframework.boot</groupId>
@@ -32,7 +39,20 @@
3239
<artifactId>spring-boot-starter-test</artifactId>
3340
<scope>test</scope>
3441
</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>
3548
</dependencies>
49+
<repositories>
50+
<repository>
51+
<id>spring-plugins-release</id>
52+
<name>Spring Plugins Release Maven Repository</name>
53+
<url>https://repo.spring.io/plugins-release</url>
54+
</repository>
55+
</repositories>
3656
<build>
3757
<plugins>
3858
<plugin>

spring-boot-samples/spring-boot-sample-data-gemfire/src/main/java/sample/data/gemfire/SampleDataGemFireApplication.java

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,26 @@
1616

1717
package sample.data.gemfire;
1818

19+
import java.util.Properties;
20+
21+
import org.springframework.beans.factory.annotation.Qualifier;
22+
import org.springframework.beans.factory.annotation.Value;
1923
import org.springframework.boot.SpringApplication;
2024
import org.springframework.boot.autoconfigure.SpringBootApplication;
21-
import org.springframework.context.annotation.ImportResource;
25+
import org.springframework.boot.context.properties.ConfigurationProperties;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.data.gemfire.CacheFactoryBean;
28+
import org.springframework.data.gemfire.GemfireTransactionManager;
29+
import org.springframework.data.gemfire.RegionAttributesFactoryBean;
30+
import org.springframework.data.gemfire.ReplicatedRegionFactoryBean;
2231
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
2332
import org.springframework.transaction.annotation.EnableTransactionManagement;
2433

34+
import com.gemstone.gemfire.cache.Cache;
35+
import com.gemstone.gemfire.cache.RegionAttributes;
36+
37+
import sample.data.gemfire.domain.Gemstone;
38+
2539
/**
2640
* The GemstoneAppConfiguration class for allowing Spring Boot to pick up additional
2741
* application Spring configuration meta-data for GemFire, which must be specified in
@@ -30,13 +44,71 @@
3044
* @author John Blum
3145
*/
3246
@SpringBootApplication
33-
@ImportResource("/spring-data-gemfire-cache.xml")
47+
@ConfigurationProperties
48+
//@ImportResource("/spring-data-gemfire-cache.xml")
3449
@EnableGemfireRepositories
3550
@EnableTransactionManagement
51+
@SuppressWarnings("unused")
3652
public class SampleDataGemFireApplication {
3753

54+
protected static final String GEMSTONES_REGION_NAME = "Gemstones";
55+
3856
public static void main(final String[] args) {
3957
SpringApplication.run(SampleDataGemFireApplication.class, args);
4058
}
4159

60+
@Bean
61+
Properties gemfireProperties(@Value("${sample.data.gemfire.log.level:config}") String logLevel) {
62+
Properties gemfireProperties = new Properties();
63+
64+
gemfireProperties.setProperty("name", SampleDataGemFireApplication.class.getSimpleName());
65+
gemfireProperties.setProperty("mcast-port", "0");
66+
gemfireProperties.setProperty("log-level", logLevel);
67+
68+
return gemfireProperties;
69+
}
70+
71+
@Bean
72+
CacheFactoryBean gemfireCache(@Qualifier("gemfireProperties") Properties gemfireProperties) {
73+
CacheFactoryBean gemfireCache = new CacheFactoryBean();
74+
75+
gemfireCache.setClose(true);
76+
gemfireCache.setProperties(gemfireProperties);
77+
78+
return gemfireCache;
79+
}
80+
81+
@Bean(name = GEMSTONES_REGION_NAME)
82+
ReplicatedRegionFactoryBean<Long, Gemstone> gemstonesRegion(Cache gemfireCache,
83+
RegionAttributes<Long, Gemstone> gemstonesRegionAttributes) {
84+
85+
ReplicatedRegionFactoryBean<Long, Gemstone> gemstonesRegion =
86+
new ReplicatedRegionFactoryBean<Long, Gemstone>();
87+
88+
gemstonesRegion.setAttributes(gemstonesRegionAttributes);
89+
gemstonesRegion.setClose(false);
90+
gemstonesRegion.setCache(gemfireCache);
91+
gemstonesRegion.setName(GEMSTONES_REGION_NAME);
92+
gemstonesRegion.setPersistent(false);
93+
94+
return gemstonesRegion;
95+
}
96+
97+
@Bean
98+
@SuppressWarnings("unchecked")
99+
RegionAttributesFactoryBean gemstonesRegionAttributes() {
100+
RegionAttributesFactoryBean gemstonesRegionAttributes =
101+
new RegionAttributesFactoryBean();
102+
103+
gemstonesRegionAttributes.setKeyConstraint(Long.class);
104+
gemstonesRegionAttributes.setValueConstraint(Gemstone.class);
105+
106+
return gemstonesRegionAttributes;
107+
}
108+
109+
@Bean
110+
GemfireTransactionManager gemfireTransactionManager(Cache gemfireCache) {
111+
return new GemfireTransactionManager(gemfireCache);
112+
}
113+
42114
}

spring-boot-samples/spring-boot-sample-data-gemfire/src/main/java/sample/data/gemfire/domain/Gemstone.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public int hashCode() {
8989
@Override
9090
public String toString() {
9191
return String.format("{ @type = %1$s, id = %2$d, name = %3$s }",
92-
getClass().getName(), getId(), getName());
92+
getClass().getName(), getId(), getName());
9393
}
9494

9595
}

spring-boot-samples/spring-boot-sample-data-gemfire/src/main/java/sample/data/gemfire/service/GemstoneServiceImpl.java

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.List;
22-
2322
import javax.annotation.PostConstruct;
2423

25-
import sample.data.gemfire.domain.Gemstone;
26-
2724
import org.springframework.beans.factory.annotation.Autowired;
2825
import org.springframework.stereotype.Service;
2926
import org.springframework.transaction.annotation.Transactional;
3027
import org.springframework.util.Assert;
3128

29+
import sample.data.gemfire.domain.Gemstone;
30+
3231
/**
3332
* The GemstoneServiceImpl class is a Service object implementing the GemstoneService
3433
* interface containing business logic and rules in addition to data services for
@@ -40,17 +39,17 @@
4039
public class GemstoneServiceImpl implements GemstoneService {
4140

4241
protected static final List<String> APPROVED_GEMS = new ArrayList<String>(
43-
Arrays.asList("ALEXANDRITE", "AQUAMARINE", "DIAMOND", "OPAL", "PEARL", "RUBY",
44-
"SAPPHIRE", "SPINEL", "TOPAZ"));
42+
Arrays.asList("ALEXANDRITE", "AQUAMARINE", "DIAMOND", "OPAL", "PEARL", "RUBY",
43+
"SAPPHIRE", "SPINEL", "TOPAZ"));
4544

4645
@Autowired
47-
private GemstoneRepository gemstoneRepo;
46+
@SuppressWarnings("unused")
47+
private GemstoneRepository gemstoneRepository;
4848

4949
@PostConstruct
5050
public void init() {
51-
Assert.notNull(this.gemstoneRepo,
52-
"A reference to the 'GemstoneRepository' was not properly configured!");
53-
System.out.printf("%1$s initialized!%n", getClass().getSimpleName());
51+
Assert.notNull(gemstoneRepository, "'gemstoneRepository' was not properly initialized");
52+
System.out.printf("[%1$s] initialized!%n", getClass().getSimpleName());
5453
}
5554

5655
/**
@@ -62,7 +61,7 @@ public void init() {
6261
@Override
6362
@Transactional(readOnly = true)
6463
public long count() {
65-
return this.gemstoneRepo.count();
64+
return this.gemstoneRepository.count();
6665
}
6766

6867
/**
@@ -75,8 +74,8 @@ public long count() {
7574
*/
7675
@Override
7776
@Transactional(readOnly = true)
78-
public Gemstone get(final Long id) {
79-
return this.gemstoneRepo.findOne(id);
77+
public Gemstone get(Long id) {
78+
return this.gemstoneRepository.findOne(id);
8079
}
8180

8281
/**
@@ -89,8 +88,8 @@ public Gemstone get(final Long id) {
8988
*/
9089
@Override
9190
@Transactional(readOnly = true)
92-
public Gemstone get(final String name) {
93-
return this.gemstoneRepo.findByName(name);
91+
public Gemstone get(String name) {
92+
return this.gemstoneRepository.findByName(name);
9493
}
9594

9695
/**
@@ -105,7 +104,7 @@ public Gemstone get(final String name) {
105104
@Override
106105
@Transactional(readOnly = true)
107106
public Iterable<Gemstone> list() {
108-
return this.gemstoneRepo.findAll();
107+
return this.gemstoneRepository.findAll();
109108
}
110109

111110
/**
@@ -118,37 +117,36 @@ public Iterable<Gemstone> list() {
118117
*/
119118
@Override
120119
@Transactional
121-
public Gemstone save(final Gemstone gemstone) {
120+
public Gemstone save(Gemstone gemstone) {
122121
Assert.notNull(gemstone, "The Gemstone to save must not be null!");
123122
Assert.notNull(gemstone.getName(), "The name of the Gemstone must be specified!");
124123

125-
// NOTE deliberately (naively) validate the Gemstone after mutating data access in
126-
// GemFire rather than before
127-
// to demonstrate transactions in GemFire.
128-
Gemstone savedGemstone = validate(this.gemstoneRepo.save(gemstone));
124+
// NOTE deliberately (& naively) validate the Gemstone after mutating data access in
125+
// GemFire rather than before to demonstrate transactions in GemFire.
126+
Gemstone savedGemstone = validate(this.gemstoneRepository.save(gemstone));
129127

130-
Assert.state(savedGemstone.equals(get(gemstone.getId())),
131-
String.format(
132-
"Failed to find Gemstone (%1$s) in GemFire's Cache Region 'Gemstones'!",
133-
gemstone));
128+
Assert.state(savedGemstone.equals(get(gemstone.getId())), String.format(
129+
"Failed to find Gemstone (%1$s) in GemFire's Cache Region 'Gemstones'!",
130+
gemstone));
134131

135-
System.out.printf("Saved Gemstone (%1$s)%n", savedGemstone.getName());
132+
System.out.printf("Saved Gemstone [%1$s]%n", savedGemstone.getName());
136133

137134
return gemstone;
138135
}
139136

140-
private Gemstone validate(final Gemstone gemstone) {
137+
Gemstone validate(Gemstone gemstone) {
141138
if (!APPROVED_GEMS.contains(gemstone.getName().toUpperCase())) {
142-
// NOTE if the Gemstone is not valid, blow chunks (should cause transaction to
143-
// rollback in GemFire)!
144-
System.err.printf("Illegal Gemstone (%1$s)!%n", gemstone.getName());
139+
// NOTE if the Gemstone is not valid, throw error...
140+
// Should cause transaction to rollback in GemFire!
141+
System.err.printf("Illegal Gemstone [%1$s]!%n", gemstone.getName());
145142
throw new IllegalGemstoneException(
146-
String.format("'%1$s' is not a valid Gemstone!", gemstone.getName()));
143+
String.format("[%1$s] is not a valid Gemstone!", gemstone.getName()));
147144
}
148145

149146
return gemstone;
150147
}
151148

149+
@SuppressWarnings("unused")
152150
public static final class IllegalGemstoneException extends IllegalArgumentException {
153151

154152
public IllegalGemstoneException() {
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:gfe="http://www.springframework.org/schema/gemfire" xmlns:util="http://www.springframework.org/schema/util"
3+
xmlns:gfe="http://www.springframework.org/schema/gemfire"
4+
xmlns:util="http://www.springframework.org/schema/util"
45
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
56
xsi:schemaLocation="
6-
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
7-
http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
8-
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
7+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
8+
http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
9+
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
10+
">
911

10-
<util:properties id="gemfireCacheConfigurationSettings">
11-
<prop key="name">GemstonesSpringGemFireApp</prop>
12+
<util:properties id="gemfireProperties">
13+
<prop key="name">SampleDataGemFireApplication</prop>
1214
<prop key="log-level">config</prop>
1315
<prop key="mcast-port">0</prop>
1416
</util:properties>
1517

16-
<gfe:cache properties-ref="gemfireCacheConfigurationSettings" />
18+
<gfe:cache properties-ref="gemfireProperties"/>
1719

18-
<gfe:replicated-region id="Gemstones" ignore-jta="true"
19-
persistent="false" key-constraint="java.lang.Long"
20-
value-constraint="sample.data.gemfire.domain.Gemstone" />
20+
<gfe:replicated-region id="Gemstones" persistent="false"
21+
value-constraint="sample.data.gemfire.domain.Gemstone"
22+
key-constraint="java.lang.Long"/>
2123

2224
<gfe:transaction-manager id="transactionManager"
23-
copy-on-read="true" />
25+
copy-on-read="true"/>
2426

2527
</beans>

0 commit comments

Comments
 (0)