狠狠撸

狠狠撸Share a Scribd company logo
#JCConfTaiwan 2017
Reactive Programming
with Reactor
Swanky Hsiao (史旺基)
http://swanky.github.io/
#JCConfTaiwan 2017
Reactive?
Programming
Nonblocking?
Asynchronous
#JCConfTaiwan 2017
Reactor Flux
#JCConfTaiwan 2017
Reactor Mono
#JCConfTaiwan 2017
Operators + Functional Programming
filter
flatMap
publishOn
just fromArray fromIterable
empty
error
never
map
collect
subscribeOn
zipWith
#JCConfTaiwan 2017
Blocking Synchronous Call
class	MyService	{?
?
				boolean	doSomething(){?
								//	…	sync	&	blocking	?
				}?
?
}
#JCConfTaiwan 2017
Reactive Call
class	MyService	{?
?
				boolean	doSomething(){?
								//	…	sync	&	blocking?
				}?
?
				boolean	reactiveDoSomething(){?
								Mono.fromCallable(()	->	doSomething())?
												.subscribeOn(Schedulers.elastic())?
												.subscribe();?
				}?
?
}
#JCConfTaiwan 2017
史旺基 (蕭宇程)
《制服.女孩 × 史旺基》系列列與
《?高校制服戀物論》攝影作者

朱學恒「阿宅反抗軍」及?
「制服地圖」特約攝影師

國立臺灣師範?大學資訊?工程博?士

JWorld@TW認證版主

?目前任職於資策會
#JCConfTaiwan 2017
Reactive?
#JCConfTaiwan 2017
#JCConfTaiwan 2017
Reactive Systems
Reactive Programming
Functional Reactive?
Programming (FRP)
#JCConfTaiwan 2017
FRP
ICFP 1997
Ref: http://conal.net/papers/icfp97/
#JCConfTaiwan 2017
Reactive Manifesto
Ref: https://www.reactivemanifesto.org/
#JCConfTaiwan 2017
Responsive
React to its users
#JCConfTaiwan 2017
Resilient
React to failure and stay available
#JCConfTaiwan 2017
Elastic
React to variable load conditions
#JCConfTaiwan 2017
Message-driven
React to inputs
#JCConfTaiwan 2017
Reactive Programming
Combination of
?Functional
Programming
?Event Based
Programming
?Loose Coupling
?Error Resilience
#JCConfTaiwan 2017
Blocking APIs
Ref: https://www.lightbend.com/reactive-programming-versus-reactive-systems
#JCConfTaiwan 2017
call model makeup
dress up
photo shooting
Ref: https://www.lightbend.com/reactive-programming-versus-reactive-systems
#JCConfTaiwan 2017
Messaging
Ref: https://www.lightbend.com/reactive-programming-versus-reactive-systems
#JCConfTaiwan 2017
call model
makeup
dress up
photo shooting
Ref: https://www.lightbend.com/reactive-programming-versus-reactive-systems
#JCConfTaiwan 2017Ref: https://projectreactor.io/learn
#JCConfTaiwan 2017Ref: https://projectreactor.io/learn
#JCConfTaiwan 2017Ref: https://projectreactor.io/learn
#JCConfTaiwan 2017
Reactive API Implementation
Callback-based
Future/Promise
Reactive Stream
#JCConfTaiwan 2017
Callback Hell
#JCConfTaiwan 2017
Ref. Callback
? Event Handling and Callback
Methods with Java in Android?
https://youtu.be/8cufpQlHUOE
#JCConfTaiwan 2017
Future/Promise
#JCConfTaiwan 2017
Ref. Future
? [JCConf 2015] 使?用 Java 的
Future/Promise API 來來撰寫非同
步程式 by kojilin?
https://youtu.be/_EqitUOUpes
? Reactive Programming Patterns
with Java 8 Futures?
https://youtu.be/tiJEL3oiHIY
#JCConfTaiwan 2017
Reactive Stream
[JDK	9]?
java.util.concurrent.Flow
http://www.reactive-streams.org/
#JCConfTaiwan 2017
Stream
List<Model>	myModels	=	models.stream()	
				.filter(m	->	m.getAge()	<	22)	
				.filter(m	->	m.getHeight()	>	168)	
				.filter(m	->	m.getWeight()	<	50)	
				.map(Model::makeup)	
				.filter(Model::isTheSamePerson)	
				.distinct()	
				.sorted()	
				.collect(Collectors.toList());
