Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Reactive Spring 5
(AKA Functional Reactive Programming with Spring)
Why I chose this talk?
A definition
Reactive Programming is all about non-blocking applications
that are asynchronous and event-driven and require a small
number of threads to scale
-Spring.io
What is Reactive Programming?
• Observer
• Interface to notify an object that the
next item in a sequence it is watching
it is available
• Streams
• Controlled exchange of stream data
across Applications
• Back-pressure
• control the flow of a Stream between
producer and consumer
Functional Reactive Endpoints using Spring 5
What is Functional Reactive Programming?
Conal Elliot defined FRP back in 1998, in his paper "Functional
Reactive Animation”:
• “FRP expressions describe entire evolutions of values over
time, representing these evolutions directly as first-class
values
What is Functional reactive programming?
• Compositionality
• Being able to compose functions
• Immutability
• Guarantees inherently parallelisable
Functional Reactive Endpoints using Spring 5
Spring Reactor
Reactive Mongo
public interface QuoteMongoReactiveRepository
extends ReactiveCrudRepository<Quote, String>
{
}
Functional Reactive Endpoints using Spring 5
Reactive Rest
@GetMapping("/quotes-reactive")
public Flux<Quote> getQuoteFlux() {
return quoteMongoReactiveRepository.findAll();
}
Angular
quotes: Quote[] = new Array();
url: string = 'http://localhost:8080/quotes-reactive';
getQuoteStream(page?: number, size?: number): Observable<Array<Quote>> {
this.quotes = new Array();
return Observable.create((observer) => {
let url = this.url;
let eventSource = new EventSource(url);
eventSource.onmessage = (event) => {
console.debug('Received event: ', event);
let json = JSON.parse(event.data);
this.quotes.push(new Quote(json['id'], json['book'], json['content']));
observer.next(this.quotes);
};
eventSource.onerror = (error) => observer.error('EventSource error: ' + error);
});
}
Angular
quotes: Quote[] = new Array();
url: string = 'http://localhost:8080/quotes-reactive';
getQuoteStream(page?: number, size?: number): Observable<Array<Quote>> {
this.quotes = new Array();
return Observable.create((observer) => {
let url = this.url;
let eventSource = new EventSource(url);
eventSource.onmessage = (event) => {
console.debug('Received event: ', event);
let json = JSON.parse(event.data);
this.quotes.push(new Quote(json['id'], json['book'], json['content']));
observer.next(this.quotes);
};
eventSource.onerror = (error) => observer.error('EventSource error: ' + error);
});
}
Demo Time
How do you test?
• curl -H "Accept: text/event-stream" http://localhost:8080/quotes-reactive
• Spring WebClient
• Spring WebTestClient
• Postman? Soapui? Browser?
Thoughts on Reactive Spring
• Pros
• Safer concurrency
• scale with a small, fixed number of threads and less
memory.
• makes applications more resilient under load because
they scale in a more predictable way
• Baked into Spring
• Java 10 Oracle Drivers Planned
Thoughts on Reactive Spring
• Cons
• Debugging complexity
• Steeeeeeep learning curve
• Do we understand Blocking?
• “it doesn’t matter if you use a Reactive Web approach in
the backend, it won’t be really reactive and non-blocking
unless your client is able to handle it as well”
Finally
• Source - https://github.com/roryp/full-reactive-stack
• Resources
• Project Reactor Site - https://projectreactor.io/
• Conal’s original paper https://github.com/conal/talk-2015-essence-and-
origins-of-frp

More Related Content

Functional Reactive Endpoints using Spring 5

  • 1. Reactive Spring 5 (AKA Functional Reactive Programming with Spring)
  • 2. Why I chose this talk?
  • 3. A definition Reactive Programming is all about non-blocking applications that are asynchronous and event-driven and require a small number of threads to scale -Spring.io
  • 4. What is Reactive Programming? • Observer • Interface to notify an object that the next item in a sequence it is watching it is available • Streams • Controlled exchange of stream data across Applications • Back-pressure • control the flow of a Stream between producer and consumer
  • 6. What is Functional Reactive Programming? Conal Elliot defined FRP back in 1998, in his paper "Functional Reactive Animation”: • “FRP expressions describe entire evolutions of values over time, representing these evolutions directly as first-class values
  • 7. What is Functional reactive programming? • Compositionality • Being able to compose functions • Immutability • Guarantees inherently parallelisable
  • 10. Reactive Mongo public interface QuoteMongoReactiveRepository extends ReactiveCrudRepository<Quote, String> { }
  • 12. Reactive Rest @GetMapping("/quotes-reactive") public Flux<Quote> getQuoteFlux() { return quoteMongoReactiveRepository.findAll(); }
  • 13. Angular quotes: Quote[] = new Array(); url: string = 'http://localhost:8080/quotes-reactive'; getQuoteStream(page?: number, size?: number): Observable<Array<Quote>> { this.quotes = new Array(); return Observable.create((observer) => { let url = this.url; let eventSource = new EventSource(url); eventSource.onmessage = (event) => { console.debug('Received event: ', event); let json = JSON.parse(event.data); this.quotes.push(new Quote(json['id'], json['book'], json['content'])); observer.next(this.quotes); }; eventSource.onerror = (error) => observer.error('EventSource error: ' + error); }); }
  • 14. Angular quotes: Quote[] = new Array(); url: string = 'http://localhost:8080/quotes-reactive'; getQuoteStream(page?: number, size?: number): Observable<Array<Quote>> { this.quotes = new Array(); return Observable.create((observer) => { let url = this.url; let eventSource = new EventSource(url); eventSource.onmessage = (event) => { console.debug('Received event: ', event); let json = JSON.parse(event.data); this.quotes.push(new Quote(json['id'], json['book'], json['content'])); observer.next(this.quotes); }; eventSource.onerror = (error) => observer.error('EventSource error: ' + error); }); }
  • 16. How do you test? • curl -H "Accept: text/event-stream" http://localhost:8080/quotes-reactive • Spring WebClient • Spring WebTestClient • Postman? Soapui? Browser?
  • 17. Thoughts on Reactive Spring • Pros • Safer concurrency • scale with a small, fixed number of threads and less memory. • makes applications more resilient under load because they scale in a more predictable way • Baked into Spring • Java 10 Oracle Drivers Planned
  • 18. Thoughts on Reactive Spring • Cons • Debugging complexity • Steeeeeeep learning curve • Do we understand Blocking? • “it doesn’t matter if you use a Reactive Web approach in the backend, it won’t be really reactive and non-blocking unless your client is able to handle it as well”
  • 19. Finally • Source - https://github.com/roryp/full-reactive-stack • Resources • Project Reactor Site - https://projectreactor.io/ • Conal’s original paper https://github.com/conal/talk-2015-essence-and- origins-of-frp