Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

CON5898 What Servlet 4.0 Means To You

Shing Wai Chan and Ed Burns present their vision for Servlet 4.0, which brings HTTP 2.0 to the Java EE platform

  • Login to see the comments

CON5898 What Servlet 4.0 Means To You

  1. 1. HTTP 2 Comes to Java What Servlet 4.0 Means to You Ed Burns and Shing Wai Chan Java EE Specifica=on Team Oracle Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
  2. 2. Safe Harbor Statement The following is intended to outline our general product direc=on. It is intended for informa=on purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or func=onality, and should not be relied upon in making purchasing decisions. The development, release, and =ming of any features or func=onality described for Oracle’s products remains at the sole discre=on of Oracle. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 3
  3. 3. Our Plan for Your Time Investment Why HTTP 2? HTTP 2 Big Features How Servlet Might Expose These Features Java SE 9 Support for HTTP 2 Summary and Current Status Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 1 2 3 4 5 S 4
  4. 4. Our Plan for Your Time Investment Why HTTP 2? HTTP 2 Big Features How Servlet Might Expose These Features Java SE 9 Support for HTTP 2 Summary and Current Status Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 1 2 3 4 5 S 5
  5. 5. . . . Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Why HTTP 2? A Real Life Example S index.html style1.css style2.css script1.js . . . script9.js pic1.jpg pic8.jpg photo1.png photo2.png . . .
  6. 6. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Why HTTP 2? Problems in HTTP 1.1 • HTTP Pipelining • Head-­‐of-­‐Line blocking S style1.css style2.css Client Server index.html index.html style1.css style2.css script1.js script2.js . . . script1.js script2.js . . .
  7. 7. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Why HTTP 2? Problems in HTTP 1.1 • Inefficient use of TCP sockets S Client Server Client Server Client Server Client Server Client Server Client Server
  8. 8. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Why HTTP 2? What is an opEmizaEon? • Much of what we do in web-­‐apps is a hack to work around shortcomings in HTTP 1.1 – File concatena=on and image sprites – Domain sharding – Inlined assets E 9
  9. 9. File Concatena=on and Image Sprites TCP Efficiency Improves with Larger Files Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | E 10
  10. 10. File Concatena=on and Image Sprites TCP Efficiency Improves with Larger Files Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | E 11 • Modern web page now consists of more than 90 resources fetched from 15 dis=nct hosts • Solu=on: – Just work around it by shoving more than one logical file into one physical file. – Seminal ar=cle: A List Apart hPp://alistapart.com/arEcle/sprites – Useful tool: SpritePad hPp://spritepad.wearekiss.com/
  11. 11. File Concatena=on and Image Sprites TCP Efficiency Improves with Larger Files Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | E 12 .ic-AerospaceAndDefense-wht-on-gray, .ic-AerospaceAndDefense-wht-on-red, .ic-Airline-wht-on-gray, .ic-Airline-wht-on-red{ background: url(sprites.png) no-repeat; } .ic-AerospaceAndDefense-wht-on-gray{ background-position: 0 0; width: 80px; height: 80px; } .ic-AerospaceAndDefense-wht-on-red{ background-position: -81px 0; width: 80px; height: 80px; } .ic-Airline-wht-on-gray{ background-position: 0 -80px ; width: 80px; height: 80px; } .ic-Airline-wht-on-red{ background-position: -81px -79px ; width: 80px; height: 80px; }
  12. 12. Domain Sharding Split page resources across several hosts to work around browser limits Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 13
  13. 13. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Inlined Assets Base64 Encoding Will Never Die • data URLs • <img src="data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/ 7LZv/0jvb29t/f3//Ub/ /ge8WSLf/rhf/3kdbW1mxsbP//mf/// yH5BAAAAAAALAAAAAAQAA4…" /> E 14
  14. 14. Our Plan for Your Time Investment Why HTTP 2? HTTP 2 Big Features How Servlet Might Expose These Features Java SE 9 Support for HTTP 2 Summary and Current Status Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 1 2 3 4 5 E 15
  15. 15. • Request/Response Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | mul=plexing • Binary Framing • Stream Priori=za=on • Server Push • Header Compression • Upgrade from HTTP 1.1 • ALPN (or NPN) • 101 Switching Protocols 16 HTTP/2 Big Ticket Feature Review E
  16. 16. HTTP/2 Request Response Mul=plexing Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Fully bi-­‐direc=onal • Enabled by defining some terms – Connec&on A TCP socket – Stream A “channel” within a connec=on – Message A logical message, such as a request or a response – Frame The smallest unit of communica=on in HTTP/2. 17 Lets you do more things with a single TCP connecEon S
  17. 17. HTTP/2 Request Response Mul=plexing Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 18 ConnecEons, Streams, Messages, Frames S
  18. 18. HTTP/2 Request Response Mul=plexing Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Once you break the communica=on down into frames, you can interweave the logical streams over a single TCP connec=on. • Yet another idea from the 1960s is new again. 19 ConnecEons, Streams, Messages, Frames S
  19. 19. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | HTTP/2 Binary Framing • Solves Head-­‐Of-­‐Line (HOL) blocking problem • Type field can be DATA, HEADERS, PRIORITY, RST_STREAM, SETTINGS, PUSH_PROMISE, PING, GOAWAY, WINDOW_UPDATE, CONTINUATION 20 Enabled by dumping newline delimited ASCII S Length (24) Type (8) Flags (8) R Stream IdenEfier (31) Frame Payload (0 …)
  20. 20. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | HTTP/2 Binary Framing GET /index.html HTTP/1.1 Host: example.com Accept: text/html 21 Example 1 S HEADERS + END_STREAM + END_HEADERS :method: GET :scheme: htp :path: /index.html :authority: example.org accept: text/html
  21. 21. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | HTTP/2 Binary Framing HTTP/1.1 200 OK Content-­‐Length: 11 Content-­‐Type: text/html Hello World 22 Example 2 S HEADERS -­‐ END_STREAM + END_HEADERS :status: 200 content-­‐length: 11 content-­‐type: text/html DATA + END_STREAM Hello World
  22. 22. HTTP/2 Stream Priori=za=on Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Stream Dependency in HEADERS Frame • PRIORITY frame type • An addi=onal 40 bytes – Stream id (31) – Weight (8): [1, 256] – Exclusive bit (1) • Only an advice 23 S A B C 4 12 A B D C 4 16 12 exclusive = 0 A D 16 B C 4 12 exclusive = 1
  23. 23. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | HTTP/2 Server Push • Eliminates the need for resource inlining. • Lets the server populate the browser’s cache in advance of the browser asking for the resource to put in the cache. • No corresponding JavaScript API, but can be combined with SSE – Server pushes stuff into the browser’s cache. – Server uses SSE to tell the browser to go fetch it (but we know it’s already in the browser’s cache). 24 E
  24. 24. HTTP/2 Header Compression Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Observa=on: most of the headers are the same in a given stream – Host: Accept: user-­‐agent: etc. • Why send them every =me? • Have the server and the client keep tables of headers, then just send references and updates to the tables. 25 Known as HPACK E
  25. 25. HTTP/2 Upgrade from HTTP 1.1 Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Not secure – We have to use port 80 – Use exis=ng 101 Switching Protocols from HTTP 1.1 • Secure – Next Protocol Nego=a=on (NPN) – Applica=on Layer Protocol Nego=a=on 26 Secure or not-­‐secure? E
  26. 26. Our Plan for Your Time Investment Why HTTP 2? HTTP 2 Big Features How Servlet Might Expose These Features Java SE 9 Support for HTTP 2 Summary and Current Status Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 1 2 3 4 5 E 27
  27. 27. Servlet 4.0 Big Ticket New Features Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Exis=ng API is designed for One Request == One Response. • HTTP/2 destroys this assump=on. • It will be challenging to do jus=ce to the new reality of One Request == One or More Responses. • We must not simply bolt the “One or More Responses” concept onto some convenient part of the exis=ng API. 28 Challenges in Exposing HTTP/2 Features in Servlet API S
  28. 28. Servlet 4.0 Big Ticket New Features Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Request/Response mul=plexing • Binary Framing • Stream Priori=za=on • Server Push • Header Compression • Upgrade from HTTP 1.1 – ALPN or (NPN) – 101 Switching Protocols 29 HTTP/2 Features PotenEally Exposed in Servlet API S
  29. 29. Servlet 4.0 Big Ticket New Features Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Add method HttpServletRequest and HttpServletResponse – int getStreamId() 30 HTTP/2 Features PotenEally Exposed in Servlet API S
  30. 30. Servlet 4.0 Big Ticket New Features Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Add a new class Priority – boolean exclusive – int streamId – int weight • Add method to HttpServletRequest – Priority getPriority() • Add methods to HttpServletResponse – Priority getPriority() – void setPriority(Priority p) 31 Stream PrioriEzaEon S
  31. 31. Servlet 4.0 Big Ticket New Features Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Push resource to client for a given url and headers • May add callback for comple=on or error of a push • Not at all a replacement for WebSocket • Really useful for frameworks that build on Servlet, such as JSF 32 Server Push E
  32. 32. synthetic GET /style.css Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 33 Servlet 4.0 Big Ticket New Features Server Push Browser Server Thread A servlet.service() GET /index.html Server discovers browser will need style.css and script.js Server Thread B servlet.service() request.dispatchPushRequest("style.css") request.dispatchPushRequest("script.js") synthetic GET /script.js Server Thread C servlet.service() style.css script.js index.html E
  33. 33. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Server Push Example of PotenEal Use from JSF public class FacesServlet implements Servlet { public void service(ServletRequest req, ServletResponse resp) throws IOException, ServletException { //.. HttpServletRequest request = (HttpServletRequest) req; try { ResourceHandler handler = context.getApplication().getResourceHandler(); if (handler.isResourceRequest(context) || request.isPushRequest()) { handler.handleResourceRequest(context); } else { lifecycle.attachWindow(context); lifecycle.execute(context); lifecycle.render(context); } } } E 34
  34. 34. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Server Push Example of PotenEal Use from JSF public class ExternalContextImpl extends ExternalContext { //… public String encodeResourceURL(String url) { if (null == url) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "url"); throw new NullPointerException(message); } Map attrs = getResourceAttrs(); ((HttpServletRequest) request).dispatchPushRequest(url, attrs); return ((HttpServletResponse) response).encodeURL(url); } //… } E 35
  35. 35. Elas=c Resilient Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Reac=ve Programming 36 S Responsive Message Driven image credit: reac=vemanifesto.org
  36. 36. Servlet 4.0 and Reac=ve Programming Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Non-­‐blocking IO in Servlet 3.1 – ServletInputStream • #setReadListener, #isReady – ServletOutputStream • #setWriteListener, #isReady – ReadListener • #onDataAvailable, #onAllDataRead, #onError – WriteListener • #onWritePossible, #onError S 37
  37. 37. Servlet 4.0 and Reac=ve Programming Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Asynchronous in Servlet 3.0 – ServletRequest#startAsync – AsyncContext • #addListener, #dispatch, #complete – AsyncListener • #onComplete, #onError, #onStartAsync, #onTimeout • Event-­‐driven – Server-­‐Sent Events S 38
  38. 38. Our Plan for Your Time Investment Why HTTP 2? HTTP 2 Big Features How Servlet Might Expose These Features Java SE 9 Support for HTTP 2 Summary and Current Status Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 1 2 3 4 5 E 39
  39. 39. Java SE 9 Support for HTTP/2 • JEP 110 htp://openjdk.java.net/jeps/110 Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Easy to use API • Covers only the most common use cases • Supports both HTTP 1.1 and 2 E 40
  40. 40. Java SE 9 Support for HTTP/2 Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Small footprint • Two classes (working =tles) – HtpRequestGroup • configura=on for mul=ple requests – HtpRequest • one request/response interac=on E 41
  41. 41. Java SE 9 Support for HTTP/2 Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Small footprint • Blocking mode: one thread per request/response – send request – get response • Non-­‐blocking mode: handle mul=ple request/response interac=ons in single thread using non-­‐blocking API – analogous to NIO selectors E 42
  42. 42. Java SE 9 Support for HTTP/2 Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Responses handled through lambda style callback handlers – invoked on the calling thread – Must allocate threads to responses manually • Request/Response bodies handled as InputStream/OutputStream Callback handlers E 43
  43. 43. Java SE 9 Support for HTTP/2 HttpRequestGroup group = HttpRequestGroup.create(); HttpRequest req = group.createRequest() .setRequestMethod("POST") .setRequestURI(new URI("http://www.foo.com/a/b") .setRequestBody("Param1=1,Param2=2") .onResponseHeader("X-­‐Foo", (request, name, value) -­‐> { System.out.printf(" received an X-­‐Foo header"); }) .sendRequest() .waitForCompletion(); Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | E 44
  44. 44. Java SE 9 Support for HTTP/2 HttpRequestGroup group = HttpRequestGroup.create(); HttpRequest req = group.createRequest() … .onResponseBody((HttpRequest request, InputStream in) -­‐> { if (request.getResponseCode() == 200) { Path out = Paths.get("/tmp/out"); try { Files.copy(in, out); } finally { in.close(); } } }) .sendRequest() .waitForCompletion(); Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | E 45
  45. 45. Java SE 9 Support for HTTP/2 Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | HTTP/2 features • Nego=a=on of HTTP/2 from 1.1 – ALPN or plaintext • Server Push – Support for PUSH_PROMISE frames • HPACK parameters E 46
  46. 46. Our Plan for Your Time Investment Why HTTP 2? HTTP 2 Big Features How Servlet Might Expose These Features Java SE 9 Support for HTTP 2 Summary and Current Status Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 1 2 3 4 5 S 47
  47. 47. Summary and Current Status • Servlet 4.0 brings HTTP 2 to Java EE Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | – 100% compliant implementa=on of HTTP 2 – Expose key features to the API • Server Push • Stream Priori=za=on • Request/Response mul=plexing S 48
  48. 48. Summary and Current Status • JSR-­‐369 just formed on 22 September Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Tenta=ve Delivery Schedule – Q3 2014: expert group formed – Q1 2015: early dra{ – Q3 2015: public review – Q4 2015: proposed final dra{ – Q3 2016: final release S 49
  49. 49. Safe Harbor Statement The preceding is intended to outline our general product direc=on. It is intended for informa=on purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or func=onality, and should not be relied upon in making purchasing decisions. The development, release, and =ming of any features or func=onality described for Oracle’s products remains at the sole discre=on of Oracle. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 50

×