#JCConfTaiwan 2017https://zeroturnaround.com/rebellabs/java-8-streams-cheat-sheet/
#JCConfTaiwan 2017
Ref. Stream
? Get a Taste of Lambdas and Get
Addicted to Streams?
https://youtu.be/1OpAgZvYXLQ
#JCConfTaiwan 2017
Project Reactor Start - 2012
Reactor 1.x - 2013
Reactor 2.0 - 2015 April
Reactor 2.5 - 2016 Feb
Reactor 3.0 - 2016 Aug
Reactor 3.1 - 2017
https://projectreactor.io/
#JCConfTaiwan 2017
#JCConfTaiwan 2017
#JCConfTaiwan 2017
#JCConfTaiwan 2017
#JCConfTaiwan 2017
Reactor Core Features
? Flux, an asynchronous sequence of 0-n items
? Mono, an asynchronous 0-1 result
? Simple ways to create a Flux/Mono and to
subscribe to it
? Programmatically creating a sequence
? Schedulers
? Handling Errors
? Processor
#JCConfTaiwan 2017
Flux / Mono
#JCConfTaiwan 2017
Flux Mono
#JCConfTaiwan 2017
Operators
? Creating
? Transforming
? Filtering
? Combining
? Error Handling
? Utility
? Conditional
and Boolean
? Mathematical
and Aggregate
? Converting
? Connectable
? Backpressure
#JCConfTaiwan 2017
#JCConfTaiwan 2017
Creating Flux/Mono: just
Flux<String>	seq	=?
				Flux.just("foo",	"bar",	"foobar");
Mono<String>	data	=	Mono.just("foo");
#JCConfTaiwan 2017
Creating Flux: fromXxx
List<String>	iterable	=	Arrays.asList("foo",	"bar",	"foobar");	
Flux<String>	seq	=	Flux.fromIterable(iterable);
#JCConfTaiwan 2017
Creating Mono: fromXxx
#JCConfTaiwan 2017
Creating Flux: range
Flux<Integer>	ints	=	Flux.range(1,	3);
#JCConfTaiwan 2017
Publisher Subscriber
pull
push
#JCConfTaiwan 2017
Publisher Subscriber
subscribe(:Subscriber)
Subscription
onSubscribe(:Subscription)
request(n)/cancel()
onNext(:Data)
onError()/onComplete()
#JCConfTaiwan 2017
Publisher SubscriberProcessor Processor
#JCConfTaiwan 2017
Request to Start streaming data:
subscribe
#JCConfTaiwan 2017
Flux<String>	flux	=	Flux.just("foo",	"chain");	
flux	=	flux.map(secret	->	secret.replaceAll(".",	"*"));
flux.subscribe(next	->	System.out.println("Received:	"	+	next));
Lazy Evaluation
…
#JCConfTaiwan 2017
Scheduler Threading
? Schedulers.immediate()?
current thread
? Schedulers.single()?
single, reusable thread
? Schedulers.elastic()?
elastic thread pool
? Schedulers.parallel()?
fixed pool of workers(CPU cores)
?Schedulers.fromExecutorService(ExecutorService)
#JCConfTaiwan 2017
Scheduler Threading (ex: Flux)
Ref: https://stackoverflow.com/a/41941592
Flux.just("a",	"b",	"c")	
				.doOnNext(v	->	System.out.println("before	publishOn:	"	+	Thread.currentThread().getName()))	
				.publishOn(Schedulers.elastic())	
				.doOnNext(v	->	System.out.println("after	publishOn:	"	+	Thread.currentThread().getName()))	
				.subscribeOn(Schedulers.parallel())	
				.subscribe(v	->	System.out.println("received	"	+	v	+	"	on	"	+	Thread.currentThread().getName()));
before	publishOn:	parallel-1	
before	publishOn:	parallel-1	
before	publishOn:	parallel-1	
after	publishOn:	elastic-2	
received	a	on	elastic-2	
after	publishOn:	elastic-2	
received	b	on	elastic-2	
after	publishOn:	elastic-2	
received	c	on	elastic-2
Output:
#JCConfTaiwan 2017
Reactor Core
Reactor Test
Reactor Netty
Reactor Adapter
Reactor Kafka
Reactor Extra
#JCConfTaiwan 2017
#JCConfTaiwan 2017
Reactor
Reactive?
Programming
#JCConfTaiwan 2017
Ref. Reactive Programming with Reactor
? Project Reactor?
https://projectreactor.io/
? Reactive Programming versus Reactive Systems?
https://www.lightbend.com/reactive-programming-versus-reactive-systems
? Reactive Programming with Reactor 3?
https://tech.io/playgrounds/929/reactive-programming-with-reactor-3/Intro
? What Are Reactive Streams in Java??
https://dzone.com/articles/what-are-reactive-streams-in-java
? 5 Things to Know About Reactive Programming?
https://dzone.com/articles/5-things-to-know-about-reactive-programming
? Java 9 Flow API – Reactive Streams?
http://javasampleapproach.com/java/java-9/java-9-flow-api-reactive-streams
#JCConfTaiwan 2017
Thank You!

More Related Content

[JCConf 2017] Reactive Programming with Reactor