HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

35

Transcript of HTTP2 e Java9 (Francisco Melo e Gustavo Oliveira)

HTTP/2 and java 9

Gustavo Oliveira & Francisco de Assis

Gustavo OliveiraFrancisco de Assis

Summary

•Short history of HTTP/2

•Advantages & Disadvantages

•HTTP/2 & JAVA 9

•Synchronous request

•Asynchronous request

Short history of HTTP/2

q

HTTP2

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

● 2012 - SPDY from Google

● 2015 - HTTP/2

HTTP 1 vs HTTP/2

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

Multiplexing

Multiplexing

Server Push

HTTP2

Server Push

● Send resources inline● Internal HTTP2’s resources

Image Example Without push

Advantages & Disadvantages

HTTP/2 into JAVA 9

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

Comparing Old vs New

OLD

NEW

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

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());

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()));

Async API

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()));

Github

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

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

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

Tipos de Retorno

q

HTTP2

BodyHandler.as

ele le transforma o resultado

asByteArrayasByteArrayConsumerasFileasFileDownloadasSTring

Redirects

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();

Tratando o Retorno

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()));

Imports estáticos

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());

Multiplexing

HTTP2

Server Push

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

Example in JAVA: