150 likes | 164 Views
Delve into the features of Servlet Spec 3.0, including asynchronous servlets, annotation processing, and pluggability, revolutionizing web development. Discover the evolution from synchronous to asynchronous web processing.
E N D
JSR 315 – Servlet Spec 3.0 Damodar Chettydamodarchetty@gmail.com Software Engineering Solutions, Inc.www.swengsol.com
Introductions • Author, Coach, Architect • Blogger at www.swengsol.com • Two decades in software development • Adjunct Faculty at Metro State University
Java 6 - What’s New? • JSR 316 - umbrella specification • Contains 28 specs. • Web Services (JAX-WS, JAX-RS, JAXB, …) • Web (Servlet 3, JSF, JSP, JSTL, EL) • Enterprise (EJB 3, JPA, JTA, …) • Misc (Annotations, …) • Introduces Pruning and Profiles JAX-RPC, JAXR, EJB 2 CMP Web Profile 1.0 : Web, EJB Lite, JPA, JTA, Annotations
Servlet 3.0: What’s on our menu? • Asynchronous Servlets • Enhanced Modularity • Programmatic registration and configuration of components • Annotations • Security (won’t discuss this tonight)
Annotation Processing • web.xml is optional but overrides annotations • Scans WEB-INF/classes and WEB-INF/lib/*.jar • metadata-complete attribute • Annotations for components • Servlets • Filters • Listeners • Ordering is unspecified • Demo MyFilter, HelloWorldServlet @WebServlet(name=“fooServlet”, urlPatterns={“/foo”, “/bar”} asyncSupported = false) @WebFilter(urlPatterns={“/foo”, “/bar”} servletNames={“fooServlet”}) @WebListener
Programmatic Definition • Create and configureServlets, Filters, and Listeners • Useful to frameworks • DemoMyServletContextListener, MyProgrammaticFilter, MyProgrammaticServlet
Pluggability • Frameworks can plug-in to your application • Configuration • Resources • Container assembles all fragments • DemojSwengsol.jar META-INF/web-fragment.xml lets frameworks augment your web.xml META-INF/resources frameworks can serve up static and dynamic resources from your application’s WEB-INF! Supports relative or absolute ordering
Async Servlets – Prior to JSR315 • The Synchronous Web • HTTP Connections • One Server Thread Per Connection • Server Thread Pool Mechanism HTTP 1.0 and HTTP 1.1 differences Connector / Acceptor / Processor
Async Servlets – The Problem • Thread Starvation • Threads consume resources • Blocking consumes threads • Staleness • Static snapshots into a dynamic system • Ajax improves freshness, but does not solve. • Non standard solutions • Tomcat’s Comet, WebLogic’s Future Response servlet,WebSphere’s Asynchronous Request Dispatcher
Async Servlets – After JSR315 • The Asynchronous Web • Solves Thread Starvation • Solves Staleness • Solves Non Standard Solutions Processor thread delegates to async thread and returns to processing connections Client can make preemptive requests. The one true way
Async Servlets – Ingredients • Thread Pool and Queue • AsyncContext • Runnable instance • Servlet/Filter chain Executor & ExecutorService Asynchronous Execution Context wraps the request and response Encapsulates the processing of the blocking request All components in the processing chain must be marked as supporting asynchronous requests
Async Servlets – The Recipe • Context listener sets up an Executor • Servlets/filters that will block are marked as asyncSupported. • An async servlet: • invokes startAsync() on the request • wraps the returned AsyncContext in a Runnable, • hands the Runnable to an Executor for async processing. • returns without the response being committed. • The main thread returns to the server’s pool ready to handle the next request. • The Executor assigns an async thread to process the Runnable. • The Runnable: • uses the request and response stored in its AsyncContext • accesses request attributes/parameters as necessary. • stores the result in the request. • “completes” the response, by generating the response, or by dispatching to a server side resource. • The client, which has been waiting patiently, now displays the response.
State Transition Diagram Synchronous servlet Async dispatch Async complete
Async Servlets – Demo • AsyncServlet, AsyncRequestProcessor, MyAsyncListener
Thank You! • damodarchetty@gmail.comdamodarchetty@softwareengineeringsolutions.com • www.swengsol.com • 651 270 8457 Contact information for training/consultation: