Slide 1

Slide 1 text

Getting · Ready for Spring 5 and Reactive Daniel Fernández @danfenz

Slide 2

Slide 2 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org What is ? • Server-side template engine for Java – HTML, XML, JavaScript, CSS, text – View layer in SpringMVC, WebFlux, and others • First version 2011 — Currently 3.0.6 • 220,000+ downloads/month (core) April 2017 – Was 75,000+ a year ago!

Slide 3

Slide 3 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org It looks like this… Name Price Oranges 0.99

Slide 4

Slide 4 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org …or this… See Profile
...
[[${user.name}]]

Slide 5

Slide 5 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org …or even this, too var sourceUrl = [[@{/streams/events}]]; var source = new EventSource(sourceUrl); ... Dear [(${name})], here's a list of our current products: [# th:each="item : ${items}"] - [(${item})] [/] Regards.

Slide 6

Slide 6 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org Summary of features • Iteration, conditionals, layout, i18n… All the Basics • Non-intrusive for design Natural Templates • Non-intrusive for client-side code Coexist with JS • Extensibility Custom Dialects

Slide 7

Slide 7 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org SPRING 5
 SPRING 5 MVC

Slide 8

Slide 8 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org Very little effort to adapt • Unaffected by most changes – Java 8 Thymeleaf still supporting Java 6 ¯\_(ϑ)_/¯ – Package refactoring, tech support cleanup • New integration module – thymeleaf-spring5 – 3.0.6.M4, will be .RELEASE when Spring 5 is too

Slide 9

Slide 9 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org SPRING 5 WEBFLUX

Slide 10

Slide 10 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org That's a different story • WebFlux APIs are equivalent to MVC's • Still, they are other APIs – RequestContext, BindStatus… – No Servlet-based Request/Response objects • Parameters, attributes, link building, output – View/ViewResolver

Slide 11

Slide 11 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org The big one: WebFlux is reactive • Thymeleaf 3.0 is not a reactive technology – Being reactive needs being coded reactive • Evaluate: How much blocking do we have? – Template reading: do cache! Still, cache blocks :( – Blocking data retrieval Not Thymeleaf's fault – Output

Slide 12

Slide 12 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org The issue with output • No HttpResponse.getWriter() – No "send to the browser as you render it" • Reactive output: Publisher – Mono with the whole output? • Formally valid, but what about memory usage? – Flux? • We are subject to backpressure • We need to stop the engine after each DataBuffer • Being a hot source is not an option for a template engine

Slide 13

Slide 13 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org Thymeleaf is input-driven • Thymeleaf 3 is (normally) input-driven – Parser/cache produce template model events – Events are processed in sequence, produce output S:table S:tr S:td T:"John" E:td ... John ... process()

Slide 14

Slide 14 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org Engine throttling • Throttling allows it to stop and resume – Whenever it has written n bytes of output – Engine might need to hibernate after any event • Not in the middle of an event, so a tiny bit of buffering S:table S:tr S:td processThrottled() T:"John" E:td ... John ... processThrottled() STOP OUTPUT

Slide 15

Slide 15 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org Reactive context variables • Publisher variables in context (model) • WebFlux resolves them all – Before asking the View to render – In a non-blocking way – Transparent to Thymeleaf • What if we don't want them all resolved?

Slide 16

Slide 16 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org Data-driven execution • A Publisher driving template execution – Special variable in context – Only one, and it has to be iterated (th:each) – HTML rendered as values are published • Thymeleaf = Processor – Subscriber – Publisher

Slide 17

Slide 17 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org Server-Sent Events (SSE) • Data-driven execution, 1 iteration = 1 event – Thymeleaf's output channels must understand SSE event: head id: 0 data: event: message id: 1 data: data: John Apricot data: event: message id: 2 data: data: Josephine Watermelon data: event: tail id: 3 data:

Slide 18

Slide 18 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org Reactive-friendly operation modes Publisher Data Iterable CONTEXT "\n\n..." String Mono OUTPUT CHANNEL Publisher Data Iterable CONTEXT Flux OUTPUT CHANNEL Publisher CONTEXT Flux OUTPUT CHANNEL 1 DATA-DRIVEN CHUNKED FULL

Slide 19

Slide 19 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org SPRING BOOT 2.0

Slide 20

Slide 20 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org Spring Boot 2.0 awesomeness • Uses Thymeleaf 3.0 by default • Main way to develop Spring WebFlux apps • Supports thymeleaf-spring5 • Autoconfigures Thymeleaf + WebFlux! org.springframework.boot spring-boot-starter-thymeleaf

Slide 21

Slide 21 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org MATCHDAY! (DEMO TIME)

Slide 22

Slide 22 text

@spring_io #springio17 @danfenz · @thymeleaf http://thymeleaf.org THANKS! Q&A? https://github.com/danielfernandez/reactive-matchday