Java Magazine, Jan/Feb 2018
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2018 68 reactive programming Then the title and book author are used to create a Book Then the records are saved to the data source MongoDB The result of the save operation is a Mono Book Something needs to subscribe to each of those resulting Publisher T instances so I use the flatMap operator Then I turn my focus to the results of finding all records and then to logging them for inspection This code defines a pipeline each operator defines a stage in a pipeline The pipeline is not eager that is it wont be executed until it is activated You activate the pipeline by subscribing to it the last step in the code in Listing 8 Publisher defines only one type of subscription but Reactor provides hooks to process each emitted value as well as any exceptions thrown among other things Were you to put a breakpoint in any of the lambdas in Listing 8 and then inspect Thread currentThread getName youd see that the thread on which processing is running is diferent than the main thread which is named main Reactor defers to a Scheduler implementation for its processing You can specify the default global Scheduler youd like to use by calling Schedulers setFactory Factory You can specify on which thread a particular Publisher should run when it subscribes by specifying Mono subscribeOn Scheduler or Flux subscribeOn Scheduler Conclusion You have now used Spring Boot and Spring Initializr to quickly create and run a reactive data application that hews closely to the requirements of reactive development In the second and final part of this article Ill use Spring Framework 50 to stand up a REST API and to implement secure access to this data Meanwhile if you want to look at the complete application the source code is all online article Josh Long @ starbuxman is a Java Champion and a Spring developer advocate at Pivotal He is the author of several books on Spring programming and he speaks frequently at developer conferences
You must have JavaScript enabled to view digital editions.