Files
spring-data-cassandra/spring-cql
Mark Paluch 52f4f570a9 DATACASS-335 - Add support for reactive data access.
We now support reactive data access with Spring Data Cassandra by adopting Datastax' asynchronous driver.

ReactiveCqlTemplate and ReactiveCassandraTemplate use Project Reactor wrapper types Mono and Flux to implement Template API and repository support. Reactive template supports common operations such as:

* Query/Execution methods for static CQL and prepared statements
* Insert/Save/Update/Delete methods
* Exists and Count projections
* Reactive Callback methods

Person person = new Person("Dave", 25);

template.insert(person) //
    .flatMap(p -> template.update(new Person("Sven", 25))) //
    .flatMap(p -> template.selectOneById(person.getId(), Person.class)) //
    .subscribeWith(TestSubscriber.create()) //
    .await() //
    .assertValuesWith(result -> {
        assertThat(result.getFirstName(), is(equalTo("Sven")));
    });

Reactive Repository support is built on top of ReactiveCassandraTemplate using ReactiveCassandraRepository as the store-specific base repository. Reactive repositories are enabled by using @EnableReactiveCassandraRepositories on a @Configuration class to opt-in for reactive support. Reactive repositories can be composed of a reactive base interface such as

* ReactiveCrudRepository
* ReactiveSortingRepository
* RxJava1CrudRepository
* RxJava1SortingRepository

and are identified as reactive repository if one method uses a reactive wrapper type (such as Flux or Observable). If a reactive repository is discovered, it's not implemented by the blocking repository support but with the reactive repository factory. Blocking methods are not (yet) synchronized when using a reactive repository so each repository method must use a reactive wrapper result type. Reactive repository support with Spring Data allows using RxJava1 and Project Reactor types to declare repository methods. Reactive wrapper types are internally converted so the composition library choice on repository level is left up to the user.

There's feature parity between Reactive Cassandra repository support and blocking repository support.

Feature overview:

* Query Methods using String queries and Query Derivation
* Projections

@Configuration
@EnableReactiveCassandraRepositories
class ApplicationConfig extends AbstractReactiveCassandraConfiguration {

  @Override
  protected String getKeyspaceName() {
    return "mykeyspace";
  }

  @Override
  protected String getEntityBasePackages() {
    return new String[] {"com.springdata.cassandra"};
  }
}

public interface PersonRepository extends ReactiveSortingRepository<Person, String> {

  Flux<Person> findByFirstname(String firstname);

  Flux<Person> findByFirstname(Publisher<String> firstname);

  Mono<Person> findByFirstnameAndLastname(String firstname, String lastname);
}

public interface PersonRepository extends RxJava1SortingRepository<Person, String> {

  Observable<Person> findByFirstname(String firstname);

  Observable<Person> findByFirstname(Single<String> firstname);

  Single<Person> findByFirstnameAndLastname(String firstname, String lastname);
}
2016-11-11 04:45:22 -08:00
..
2016-10-25 09:37:49 +02:00