NL-JUG JFall 2014: Reactive programming met Java 8 en Java EE 7

Post on 07-Jul-2015

298 views 2 download

Tags:

description

Reactive programming met Java 8 en Java EE 7, transform a synchronous, blocking REST-service to a non-blocking, asychronous, reactive service with completable futures

Transcript of NL-JUG JFall 2014: Reactive programming met Java 8 en Java EE 7

Reactive programming met Java SE 8 en Java EE 7

Martijn Blankestijn

Agenda

● Context

● Sequence, zonder futures

● Futures

● CompletableFuture

● Round-up

Context

Our responsiblity

@Path("customers") public class DemoOverviewResource {

@GET @Path("{username}") @Produces(APPLICATION_JSON) public CustomerOverview retrieve( @PathParam("username") String username) {

Customer customer = getCustomerInfo(username);

Contract[] contracts = getContracts(customer); Communication[] communications = getCommunications(customer);

return createOverview( customer, contracts, communications); }

Customer

Contract

Communications

TIME

Sequential

A Future

represents

the result

of an

asynchronous computation

public interface Future<V> {

boolean isDone();

V get()

V get(long timeout, TimeUnit unit)

Customer

Contract

Communications

TIME

Sequential

Futures

Reactive programmingis a programming paradigm

oriented around

data flows

and the

propagation of change

CompletableFutureA Future that may be

explicitly completed

and may trigger actions

upon its completion.

Chaining methods

runthenAcceptthenApply

thenComposewhenComplete

Joining methods

runAfterBoththenAcceptBoththenCombine...

allOf

Model the flow

Customer

Contract

Communications

TIME

Sequential

Futures

Completable Futures

Links

● Demo: https://github.com/martijnblankestijn/reactive-rest-demo

● JAX-RS

● Completable Future

● ManagedExecutor: JSR 236 Concurrency Utilities for Java EE

● Reactive Manifesto