Top Banner
35

HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Jan 22, 2018

Download

Technology

concrete
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)
Page 2: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

HTTP/2 and java 9

Gustavo Oliveira & Francisco de Assis

Page 3: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Gustavo OliveiraFrancisco de Assis

Page 4: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Summary

•Short history of HTTP/2

•Advantages & Disadvantages

•HTTP/2 & JAVA 9

•Synchronous request

•Asynchronous request

Page 5: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Short history of HTTP/2

Page 6: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

q

HTTP2

● 2007 - Best Practices for Speeding Up Your Web Site from Yahoo

● 2012 - SPDY from Google

● 2015 - HTTP/2

Page 7: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

HTTP 1 vs HTTP/2

Page 8: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

q

HTTP2

HTTP 1 HTTP/2Queue request Requests by multiplexing, single TCP

connection

Request stateless Requests statefull

Without security HTTPS by default

Server push

HPACK compression and GZIP

Request priorization

Page 9: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Multiplexing

Page 10: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Multiplexing

Page 11: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Server Push

Page 12: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

HTTP2

Server Push

● Send resources inline● Internal HTTP2’s resources

Page 13: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Image Example Without push

Page 14: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Advantages & Disadvantages

Page 15: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

HTTP/2 into JAVA 9

Page 16: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

HTTP2

HTTP/2 & JAVA 9

● What is new?○ Incubator Modules○ new HTTP/2 api

● jshell --add-modules jdk.incubator.httpclient● import jdk.incubator.http.*● Main Classes: HttpClient, HttpRequest,

HttpResponse

Page 17: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Comparing Old vs New

OLD

NEW

Page 18: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

JSHELL

Jshell e Atalhos

● Jshell is a REPL● Implicity variables (Snippets) ● Feedback (verbose e silent)● jshell --add-modules jdk.incubator.httpclient (adiciona modulo)

/exit exit of jshell

/set feedback Feedback

/list To list snippets

/drop id-snippets Remove a snippet

Shift+tab+i Increase a import

Ctrl+a Move Cursor para inicio da linha

Ctrl+e Move cursos para final da linha

Alt+b Volta uma palavra

Alt+f Avança uma palavra

/imports lista os imports

$id-snippet mostra o valor do snippet

Page 19: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Synchronous connection

URI uri = new URI("https://www.google.com.br/");HttpClient client = newHttpClient();HttpRequest request = newBuilder().uri(uri) .GET() .build();

HttpResponse<String> response = client.send(request, asString());

System.out.println(response.body());

Page 20: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Asynchronous connection

URI uri = new URI("https://www.google.com.br/");HttpClient client = newHttpClient();HttpRequest request = newBuilder().uri(uri) .GET() .build();

client.sendAsync(request, asString()) .whenComplete((r, t) -> System.out.println(r.body()));

Page 21: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Async API

Page 22: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

HTTP2

The new api can sent asynchronous request easily

● The process stand in background, while the main thread stand free for other tasks● httpClient.sendAsync(request, ...)● Returns CompletableFuture

final CompletableFuture<HttpResponse<String>> response;response = HttpClient.newHttpClient() .sendAsync(request);

● isDone verify if the answers is done

if(response.isDone()){

System.out.println(response.get().body());} else {

System.out.println("cancelando o request");response.cancel(true);

}

Callback:

response.whenComplete((r,t) -> System.out.println(r.body()));

Send and callback:

HttpClient.newHttpClient() .sendAsync(request) .whenComplete((r, t) -> System.out.println(r.body()));

Page 23: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Github

● Gustavo Oliveira - https://github.com/guuhworship● Francisco de Assis - https://github.com/assisjr

Page 24: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

HTTP2

Referencias

● https://blog.chromium.org/2013/11/making-web-faster-with-spdy-and-http2.html● https://www.shimmercat.com/en/docs/1.5/everything-in-one-go/● http://blog.caelum.com.br/performance-web-no-mundo-real-porque-o-site-do-alura-voa/● https://www.youtube.com/watch?v=_xF2dgB5NDk● https://www.youtube.com/watch?v=kwOp3jr-EhI● https://gtmetrix.com/reports/concretesolutions.com.br/l0rMmWlr● https://www.youtube.com/watch?v=nLTqSWomldc● http://blog.caelum.com.br/http2-server-push-na-pratica/● http://blog.caelum.com.br/as-fantasticas-novidades-do-http-2-0-e-do-spdy/● https://www.smashingmagazine.com/2017/04/guide-http2-server-push/

● https://www.youtube.com/watch?v=0L5Q_897fwk● https://loadimpact.com/● http://blog.caelum.com.br/spdy-http2-e-por-que-voce-deveria-conhece-los/● https://www.youtube.com/watch?v=RjnNaKLfjEU● http://blog.caelum.com.br/otimizacoes-na-web-e-o-carregamento-assincrono/●● https://hipsters.tech/http2-magia-com-o-novo-protocolo/● http://blog.caelum.com.br/otimizacoes-na-web-e-o-carregamento-assincrono/● https://www.youtube.com/watch?v=kwOp3jr-EhI● http://www.http2demo.io/● https://www.youtube.com/watch?v=WfA1uDAgXf8● https://www.youtube.com/watch?v=2QVxUuTHLus

Page 25: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

q

Centro

Av. Presidente Wilson,

231 - 29º andar

(21) 2240-2030

Cidade Monções

Av. Nações Unidas,

11.541 - 3º andar

(11) 4119-0449

Savassi

Av. Getúlio Vargas, 671

Sala 800 - 8º andar

(31) 3360-8900

www.concrete.com.br

Page 26: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Tipos de Retorno

Page 27: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

q

HTTP2

BodyHandler.as

ele le transforma o resultado

asByteArrayasByteArrayConsumerasFileasFileDownloadasSTring

Page 28: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Redirects

Page 29: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

HTTP2

Redirects não acompanha a api do java9 por padrão

HttpClient.Redirect.*NEVER: Não acompanha nenhum tipo de redirecionamentoALWAYS: Acompanha independente da origem do protocoloSAME_PROTOCOL: só para o mesmo protocoloSECURE: Sempre ocorre, a não ser que as requisições https tenta ser redirecionada para http

HttpClient.newBuilder().followRedirects(HttpClient.Redirect.SECURE).version(HttpClient.Version.HTTP_2).build().send(HttpRequest.newBuilder().uri(new URI("https://www.google.com")).GET().build(), HttpResponse.BodyHandler.asString()).body();

Page 30: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Tratando o Retorno

Page 31: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

HTTP2

HttpClient.newHttpClient().sendAsync(HttpRequest.newBuilder().uri(new URI("http://insight.dev.schoolwires.com/HelpAssets/C2Assets/C2Files/C2ImportCalEventSample.csv")).GET().build(), BodyHandler.asFile(Paths.get("example.csv"))).whenComplete((r,t) -> System.out.println("arquivo salvo: " + r.body().toAbsolutePath()));

Page 32: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Imports estáticos

Page 33: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

HTTP2

HTTP/2 e JAVA 9

● import static jdk.incubator.http.HttpClient.newHttpClient● import static jdk.incubator.http.HttpRequest.newBuilder;● import static jdk.incubator.http.HttpResponse.BodyHandler● .asString;●

HttpResponse<String> response = newHttpClient().send(newBuilder().uri(new URI("https://www.google.com.br")).GET().build(), asString());

Page 34: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

Multiplexing

Page 35: HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

HTTP2

Server Push

● Link: <style.css>; rel=preload; as=style

Example in JAVA: