intro to reactor

26
1 © Copyright 2013 Pivotal. All rights reserved. 1 © Copyright 2013 Pivotal. All rights reserved. Reactor Foundational framework for Reactive, Fast Data applications on the JVM Jon Brisbin Reactor Project Lead

Upload: jon-brisbin

Post on 26-Jan-2015

107 views

Category:

Technology


1 download

DESCRIPTION

Brief introduction to Reactor. What it is, why it's important, what it looks like to code with it.

TRANSCRIPT

Page 1: Intro to Reactor

1© Copyright 2013 Pivotal. All rights reserved. 1© Copyright 2013 Pivotal. All rights reserved.

Reactor

Foundational framework for Reactive, Fast Data applications on the JVM

Jon BrisbinReactor Project Lead

Page 2: Intro to Reactor

2© Copyright 2013 Pivotal. All rights reserved.

What is Reactor?

Reactor is a foundational library.

Plays in grey area between user-level and lower-level abstractions.

Components and application cores can be built on Reactor.

Drivers, servers, data integration libraries, domain integration libraries, evented architectures.

Page 3: Intro to Reactor

3© Copyright 2013 Pivotal. All rights reserved.

What is Reactor?

Reactor is a distillation of other libraries and best-practices.

Elements of other patterns and libraries surface throughout Reactor's abstractions.

Page 4: Intro to Reactor

4© Copyright 2013 Pivotal. All rights reserved.

What is Reactor?

Page 5: Intro to Reactor

5© Copyright 2013 Pivotal. All rights reserved.

What is Reactor?

Page 6: Intro to Reactor

6© Copyright 2013 Pivotal. All rights reserved.

What is Reactive?

“Reactive programming can be seen as a natural extension of higher-order functional programming to concurrent systems that deal with distributed state by coordinating and orchestrating asynchronous data streams exchanged by actors.”

https://www.coursera.org/course/reactive

Page 7: Intro to Reactor

7© Copyright 2013 Pivotal. All rights reserved.

What is Reactive?

Reactive Extensions in .NET– http://rx.codeplex.com/

Netflix RxJava– https://github.com/Netflix/RxJava

Observer pattern– http://en.wikipedia.org/wiki/Observer_pattern

Page 8: Intro to Reactor

8© Copyright 2013 Pivotal. All rights reserved.

How is Reactor reactive?

Reactor applications route events based on a Selector– Like a routing topic, but can be any object– $(“string”)– $(anonymousObject)

RegexSelector: R(“topic.(.+)”)

UriTemplateSelector: U(“/{path}/{segment}**”)

ClassSelector: T(Throwable.class)

JsonPathSelector: J(“$.”)

Page 9: Intro to Reactor

9© Copyright 2013 Pivotal. All rights reserved.

What does the code look like?

Page 10: Intro to Reactor

10© Copyright 2013 Pivotal. All rights reserved.

What does the code look like?

Page 11: Intro to Reactor

11© Copyright 2013 Pivotal. All rights reserved.

What does the code look like?

Page 12: Intro to Reactor

12© Copyright 2013 Pivotal. All rights reserved.

