Skip to content

Commit fa0021c

Browse files
Patrick Johnsonmp911de
Patrick Johnson
authored andcommitted
#539 - Add spring-data-geode-examples module.
1 parent 08dce4f commit fa0021c

File tree

151 files changed

+6712
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+6712
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ target/
77
#IntelliJ Stuff
88
.idea
99
*.iml
10+
11+
#Geode Stuff
12+
*.log
13+
*.dat

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ This repository contains example projects for the different Spring Data modules
66

77
We have separate folders for the samples of individual modules:
88

9+
## Spring Data for Apache Geode
10+
11+
* `events` - In this example the test will make use of event handlers and async event queue to handle events.
12+
* `expiration-eviction` - In these examples the server is configured to delete entries after a certain idle period or after a Time-To-Live period (expiration0 or remove data from memory when certain thresholds are reached (eviction).
13+
* `function-invocation` - In this example the server will have 3 functions registered. The client will invoke each of the functions.
14+
* `queries` - In this example a client will query the data in various ways using OQl, continuous queries, and Apache Lucene indexes.
15+
* `security` - In this example the servers and clients are set up with security (username/password) authentication using Geode Security and Apache Shiro.
16+
* `storage` - In this example the server is configured to store data off of hte JVM heap using the `@EnableOffHeap` annotation and to compress region data using SnappyCompressor`.
17+
* `transactions` - In this example the client will perform operations within a transaction. First, it will do a successful transaction where entries are saved to the server, and then a failed transaction where all changes are reverted.
18+
* `wan` - In these example two servers are deployed. One server populates itself with data, and the other server gets populated with that data via WAN replication.
19+
920
## Spring Data JPA
1021

1122
* `eclipselink` - Sample project to show how to use Spring Data JPA with Spring Boot and [Eclipselink](https://www.eclipse.org/eclipselink/).

geode/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Spring Data For GemFire and Apache Geode Examples
2+
=========================================================
3+
4+
This project provides a number of examples to get you started using Spring Data for Apache Geode or Pivotal GemFire. These examples are designed to work with [Spring Data for Pivotal GemFire](http://projects.spring.io/spring-data-gemfire) 2.0.9-RELEASE or higher and are organized into the following sub projects:
5+
6+
It is important to note that all examples will follow the prescribed Maven directory structure.
7+
8+
Examples:
9+
10+
* **events** - In this example the test will make use of event handlers and async event queue to handle events.
11+
* **expiration-eviction** - In these examples the server is configured to delete entries after a certain idle period or after a Time-To-Live period (expiration0 or remove data from memory when certain thresholds are reached (eviction).
12+
* **function-invocation** - In this example the server will have 3 functions registered. The client will invoke each of the functions.
13+
* **queries** - In this example a client will query the data in various ways using OQl, continuous queries, and Apache Lucene indexes.
14+
* **security** - In this example the servers and clients are set up with security (username/password) authentication using Geode Security and Apache Shiro.
15+
* **storage** - In this example the server is configured to store data off of hte JVM heap using the `@EnableOffHeap` annotation and to compress region data using SnappyCompressor`.
16+
* **transactions** - In this example the client will perform operations within a transaction. First, it will do a successful transaction where entries are saved to the server, and then a failed transaction where all changes are reverted.
17+
* **wan** - In these example two servers are deployed. One server populates itself with data, and the other server gets populated with that data via WAN replication.
18+
19+
# Running The Examples
20+
21+
Each example has at least one test file located in the test directory. The examples are driven by the tests, so simply run the test either through your IDE or via the commandline.
22+
The logging level of the examples is set to "error", so there will be no output. To see output, simply find the `logback.xml` file located in src/test/resources and set the loglevel to "info".

geode/events/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Events Example
2+
3+
In this example you will how to handle events using Async event queue, Cache Listeners, Cache Loaders, and Cache Writers.
4+
5+
NOTE: Inorder to see output, you must change the loglevel from "error" to "info" in the `logback.xml` file located under src/test/resources.

geode/events/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>example.springdata.geode</groupId>
7+
<artifactId>spring-data-geode-examples</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>events</artifactId>
12+
13+
<dependencies>
14+
15+
</dependencies>
16+
17+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2020 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+
* https://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 example.springdata.geode.server.events;
18+
19+
import lombok.Data;
20+
21+
import java.io.Serializable;
22+
23+
/**
24+
* An address used in the examples.
25+
*
26+
* @author Oliver Gierke
27+
* @author Udo Kohlmeyer
28+
* @author Patrick Johnson
29+
*/
30+
@Data
31+
public class Address implements Serializable {
32+
private String street;
33+
private String city;
34+
private String country;
35+
36+
public Address(String street, String city, String country) {
37+
this.street = street;
38+
this.city = city;
39+
this.country = country;
40+
}
41+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2020 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+
* https://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 example.springdata.geode.server.events;
18+
19+
import lombok.Data;
20+
import org.springframework.data.annotation.Id;
21+
import org.springframework.data.gemfire.mapping.annotation.Region;
22+
23+
import java.io.Serializable;
24+
import java.util.Arrays;
25+
import java.util.List;
26+
27+
/**
28+
* A customer used for Lucene examples.
29+
*
30+
* @author Udo Kohlmeyer
31+
* @author Patrick Johnson
32+
*/
33+
@Data
34+
@Region(name = "Customers")
35+
public class Customer implements Serializable {
36+
37+
@Id
38+
private Long id;
39+
private EmailAddress emailAddress;
40+
private String firstName;
41+
private String lastName;
42+
private List<Address> addresses;
43+
44+
public Customer(Long id, EmailAddress emailAddress, String firstName, String lastName, Address... addresses) {
45+
this.id = id;
46+
this.emailAddress = emailAddress;
47+
this.firstName = firstName;
48+
this.lastName = lastName;
49+
this.addresses = Arrays.asList(addresses);
50+
}
51+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2020 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+
* https://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 example.springdata.geode.server.events;
18+
19+
import org.apache.geode.cache.CacheWriterException;
20+
import org.apache.geode.cache.EntryEvent;
21+
import org.apache.geode.cache.util.CacheWriterAdapter;
22+
import org.apache.geode.internal.cache.EntryEventImpl;
23+
import org.springframework.stereotype.Component;
24+
25+
@Component
26+
public class CustomerCacheWriter extends CacheWriterAdapter<Long, Customer> {
27+
28+
@Override
29+
public void beforeCreate(EntryEvent<Long, Customer> event) throws CacheWriterException {
30+
EntryEventImpl e = (EntryEventImpl) event;
31+
super.beforeCreate(e);
32+
}
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2020 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+
* https://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 example.springdata.geode.server.events;
18+
19+
import org.springframework.data.repository.CrudRepository;
20+
21+
public interface CustomerRepository extends CrudRepository<Customer, Long> {
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2020 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+
* https://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 example.springdata.geode.server.events;
18+
19+
import lombok.Data;
20+
21+
import java.io.Serializable;
22+
23+
/**
24+
* Value object to represent email addresses.
25+
*
26+
* @author Udo Kohlmeyer
27+
* @author Patrick Johnson
28+
*/
29+
@Data
30+
public class EmailAddress implements Serializable {
31+
private String value;
32+
33+
public EmailAddress(String value) {
34+
35+
this.value = value;
36+
}
37+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2020 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+
* https://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 example.springdata.geode.server.events;
18+
19+
import org.apache.geode.cache.Region;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
import org.springframework.beans.factory.annotation.Qualifier;
23+
import org.springframework.boot.ApplicationRunner;
24+
import org.springframework.boot.WebApplicationType;
25+
import org.springframework.boot.autoconfigure.SpringBootApplication;
26+
import org.springframework.boot.builder.SpringApplicationBuilder;
27+
import org.springframework.context.annotation.Bean;
28+
29+
import java.math.BigDecimal;
30+
import java.util.List;
31+
import java.util.Random;
32+
import java.util.stream.IntStream;
33+
import java.util.stream.LongStream;
34+
35+
@SpringBootApplication(scanBasePackageClasses = EventServerConfig.class)
36+
public class EventServer {
37+
38+
private Logger logger = LoggerFactory.getLogger(this.getClass());
39+
40+
public static void main(String[] args) {
41+
new SpringApplicationBuilder(EventServer.class)
42+
.web(WebApplicationType.NONE)
43+
.build()
44+
.run(args);
45+
}
46+
47+
@Bean
48+
public ApplicationRunner runner(CustomerRepository customerRepository, OrderRepository orderRepository,
49+
ProductRepository productRepository, OrderProductSummaryRepository orderProductSummaryRepository, @Qualifier("Products") Region<Long, Product> products) {
50+
return args -> {
51+
createCustomerData(customerRepository);
52+
53+
createProducts(productRepository);
54+
55+
createOrders(productRepository, orderRepository);
56+
57+
logger.info("Completed creating orders ");
58+
59+
final List<OrderProductSummary> allForProductID = orderProductSummaryRepository.findAllForProductID(3L);
60+
allForProductID.forEach(orderProductSummary -> logger.info("orderProductSummary = " + orderProductSummary));
61+
};
62+
}
63+
64+
private void createOrders(ProductRepository productRepository, OrderRepository orderRepository) {
65+
Random random = new Random(System.nanoTime());
66+
Address address = new Address("it", "doesn't", "matter");
67+
LongStream.rangeClosed(1, 10).forEach((orderId) ->
68+
LongStream.rangeClosed(1, 300).forEach((customerId) -> {
69+
Order order = new Order(orderId, customerId, address);
70+
IntStream.rangeClosed(0, random.nextInt(3) + 1).forEach((lineItemCount) -> {
71+
int quantity = random.nextInt(3) + 1;
72+
long productId = random.nextInt(3) + 1;
73+
order.add(new LineItem(productRepository.findById(productId).get(), quantity));
74+
});
75+
orderRepository.save(order);
76+
}));
77+
}
78+
79+
private void createProducts(ProductRepository productRepository) {
80+
productRepository.save(new Product(1L, "Apple iPod", new BigDecimal("99.99"),
81+
"An Apple portable music player"));
82+
productRepository.save(new Product(2L, "Apple iPad", new BigDecimal("499.99"),
83+
"An Apple tablet device"));
84+
Product macbook = new Product(3L, "Apple macBook", new BigDecimal("899.99"),
85+
"An Apple notebook computer");
86+
macbook.addAttribute("warranty", "included");
87+
productRepository.save(macbook);
88+
}
89+
90+
private void createCustomerData(CustomerRepository customerRepository) {
91+
LongStream.rangeClosed(0, 300)
92+
.parallel()
93+
.forEach(customerId ->
94+
customerRepository.save(new Customer(customerId, new EmailAddress(customerId + "@2.com"), "John" + customerId, "Smith" + customerId)));
95+
}
96+
}

0 commit comments

Comments
 (0)