reactor spring one2gx_2013_0902-final

Download Reactor spring one2gx_2013_0902-final

If you can't read please download the document

Upload: stephane-maldini

Post on 16-Apr-2017

2.450 views

Category:

Technology


1 download

TRANSCRIPT

Reactor: A Foundation for Reactive FastData Applications on the JVMJon Brisbin @j_brisbinStephane Maldini @smaldini

The Spring IO Directory

Reactor Housekeeping

Tweet questions during the presentation@ProjectReactor

@j_brisbin

@smaldini

Stop us anywhere if you have a questionThere are no stupid questions, only stupid answers!

Reactor What is it?

Reactor is a foundational libraryPlays 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

Reactor What is it?

Reactor is a distillation of other libraries and best-practicesElements of other patterns and libraries surface throughout Reactor's abstractions

http://stackoverflow.com/questions/16595393/akka-or-reactor

Reactor What can I build with it?

Reactor applications are reactiveReactive Extensions in .NET

Netflix RxJava

Observer pattern

Reactor applications route events based on a SelectorLike a routing topic, but can be any object

Regex, URI template, Class.isAssingableFrom, custom logic

Reactor Landscape

Reactor What does it look like?

Environment env = new Environment();

Reactor reactor = Reactors.reactor().env(env).dispatcher(RING_BUFFER).get();

reactor.on($(topic), (Event ev) {System.out.println(Hello + ev.getData());});

reactor.notify(topic, Event.wrap(John Doe));

Reactor Selectors

Selectors are the left-hand side of an equality comparisonA Selector can be created from any object using $(obj) (or the long form: Selectors.object(obj))

A Selector can extract data from the matched key

Predicate Selectors can be created to match on domain-specific criteria like header values

Reactor Selectors

A RegexSelector will match a String by executing the regex over itR(some.(.*))

Selectors.regex(some.(.*))

reactor.on(R(some.(.+)), (Event ev) {// s will be 'topic'String s = ev.getHeaders().get(group1);});

reactor.notify(some.topic, Event.wrap(John Doe));

Reactor Selectors

A UriTemplateSelector will match a String by extracting bits from a URIU(/some/{path})

Selectors.uri(/some/{path})

reactor.on(U(/some/{topic}), (Event ev) {// s will be 'topic'String s = ev.getHeaders().get(topic);});

reactor.notify(/some/topic, Event.wrap(John Doe));

Reactor Consumer, Function, Supplier, Predicate

public interface Consumer { void accept(T t); }

public interface Supplier { T get(); }

public interface Function { V apply(T t); }

public abstract class Predicate { boolean test(T t); }

Reactor Stream

Streams allow for composition of functions on dataCallback++

Netflix RxJava Observable, JDK 8 Stream

Stream str;

str.map(String::toUpperCase) .filter(new Predicate() { public boolean test(String s) { }}).consume(s log.info(consumed string {}, s));

Reactor Promise

Promises allow for composition of functions on dataShare common functions with Stream

Promise p;

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

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

Reactor Processor

Thin wrapper around Disruptor RingBufferConverts Disruptor API to Reactor API

Uber fast performance for #UberFastData

Processor proc;

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

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

Reactor Spring

Helpers to integrate Reactor into ApplicationContext@EnableReactor for easy configuration

Wiring annotated handlers

Reactor Spring

@Configuration@EnableReactorpublic class ReactorConfiguration {

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

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

Reactor Spring

@Componentpublic class SimpleHandler {

@Autowired private Reactor reactor;

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

}

Reactor Spring

DispatcherTaskExecutor Not really a high-scale TaskExecutor

Used to get Spring components running in same thread as Reactor Consumers

ConversionService integration

PromiseHandlerMethodReturnValueHandler (!)

ReactorSubscribableChannel

Reactor Groovy

First class citizen language implementation@CompileStatic ready

Prominent use of Closure as Consumers, Functions and more

Operator overloading for elegant programming

Full Reactor system Builder

Reactor Groovy : Notification and Consumer

@CompileStatic def welcome(){reactor.on('greetings') { String s ->reply hello $sreply how are you?} reactor.notify 'greetings', 'Jon'reactor.send('greetings', 'Stephane'){ println it cancel()}}

Reactor Groovy : Promises and Streams

def promise = Promises.task { longStuff(); 1 } get()

def transformation = c | { it + 1 }transformation.await() == 2

def deferredStream = Streams.defer().get()(deferredStream & { it > 2 })