What is Fast Data (and #uberfastdata)?

High throughput: millions per second.– Selector-based dispatch: 10-15MM/sec– RingBuffer Processor: 100MM/sec

Low latency: microseconds per request.– DO. NOT. BLOCK!

High volume: billions per minute/hour/day.– High sustained throughput

Page 13: Intro to Reactor

13© Copyright 2013 Pivotal. All rights reserved.

Reactor - Dispatchers

Dispatchers manage task execution

ThreadPoolExecutorDispatcher– Backed by standard ThreadPoolExecutor

BlockingQueueDispatcher– Event Loop style

RingBufferDispatcher– LMAX Disruptor RingBuffer

SynchronousDispatcher

Page 14: Intro to Reactor

14© Copyright 2013 Pivotal. All rights reserved.

Reactor - Selectors

Can be created from any object using $(obj).– Or use the long form: Selectors.object(obj)

Can extract data from the matched key.– U(“/{path}/{segment}”) results in headers “path” and “segment”

Predicate<T> Selectors can match on domain-specific criteria – Header values– Object values– Time/Date– Moon Phase– Position of the ISS

Page 15: Intro to Reactor

15© Copyright 2013 Pivotal. All rights reserved.

Reactor - Streams

Streams allow for composition of functions on data

Callback++

Netflix RxJava Observable, JDK 8 Stream

stream.map(String::toUpperCase) .filter(new Predicate<String>() { public boolean test(String s) { … } }) .consume(s → log.info(“consumed string {}”, s));

Page 16: Intro to Reactor

16© Copyright 2013 Pivotal. All rights reserved.

Reactor - Promises

Promise<String> p;

String s = p .onSuccess(s → log.info(“consumed string {}”, s)) .onFailure(t → log.error(t.getMessage(), t)) .onComplete(t → log.info(“complete”)) .await(5, SECONDS);

p.map(String::toUpperCase).consume( s → log.info(“UC: {}”, s));

Page 17: Intro to Reactor

17© Copyright 2013 Pivotal. All rights reserved.

Reactor - Processor

Processor<Buffer> proc;

Operation<Buffer> op = proc.prepare();op.get().append(data).flip();op.commit();

proc.batch(512, buff → buff.append(data).flip());

Page 18: Intro to Reactor

18© Copyright 2013 Pivotal. All rights reserved.

Reactor - Spring@Configuration@EnableReactorpublic class ReactorConfiguration {

@Bean public Reactor input(Environment env) { return Reactors.reactor(env); }

@Bean public Reactor output(Environment env) { return Reactors.reactor(env); }

Page 19: Intro to Reactor

19© Copyright 2013 Pivotal. All rights reserved.

Reactor - Spring

@Componentpublic class SimpleHandler {

@Autowired private Reactor reactor;

@Selector(“test.topic”) public void onTestTopic(String s) { // Handle data }

}

Page 20: Intro to Reactor

20© Copyright 2013 Pivotal. All rights reserved.

Reactor - Demo

Page 21: Intro to Reactor

21© Copyright 2013 Pivotal. All rights reserved.

Reactor – Random Awesomeness

TCP Client/Server, with a Netty 4 implementation

Buffer tools

Sequencer support, for event ordering

Work Queue support, with OOTB Java Chronicle implementation

Logback Appender

Dynamic Interface-based event proxies

Page 22: Intro to Reactor

22© Copyright 2013 Pivotal. All rights reserved.

Reactor – 3rd Party Support

Meltdown: A Clojure binding by @michaelklishin & @ifesdjeen– https://github.com/clojurewerkz/meltdown

Couchbase: v2 Java SDK platform

Page 23: Intro to Reactor

23© Copyright 2013 Pivotal. All rights reserved.

Reactor – Spring Framework 4.0

STOMP– Reactor TCP handles STOMP broker relay

ReactorSubscribableChannel– SubscribableChannel implementation

Page 24: Intro to Reactor

24© Copyright 2013 Pivotal. All rights reserved.

Reactor – Spring Integration

ReactorProcessorMessageDispatcher– Pluggable into any MessageChannel that takes a MessageDispatcher

XML Namespace support– <int-reactor:syslog-inbound-channel-adapter id="syslog"

port="${port:5140}" channel="output" auto-startup="false"/>

More Integration planned in near future

Page 25: Intro to Reactor

25© Copyright 2013 Pivotal. All rights reserved.

Reactor – Spring XD

TCP + Netty: Supports all standard Netty Codecs– HTTP, FTP, SMTP, WebSocket, protobuf, etc…

Syslog: Standard syslog ingest using Reactor TCP– reactor-syslog: ~700k/sec – syslog-tcp: ~30k/sec

MessageBus: Using Selectors like topics and queues– Based on RedisMessageBus

Page 26: Intro to Reactor

BUILT FOR THE SPEED OF BUSINESS