Skip to content

Commit fd7d35a

Browse files
author
Thomas Darimont
committed
spring-projects#106 - Add example with CompletableFuture and Java 8 for JPA.
Simple test case that demonstrates CompletableFuture. Original pull request: spring-projects#106.
1 parent 98bfe34 commit fd7d35a

File tree

8 files changed

+69
-25
lines changed

8 files changed

+69
-25
lines changed

jpa/java8/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
<version>1.0.0.BUILD-SNAPSHOT</version>
99
</parent>
1010

11+
<properties>
12+
<spring.version>4.2.0.BUILD-SNAPSHOT</spring.version>
13+
<spring-data-releasetrain.version>Gosling-BUILD-SNAPSHOT</spring-data-releasetrain.version>
14+
</properties>
15+
1116
<artifactId>spring-data-jpa-java8</artifactId>
1217
<name>Spring Data JPA - Java 8 specific features</name>
13-
1418
</project>

jpa/java8/src/main/java/example/springdata/jpa/java8/AuditingConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import org.springframework.context.annotation.Configuration;
2121
import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters;
2222
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
23+
import org.springframework.scheduling.annotation.EnableAsync;
2324

2425
@Configuration
26+
@EnableAsync
2527
@EnableAutoConfiguration
2628
@EntityScan(basePackageClasses = { AuditingConfiguration.class, Jsr310JpaConverters.class })
2729
@EnableJpaAuditing

jpa/java8/src/main/java/example/springdata/jpa/java8/Customer.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,26 @@
1515
*/
1616
package example.springdata.jpa.java8;
1717

18+
import lombok.AllArgsConstructor;
19+
import lombok.Getter;
20+
import lombok.ToString;
21+
1822
import javax.persistence.Entity;
1923

2024
/**
2125
* @author Oliver Gierke
26+
* @author Thomas Darimont
2227
*/
2328
@Entity
29+
@Getter
30+
@ToString
31+
@AllArgsConstructor
2432
public class Customer extends AbstractEntity {
2533

2634
String firstname, lastname;
2735

28-
public Customer(String firstname, String lastname) {
29-
30-
this.firstname = firstname;
31-
this.lastname = lastname;
36+
protected Customer() {
37+
this.firstname = null;
38+
this.lastname = null;
3239
}
3340
}

jpa/java8/src/main/java/example/springdata/jpa/java8/CustomerRepository.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
*/
1616
package example.springdata.jpa.java8;
1717

18+
import java.util.List;
1819
import java.util.Optional;
20+
import java.util.concurrent.CompletableFuture;
1921
import java.util.stream.Stream;
2022

2123
import org.springframework.data.jpa.repository.Query;
2224
import org.springframework.data.repository.CrudRepository;
2325
import org.springframework.data.repository.Repository;
26+
import org.springframework.scheduling.annotation.Async;
2427

2528
/**
2629
* Repository to manage {@link Customer} instances.
@@ -80,4 +83,7 @@ default Optional<Customer> findByLastname(Customer customer) {
8083
* @return
8184
*/
8285
Stream<Customer> findAllByLastnameIsNotNull();
86+
87+
@Async
88+
CompletableFuture<List<Customer>> readAllBy();
8389
}

jpa/java8/src/test/java/example/springdata/jpa/java8/Java8IntegrationTests.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@
1919
import static org.junit.Assert.*;
2020

2121
import java.util.Optional;
22+
import java.util.concurrent.CompletableFuture;
23+
import java.util.concurrent.TimeUnit;
2224
import java.util.stream.Collectors;
2325
import java.util.stream.Stream;
2426

2527
import org.junit.Test;
2628
import org.junit.runner.RunWith;
2729
import org.springframework.beans.factory.annotation.Autowired;
28-
import org.springframework.test.context.ContextConfiguration;
30+
import org.springframework.boot.test.SpringApplicationConfiguration;
2931
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32+
import org.springframework.transaction.annotation.Propagation;
3033
import org.springframework.transaction.annotation.Transactional;
3134

3235
/**
@@ -36,7 +39,7 @@
3639
* @author Thomas Darimont
3740
*/
3841
@RunWith(SpringJUnit4ClassRunner.class)
39-
@ContextConfiguration(classes = AuditingConfiguration.class)
42+
@SpringApplicationConfiguration(classes = AuditingConfiguration.class)
4043
@Transactional
4144
public class Java8IntegrationTests {
4245

@@ -99,4 +102,35 @@ public void useJava8StreamsWithDerivedQuery() {
99102
assertThat(stream.collect(Collectors.toList()), hasItems(customer1, customer2));
100103
}
101104
}
105+
106+
/**
107+
* Here we demonstrate the usage of CompletableFuture as a result wrapper for asynchronous
108+
* Repository query methods.
109+
*
110+
* Note that we need to disable the surrounding TX to be able to asynchronously read the wirtten
111+
* data from from another thread within the same test method.
112+
*/
113+
@Test
114+
@Transactional(propagation = Propagation.NOT_SUPPORTED)
115+
public void supportsCompletableFuturesAsReturnTypeWrapper() throws Exception {
116+
117+
repository.save(new Customer("Customer1", "Foo"));
118+
repository.save(new Customer("Customer2", "Bar"));
119+
120+
CompletableFuture<Void> future = repository.readAllBy().thenAccept(customers -> {
121+
122+
assertThat(customers, hasSize(2));
123+
124+
customers.forEach(System.out::println);
125+
System.out.println("Completed!");
126+
});
127+
128+
while (!future.isDone()) {
129+
System.out.println("waiting...");
130+
TimeUnit.MILLISECONDS.sleep(500);
131+
}
132+
133+
future.get();
134+
System.out.println("Done");
135+
}
102136
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#logging.level.org.springframework=INFO
2+
#logging.level.org.springframework.orm=TRACE
3+
#logging.level.org.springframework.transaction=TRACE
4+
#logging.level.org.hsqldb=TRACE

jpa/java8/src/test/resources/logback.xml

Lines changed: 0 additions & 18 deletions
This file was deleted.

jpa/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
<artifactId>joda-time</artifactId>
4040
</dependency>
4141

42+
<!-- <dependency> -->
43+
<!-- <groupId>com.h2database</groupId> -->
44+
<!-- <artifactId>h2</artifactId> -->
45+
<!-- </dependency> -->
46+
4247
<dependency>
4348
<groupId>org.hsqldb</groupId>
4449
<artifactId>hsqldb</artifactId>

0 commit comments

Comments
 (0)