Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient -...
Transcript of Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient -...
![Page 1: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/1.jpg)
Reactive SpringKen Kousen, Kousen IT, Inc.
![Page 2: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/2.jpg)
Reactive SpringAn Exercise-Driven Approach
![Page 3: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/3.jpg)
Contact Info
Ken KousenKousen IT, [email protected]://www.kousenit.comhttp://kousenit.org (blog)@kenkousen
![Page 4: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/4.jpg)
Modern Java RecipesSource code:
https://github.com/kousen/java_8_recipes
https://github.com/kousen/cfboxscores
https://github.com/kousen/java_9_recipes
![Page 5: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/5.jpg)
![Page 6: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/6.jpg)
SpringProject infrastructure
![Page 7: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/7.jpg)
SpringLifecycle management of "beans"
Any POJO with getters/setters
![Page 8: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/8.jpg)
SpringProvides "services"
transactions, security, persistence, …
![Page 9: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/9.jpg)
SpringLibrary of beans available
transaction managers
rest client
DB connection pools
testing mechanisms
![Page 10: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/10.jpg)
SpringCode to interfaces
Library has many interfaces, each with many implementations
![Page 11: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/11.jpg)
SpringNeed "metadata"
Tells Spring what to instantiate and configure
XML → old style
Annotations → better
JavaConfig → preferred
All still supported
![Page 12: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/12.jpg)
SpringApplication Context
Collection of managed beans
the "lightweight" Spring container
![Page 13: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/13.jpg)
Spring BootEasy creation and configuration for Spring apps
Many "starters"
Gradle or Maven based
Automatic configuration based on classpath
If you add JDBC driver, it adds DataSource bean
![Page 14: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/14.jpg)
Dependency Injection- Spring adds dependencies on request
- Annotate field, or setter, or constructor- @Autowired → autowiring by type- @Resource (from Java EE) → autowiring by (bean) name, then by type if necessary
![Page 15: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/15.jpg)
Spring InitializrWebsite for creating new Spring (Boot) apps
http://start.spring.io
Incorporated into major IDEs
Select features you want
Download zip containing build file
![Page 16: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/16.jpg)
Spring BootApplication with main method created automatically
Annotated with @SpringBootApplication
Gradle or Maven build produces executable jar in build/libs folder
$ java -jar appname.jar
Or use gradle task bootRun
![Page 17: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/17.jpg)
Spring MVCAnnotation based MVC framework
@Controller → controllers
@GetMapping → annotations for HTTP methods
@RequestParam and more for model parameters
![Page 18: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/18.jpg)
Rest ClientSpring includes a class called RestTemplate
- Access RESTful web services - Set HTTP methods, headers, query string, templates- Use RestTemplateBuilder to create one- Use content negotiation to return JSON or XML- Convenient getForObject(url, class) method
![Page 19: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/19.jpg)
Rest ClientSpring 5 includes a new class called WebClient
- Can do async processing- Understands Flux and Mono
![Page 20: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/20.jpg)
TestingSpring uses special JUnit runner
@RunWith(SpringRunner.class)
Annotate test class with @SpringBootTest
Annotate tests with @Test
Use normal asserts as usual
![Page 21: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/21.jpg)
TestingSpecial annotations for web integration tests
@WebMvcTest(... controller class …)
MockMvc package
MockMvcRequestBuilders
MockMvcRequestMatchers
![Page 22: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/22.jpg)
Parsing JSONSeveral options, but one is the Jackson JSON 2 library
Create classes that map to JSON response
restTemplate.getForObject(url, … your class …)
Maps JSON to Java objects
![Page 23: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/23.jpg)
Component ScanSpring detects annotated classes in the expected folders
@Component → Spring bean
@Controller, @Service, @Repository → based on @Component
![Page 24: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/24.jpg)
Application propertiesTwo options for file name
Default folder is src/main/resources
application.properties → standard Java properties file
application.yml → YAML format
![Page 25: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/25.jpg)
TransactionsSpring transactions configured with @Transactional
Spring uses TransactionManager to talk to resource
usually a relational DB, but other options available
![Page 26: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/26.jpg)
Reactive SpringSpring 5 → requires Java SE8
WebFlux module
Web on Reactive Stack
Spring Boot 2
Spring WebFlux Framework
![Page 27: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/27.jpg)
WebFluxTwo approaches:
Annotation-based → Similar to MVC
Functional → Uses a "routing configuration"
![Page 28: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/28.jpg)
Reactive StreamsIndustry specification with wide adoption
http://www.reactive-streams.org/
Supported in Java 9
java.util.concurrent.Flow
![Page 29: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/29.jpg)
Reactive StreamsFour interfaces
Publisher
Subscriber
Subscription
Processor
![Page 30: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/30.jpg)
PublisherProvides a sequence of elements
Signals emitted:
onSubscribe → always signaled
onNext → possibly unbounded number of signals
onError → only if there is a failure
onComplete → no more elements available
![Page 31: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/31.jpg)
Publisherpublic interface Publisher<T> {
void subscribe(Subscriber<? super T> s);}
![Page 32: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/32.jpg)
SubscriberReceives signals
public interface Subscriber<T> {void onSubscribe(Subscription s);void onNext(T t);void onError(Throwable t);void onComplete();
}
![Page 33: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/33.jpg)
SubscriptionSent from Publisher to Subscriber
public interface Subscription {void request(long n);void cancel();
}
![Page 34: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/34.jpg)
ProcessorA combination Publisher/Subscriber
public interface Processor<T,R> extends Subscriber<T>, Publisher<R> {
}
No additional methods
![Page 35: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/35.jpg)
Project ReactorReactive library for the JVM based on Reactive Streams
Reactive Core → fully non-blocking
Typed sequences → Flux, Mono
Non-blocking IO with backpressure
![Page 36: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/36.jpg)
WebFluxAnnotated Controllers
Support reactive return types
Reactor, RxJava 2
Functional Endpoints
Lambda-based, functional programming model
Library of utilities to route and handle requests
![Page 37: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/37.jpg)
WebFluxUses Netty by default
Others: Undertow, Tomcat (Servlet 3.1+), Jetty (Servlet 3.1+)
![Page 38: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/38.jpg)
WebClientReactive, non-blocking client for HTTP requests
Functional API with Java 8 lambdas
Both synchronous and asynchronous
Use WebTestClient for testing → mock request and response objects
![Page 39: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/39.jpg)
WebFluxFunctional Endpoints
HandlerFunction
RouterFunction
![Page 40: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/40.jpg)
ExercisesHTML docs: http://www.kousenit.com/reactivespring/
Solutions:
https://github.com/kousen/reactive-officers
https://github.com/kousen/rest-service
https://github.com/kousen/spring-and-spring-boot (MVC, non-reactive)
![Page 41: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/41.jpg)
Docs- Spring API JavaDocs:
- http://docs.spring.io/spring/docs/current/javadoc-api/
- Spring Reference Guide:- https://spring.io/docs/reference
- Spring Boot Reference Guide- http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/
- Spring Initializr- https://start.spring.io/
![Page 42: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/42.jpg)
DocsWait, you're all on Safari, so…
- Learning Path: Learn Spring and Spring Boot- Includes Spring Framework Essentials
- Spring in Action, 4th Edition- Spring Boot in Action- … lots and lots more …
![Page 43: Reactive Spring - DeveloperMarch · 2018-07-27 · Spring 5 includes a new class called WebClient - Can do async processing - Understands Flux and Mono. ... Reactive Spring Spring](https://reader034.fdocuments.net/reader034/viewer/2022042220/5ec5ed621742e66de408f5cc/html5/thumbnails/43.jpg)
Thank you