Download - Reactive Programming in Spring 5
Reactive Programming in Spring 5
Arjen PoutsmaPivotal
About Arjen
• Twenty years of experience in Enterprise Software Development
• Joined SpringSource in 2005
• Development lead of Spring Web Services, Spring REST support, Spring Scala
• Working on Spring 5
Agenda
• Reactive Systems
• Reactive Streams
• RxJava
• Reactor
• Reactive Programming in Spring 5
Reactive Systems
Reactive Manifesto
Responsive
Message Driven
ResilientElastic
Reactive Manifesto• Responsive
• Rapid
• Consistent
• Resilient
• Replication
• Isolation
• Elastic
• Scaling
• No bottlenecks
• Message Driven
• Async
• Back-pressure
Reactive Streams
Reactive Streams Spec
• Focus on Interoperability
• No operators
• Wide support
• Akka, Vert.x, RxJava, Ratpack
• JDK 9 Flow in java.util.concurrent
Publisher Subscriber
Publisher Subscribersubscribe
Publisher Subscriber
Publisher Subscriber
Publisher Subscriber
onSubscribe
Subscription
Publisher Subscriber
Subscription
Subscription
Publisher Subscriber
Subscription
Publisher Subscriber
request
2
Subscription
Publisher Subscriber
2
Subscription
Publisher Subscriber
2
Subscription
Publisher Subscriber
onNext
1
Subscription
Publisher Subscriber
1
Subscription
Publisher Subscriber
onNext
0
Subscription
Publisher Subscriber
0
Subscription
Publisher Subscriber
request
5
Subscription
Publisher Subscriber
5
Subscription
Publisher Subscriber
5
Subscription
Publisher Subscriber
onNext
4
Subscription
Publisher Subscriber
4
Subscription
Publisher Subscriber
onNext
3
Subscription
Publisher Subscriber
3
Subscription
Publisher Subscriber
onNext
2
Subscription
Publisher Subscriber
2
Subscription
Publisher Subscriber
onComplete
2
Publisher Subscribersubscribe
onSubscribeSubscription
request
onNext
onComplete
onError
Hot & Cold
Cold Hot
Passive Active
Publish when subscribed
Publish regardless
Queue Mouse Movement
Operators
• map, flatMap, take, subscribe, …
• No Operators in Reactive Streams
• Left to implementations
Reactor
Project Reactor
• Stream extends Publisher
• Wrap Publisher
Reactor
Streams.just('a' 'b' 'c') .take(2) .map(Character::toUpperCase) .consume(System.out::println);
RxJava
RxJava
• Based On Reactive Extensions
• Ported to many languages
• Rx.NET, RxJava, RxJS, ...
• Native support for Reactive Streams coming in 2.x
RxJava
Observable.just('a', 'b', 'c') .take(2) .map(Character::toUpperCase) .subscribe(System.out::println)
RxNetty
• RxJava on top of Netty
• Client/server for TCP and HTTP
• Backpressure support
Spring 5
Spring Reactive
• Experimental work for Spring 5
• Non-blocking runtimes:
• Netty, Jetty, Tomcat, (Undertow)
• RxJava, Reactor
https://github.com/spring-projects/spring-reactive
public interface ServerHttpRequest { HttpMethod getMethod(); URI getURI(); InputStream getBody(); }
public interface ServerHttpResponse { void setStatusCode(HttpStatus status); OutputStream getBody();}
HttpMessage
public interface ReactiveServerHttpRequest { HttpMethod getMethod(); URI getURI(); Publisher<ByteBuffer> getBody(); }
public interface ServerHttpResponse { void setStatusCode(HttpStatus status); Publisher<void> setBody(Publisher<ByteBuffer> body);}
Reactive HttpMessage
Reactive Wep App
Web Service Data
Wanted: Reactiveness• JDBC
• MySQL
• Postgres
• NoSQL
• MongoDB
• HTTP Client
• OkHttp
• Spring 5
• …
Reactor
@RequestMapping("/capitalize")@ResponseBodypublic Stream<Person> capitalize(
@RequestBody Stream<Person> persons) { return persons.map(person -> { person.setName(person.getName().toUpperCase()); return person; });}
RxJava
@RequestMapping("/capitalize")@ResponseBodypublic Observable<Person> capitalize(
@RequestBody Observable<Person> persons) { return persons.map(person -> { person.setName(person.getName().toUpperCase()); return person; });}
More Spring!
Spring Meetup
http://www.meetup.com/Dutch-Spring-Meetup/
Spring Track GOTO Amsterdam 2016
http://gotoams.com
Q & A
https://github.com/spring-projects/spring-reactive