BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Pivotal Releases First Milestone of Next-Generation Spring Data Featuring Reactive Database Access

Pivotal Releases First Milestone of Next-Generation Spring Data Featuring Reactive Database Access

This item in japanese

Pivotal recently released the first milestone of next-generation Spring Data projects, dubbed Release Train Kay. As stated in the release:

This is a special release train as it’s going to ship a new generation of Spring Data that will include a couple of breaking changes going forward.

Significant changes include:

MongoDB, Cassandra, and Redis were chosen because of available drivers for reactive data access. Spring Data repository interfaces from Project Reactor and RxJava are included for all three.

To get started with a Spring Data reactive MongoDB example, consider the following POJO:

    
public class Person {
    private @Id String id;
    private final String firstname;
    private final String lastname;
    }
    

A base reactive repository interface using Project Reactor could be written as:

    
public interface ReactivePersonRepository extends ReactiveCrudRepository<Person,String> {
    Flux<Person> findByLastname(String lastname);

    @Query("{ 'firstname': ?0, 'lastname': ?1}")
    Mono<Person> findByFirstnameAndLastname(String firstname,String lastname);

    Flux<Person> findByLastname(Mono<String> lastname);
    }
    

The use of reactive classes, Flux<T> and Mono<T>, can now be used as return types and parameters.

As defined in a recent InfoQ article:

A Flux is the equivalent of an RxJava Observable, capable of emitting 0 or more items, and then optionally either completing or erroring.

 

A Mono on the other hand can emit at most once. It corresponds to both Single and Maybe types on the RxJava side. Thus an asynchronous task that just wants to signal completion can use a Mono<Void>.

Similarly, a base reactive repository interface using RxJava could be written as:

    
public interface RxJava1PersonRepository extends RxJava1CrudRepository<Person,String> {
    Observable<Person> findByLastname(String lastname);

    @Query("{ 'firstname': ?0, 'lastname': ?1}")
    Single<Person> findByFirstnameAndLastname(String firstname,String lastname);

    Observable<Person> findByLastname(Single<String> lastname);
    }
    

Activating Spring Data reactive repositories is accomplished through annotations, @EnableReactiveMongoRepositories and @EnableCassandraRepositories, as shown in the MongoDB example shown below:

    
@EnableReactiveMongoRepositories
@AutoConfigreAfter(EmbeddedMongoAutoConfiguration.class)
class ApplicationConfiguration extended AbstractReactiveMongoConfiguration {

    @Bean
    public LoggingEventListener mongoEventListener() {
        return new LoggingEventListener();
        }

    @Override
    @Bean
    public MongoClient mongoClient() {
        return MongoClients.create();
        }

    @Override
    protected String getDatabaseName() {
        return "reactive";
        }
    }
    

Similar to building a traditional template for database operations, reactive templates can also be written:

    
public interface ReactiveMongoOperations {
    // other operations...
    <T> Mono<T> findOne(Query query,Class<T> entityClass);
    <T> Flux<T> find(Query query,Class<T> entityClass);
    <T> Mono<T> insert(T objectToSave,String collectionName);
    <T> Mono<T> insert(Mono<? extends T> objectToSave);
    }
    

Data can be inserted via a template as well:

    
Flux<Person> flux = Flux.just(new Person("Vincent","Vega"),
    new Person("Jules","Winnfield"),
    new Person("Marsellus","Wallace"),
    new Person("Mia","Wallace"));
template.insertAll(flux).subscribe();
    

 

The entire Spring Data reactive MongoDB project can be found on GitHub.

Resources

Additional resources for more detailed information on reactive programming can be found via the following articles:

Rate this Article

Adoption
Style

BT