From 7a557bfd72bc624f38addb05bf19c39a822d27b5 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 25 Jul 2017 16:48:33 +0200 Subject: [PATCH] #297 - Move MongoDB example from RxJava1 to RxJava2. --- mongodb/reactive/README.md | 14 ++--- mongodb/reactive/pom.xml | 3 +- ...tory.java => RxJava2PersonRepository.java} | 22 ++++---- .../ReactiveMongoTemplateIntegrationTest.java | 2 +- ...Java2PersonRepositoryIntegrationTest.java} | 53 +++++++++---------- 5 files changed, 47 insertions(+), 47 deletions(-) rename mongodb/reactive/src/main/java/example/springdata/mongodb/people/{RxJava1PersonRepository.java => RxJava2PersonRepository.java} (73%) rename mongodb/reactive/src/test/java/example/springdata/mongodb/people/{RxJava1PersonRepositoryIntegrationTest.java => RxJava2PersonRepositoryIntegrationTest.java} (79%) diff --git a/mongodb/reactive/README.md b/mongodb/reactive/README.md index ca05d922..17d6b3ed 100644 --- a/mongodb/reactive/README.md +++ b/mongodb/reactive/README.md @@ -27,7 +27,7 @@ Reactive data access reads and converts individual elements while processing the ## Reactive Repository support -Spring Data MongoDB provides reactive repository support with Project Reactor and RxJava 1 reactive types. The reactive API supports reactive type conversion between reactive types. +Spring Data MongoDB provides reactive repository support with Project Reactor and RxJava 2 reactive types. The reactive API supports reactive type conversion between reactive types. ```java public interface ReactivePersonRepository extends ReactiveCrudRepository { @@ -48,19 +48,19 @@ public interface ReactivePersonRepository extends ReactiveCrudRepository { +public interface RxJava2PersonRepository extends RxJava2CrudRepository { - Observable findByLastname(String lastname); + Flowable findByLastname(String lastname); @Query("{ 'firstname': ?0, 'lastname': ?1}") - Single findByFirstnameAndLastname(String firstname, String lastname); + Maybe findByFirstnameAndLastname(String firstname, String lastname); // Accept parameter inside a reactive type for deferred execution - Observable findByLastname(Single lastname); + Flowable findByLastname(Single lastname); - Single findByFirstnameAndLastname(Single firstname, String lastname); + Maybe findByFirstnameAndLastname(Single firstname, String lastname); @InfiniteStream // Use a tailable cursor - Observable findWithTailableCursorBy(); + Flowable findWithTailableCursorBy(); } ``` diff --git a/mongodb/reactive/pom.xml b/mongodb/reactive/pom.xml index a0ff1d31..d0777f54 100644 --- a/mongodb/reactive/pom.xml +++ b/mongodb/reactive/pom.xml @@ -23,8 +23,9 @@ - io.reactivex + io.reactivex.rxjava2 rxjava + 2.1.1 diff --git a/mongodb/reactive/src/main/java/example/springdata/mongodb/people/RxJava1PersonRepository.java b/mongodb/reactive/src/main/java/example/springdata/mongodb/people/RxJava2PersonRepository.java similarity index 73% rename from mongodb/reactive/src/main/java/example/springdata/mongodb/people/RxJava1PersonRepository.java rename to mongodb/reactive/src/main/java/example/springdata/mongodb/people/RxJava2PersonRepository.java index 69ef0854..aad0ff3d 100644 --- a/mongodb/reactive/src/main/java/example/springdata/mongodb/people/RxJava1PersonRepository.java +++ b/mongodb/reactive/src/main/java/example/springdata/mongodb/people/RxJava2PersonRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,19 +15,21 @@ */ package example.springdata.mongodb.people; -import rx.Observable; -import rx.Single; +import io.reactivex.Flowable; +import io.reactivex.Maybe; +import io.reactivex.Single; import org.springframework.data.mongodb.repository.Query; import org.springframework.data.mongodb.repository.Tailable; -import org.springframework.data.repository.reactive.RxJava1CrudRepository; +import org.springframework.data.repository.reactive.RxJava2CrudRepository; /** * Repository interface to manage {@link Person} instances. * * @author Mark Paluch + * @author Christoph Strobl */ -public interface RxJava1PersonRepository extends RxJava1CrudRepository { +public interface RxJava2PersonRepository extends RxJava2CrudRepository { /** * Derived query selecting by {@code lastname}. @@ -35,7 +37,7 @@ public interface RxJava1PersonRepository extends RxJava1CrudRepository findByLastname(String lastname); + Flowable findByLastname(String lastname); /** * String query selecting one entity. @@ -44,7 +46,7 @@ public interface RxJava1PersonRepository extends RxJava1CrudRepository findByFirstnameAndLastname(String firstname, String lastname); + Maybe findByFirstnameAndLastname(String firstname, String lastname); /** * Derived query selecting by {@code lastname}. {@code lastname} uses deferred resolution that does not require @@ -53,7 +55,7 @@ public interface RxJava1PersonRepository extends RxJava1CrudRepository findByLastname(Single lastname); + Flowable findByLastname(Single lastname); /** * Derived query selecting by {@code firstname} and {@code lastname}. {@code firstname} uses deferred resolution which @@ -63,7 +65,7 @@ public interface RxJava1PersonRepository extends RxJava1CrudRepository findByFirstnameAndLastname(Single firstname, String lastname); + Maybe findByFirstnameAndLastname(Single firstname, String lastname); /** * Use a tailable cursor to emit a stream of entities as new entities are written to the capped collection. @@ -71,5 +73,5 @@ public interface RxJava1PersonRepository extends RxJava1CrudRepository findWithTailableCursorBy(); + Flowable findWithTailableCursorBy(); } diff --git a/mongodb/reactive/src/test/java/example/springdata/mongodb/people/ReactiveMongoTemplateIntegrationTest.java b/mongodb/reactive/src/test/java/example/springdata/mongodb/people/ReactiveMongoTemplateIntegrationTest.java index 51cf961d..31886e7b 100644 --- a/mongodb/reactive/src/test/java/example/springdata/mongodb/people/ReactiveMongoTemplateIntegrationTest.java +++ b/mongodb/reactive/src/test/java/example/springdata/mongodb/people/ReactiveMongoTemplateIntegrationTest.java @@ -89,7 +89,7 @@ public class ReactiveMongoTemplateIntegrationTest { * Note that the all object conversions are performed before the results are printed to the console. */ @Test - public void convertReactorTypesToRxJava1() throws Exception { + public void convertReactorTypesToRxJava2() throws Exception { Flux flux = template.find(Query.query(Criteria.where("lastname").is("White")), Person.class); diff --git a/mongodb/reactive/src/test/java/example/springdata/mongodb/people/RxJava1PersonRepositoryIntegrationTest.java b/mongodb/reactive/src/test/java/example/springdata/mongodb/people/RxJava2PersonRepositoryIntegrationTest.java similarity index 79% rename from mongodb/reactive/src/test/java/example/springdata/mongodb/people/RxJava1PersonRepositoryIntegrationTest.java rename to mongodb/reactive/src/test/java/example/springdata/mongodb/people/RxJava2PersonRepositoryIntegrationTest.java index 6467e29c..603957cd 100644 --- a/mongodb/reactive/src/test/java/example/springdata/mongodb/people/RxJava1PersonRepositoryIntegrationTest.java +++ b/mongodb/reactive/src/test/java/example/springdata/mongodb/people/RxJava2PersonRepositoryIntegrationTest.java @@ -17,11 +17,11 @@ package example.springdata.mongodb.people; import static org.assertj.core.api.Assertions.*; +import io.reactivex.Flowable; +import io.reactivex.Single; +import io.reactivex.disposables.Disposable; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import rx.Observable; -import rx.Single; -import rx.Subscription; import java.util.List; import java.util.concurrent.CountDownLatch; @@ -36,18 +36,19 @@ import org.springframework.data.mongodb.core.ReactiveMongoOperations; import org.springframework.test.context.junit4.SpringRunner; /** - * Integration test for {@link RxJava1PersonRepository} using RxJava1 types. Note that {@link ReactiveMongoOperations} + * Integration test for {@link RxJava2PersonRepository} using RxJava2 types. Note that {@link ReactiveMongoOperations} * is only available using Project Reactor types as the native Template API implementation does not come in multiple * reactive flavors. * * @author Mark Paluch * @author Jens Schauder + * @author Christoph Strobl */ @RunWith(SpringRunner.class) @SpringBootTest -public class RxJava1PersonRepositoryIntegrationTest { +public class RxJava2PersonRepositoryIntegrationTest { - @Autowired RxJava1PersonRepository repository; + @Autowired RxJava2PersonRepository repository; @Autowired ReactiveMongoOperations operations; @Before @@ -55,17 +56,16 @@ public class RxJava1PersonRepositoryIntegrationTest { operations.collectionExists(Person.class) // .flatMap(exists -> exists ? operations.dropCollection(Person.class) : Mono.just(exists)) // - .flatMap(o -> operations.createCollection(Person.class, CollectionOptions.empty().size(1024 * 1024).maxDocuments(100).capped())) // + .flatMap(o -> operations.createCollection(Person.class, + CollectionOptions.empty().size(1024 * 1024).maxDocuments(100).capped())) // .then() // .block(); - repository - .saveAll(Observable.just(new Person("Walter", "White", 50), // - new Person("Skyler", "White", 45), // - new Person("Saul", "Goodman", 42), // - new Person("Jesse", "Pinkman", 27))) // - .toBlocking() // - .last(); + repository.saveAll(Flowable.just(new Person("Walter", "White", 50), // + new Person("Skyler", "White", 45), // + new Person("Saul", "Goodman", 42), // + new Person("Jesse", "Pinkman", 27))) // + .blockingLast(); } /** @@ -76,14 +76,14 @@ public class RxJava1PersonRepositoryIntegrationTest { CountDownLatch countDownLatch = new CountDownLatch(1); - Observable people = Observable.just(new Person("Hank", "Schrader", 43), // + Flowable people = Flowable.just(new Person("Hank", "Schrader", 43), // new Person("Mike", "Ehrmantraut", 62)); repository.count() // .doOnSuccess(System.out::println) // - .toObservable() // + .toFlowable() // .switchMap(count -> repository.saveAll(people)) // - .last() // + .lastElement() // .toSingle() // .flatMap(v -> repository.count()) // .doOnSuccess(System.out::println) // @@ -104,7 +104,7 @@ public class RxJava1PersonRepositoryIntegrationTest { repository.findAll() // .doOnNext(System.out::println) // - .doOnCompleted(countDownLatch::countDown) // + .doOnComplete(countDownLatch::countDown) // .doOnError(throwable -> countDownLatch.countDown()) // .subscribe(); @@ -117,9 +117,9 @@ public class RxJava1PersonRepositoryIntegrationTest { @Test public void shouldStreamDataWithTailableCursor() throws Exception { - Subscription subscription = repository.findWithTailableCursorBy() // + Disposable subscription = repository.findWithTailableCursorBy() // .doOnNext(System.out::println) // - .doOnCompleted(() -> System.out.println("Complete")) // + .doOnComplete(() -> System.out.println("Complete")) // .doOnTerminate(() -> System.out.println("Terminated")) // .subscribe(); @@ -131,7 +131,7 @@ public class RxJava1PersonRepositoryIntegrationTest { repository.save(new Person("Mike", "Ehrmantraut", 62)).subscribe(); Thread.sleep(100); - subscription.unsubscribe(); + subscription.dispose(); repository.save(new Person("Gus", "Fring", 53)).subscribe(); Thread.sleep(100); @@ -145,8 +145,7 @@ public class RxJava1PersonRepositoryIntegrationTest { List whites = repository.findByLastname("White") // .toList() // - .toBlocking() // - .last(); + .blockingGet(); assertThat(whites).hasSize(2); } @@ -158,8 +157,7 @@ public class RxJava1PersonRepositoryIntegrationTest { public void shouldQueryDataWithStringQuery() { Person heisenberg = repository.findByFirstnameAndLastname("Walter", "White") // - .toBlocking() // - .value(); + .blockingGet(); assertThat(heisenberg).isNotNull(); } @@ -172,8 +170,7 @@ public class RxJava1PersonRepositoryIntegrationTest { List whites = repository.findByLastname(Single.just("White")) // .toList() // - .toBlocking() // - .single(); + .blockingGet(); assertThat(whites).hasSize(2); } @@ -185,7 +182,7 @@ public class RxJava1PersonRepositoryIntegrationTest { public void shouldQueryDataWithMixedDeferredQueryDerivation() { Person heisenberg = repository.findByFirstnameAndLastname(Single.just("Walter"), "White") // - .toBlocking().value(); + .blockingGet(); assertThat(heisenberg).isNotNull(); }