BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

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

| by Michael Redlich Follow 2 Followers on Jan 19, 2017. Estimated reading time: 2 minutes |

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 Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT