#106 - Add example with CompletableFuture and Java 8 for JPA.

Simple test case that demonstrates CompletableFuture.

Original pull request: #106.
This commit is contained in:
Thomas Darimont
2015-06-18 11:08:52 +02:00
parent 98bfe34996
commit fd7d35a826
8 changed files with 69 additions and 25 deletions

View File

@@ -8,7 +8,11 @@
<version>1.0.0.BUILD-SNAPSHOT</version>
</parent>
<properties>
<spring.version>4.2.0.BUILD-SNAPSHOT</spring.version>
<spring-data-releasetrain.version>Gosling-BUILD-SNAPSHOT</spring-data-releasetrain.version>
</properties>
<artifactId>spring-data-jpa-java8</artifactId>
<name>Spring Data JPA - Java 8 specific features</name>
</project>

View File

@@ -20,8 +20,10 @@ import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;
@Configuration
@EnableAsync
@EnableAutoConfiguration
@EntityScan(basePackageClasses = { AuditingConfiguration.class, Jsr310JpaConverters.class })
@EnableJpaAuditing

View File

@@ -15,19 +15,26 @@
*/
package example.springdata.jpa.java8;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import javax.persistence.Entity;
/**
* @author Oliver Gierke
* @author Thomas Darimont
*/
@Entity
@Getter
@ToString
@AllArgsConstructor
public class Customer extends AbstractEntity {
String firstname, lastname;
public Customer(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
protected Customer() {
this.firstname = null;
this.lastname = null;
}
}

View File

@@ -15,12 +15,15 @@
*/
package example.springdata.jpa.java8;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.Repository;
import org.springframework.scheduling.annotation.Async;
/**
* Repository to manage {@link Customer} instances.
@@ -80,4 +83,7 @@ public interface CustomerRepository extends Repository<Customer, Long> {
* @return
*/
Stream<Customer> findAllByLastnameIsNotNull();
@Async
CompletableFuture<List<Customer>> readAllBy();
}

View File

@@ -19,14 +19,17 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -36,7 +39,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Thomas Darimont
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AuditingConfiguration.class)
@SpringApplicationConfiguration(classes = AuditingConfiguration.class)
@Transactional
public class Java8IntegrationTests {
@@ -99,4 +102,35 @@ public class Java8IntegrationTests {
assertThat(stream.collect(Collectors.toList()), hasItems(customer1, customer2));
}
}
/**
* Here we demonstrate the usage of CompletableFuture as a result wrapper for asynchronous
* Repository query methods.
*
* Note that we need to disable the surrounding TX to be able to asynchronously read the wirtten
* data from from another thread within the same test method.
*/
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void supportsCompletableFuturesAsReturnTypeWrapper() throws Exception {
repository.save(new Customer("Customer1", "Foo"));
repository.save(new Customer("Customer2", "Bar"));
CompletableFuture<Void> future = repository.readAllBy().thenAccept(customers -> {
assertThat(customers, hasSize(2));
customers.forEach(System.out::println);
System.out.println("Completed!");
});
while (!future.isDone()) {
System.out.println("waiting...");
TimeUnit.MILLISECONDS.sleep(500);
}
future.get();
System.out.println("Done");
}
}

View File

@@ -0,0 +1,4 @@
#logging.level.org.springframework=INFO
#logging.level.org.springframework.orm=TRACE
#logging.level.org.springframework.transaction=TRACE
#logging.level.org.hsqldb=TRACE

View File

@@ -1,18 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="info" />
<logger name="org.springframework.boot" level="debug" />
<logger name="org.hibernate.tool.hbm2ddl" level="trace" />
<root level="error">
<appender-ref ref="console" />
</root>
</configuration>

View File

@@ -39,6 +39,11 @@
<artifactId>joda-time</artifactId>
</dependency>
<!-- <dependency> -->
<!-- <groupId>com.h2database</groupId> -->
<!-- <artifactId>h2</artifactId> -->
<!-- </dependency> -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>