Dropwizard Internals


Published on


Published in: Software, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Dropwizard Internals

  1. 1. Dropwizard (DW) Guts =
  2. 2. Jetty • Webserver and javax.Servlet container • DW adds InstrumentedHandler • DW register a Servlet which which wraps JerseyContainer containing all your Resources & ResourceMethods • Wired in AbstractServerFactory
  3. 3. InstrumentedHandler • Jetty Handler • Handlers are chained together to process request, generating a response • Provides instrumentation around request types, response code, and inflight status
  4. 4. Jersey Resource & ResourceMethod
  5. 5. Questions? • What pre & post processing happens before calling your ResourceMethod? • How do you define that? • How are arguments obtained to called your method?
  6. 6. WebApplicationImpl Contains a set of resources, each referenced by an absolute URI template • Maintains a set of tuples <Path, Rule> • Path matches request, Rule is applied • Rule has a RequestDispatcher • Rule has an instance of Resource, or constructs it (depends on whether resource is added as singleton) • Rule delegates to RequestDispatcher passing resource and context
  7. 7. RequestDispatcher public interface RequestDispatcher { public void dispatch(Object resource, HttpContext context); } RequestDispatcher maybe chained together, “Decorating” functionality
  8. 8. How does DW chain dispatchers? • Always: OptionalRequestDispatcher (if response == optional.absent -> return 404, else return 200 + body) • @CacheControl: CacheControlledRequestDispatcher(add caching header) • Instrumentation • @Timed: TimedRequestDispatcher (time the rest of dispatch chain) • @Metered: MeteredRequestDispatcher (meter thruput) • @ExceptionMetered: ExceptionMeteredRequestDispatcher (meter exceptions) • Always: *Invoker (extract args from HttpContext, reflectlively call ResourceMethod)
  9. 9. RequestDispatcher • One root RequestDispatcher per ResourceMethod • ResourceMethod created once at initialization • Every request to a ResourceMethod is dispatched to the dispatcher, providing an instance of the resource & HttpContext
  10. 10. TimedRequestDispatcher: @Timed
  11. 11. RequestDispatcher Impls
  12. 12. Metric Registry • Singleton, per JVM • All metrics are created and registered here • Codahale.Metrics
  13. 13. @Timed, @Metered what do I get? • Registers and captures metrics for your annotated ResourceMethod • Metrics collected determined by annotation • Even without ResourceMethod annotations, you get JVM and Jetty metrics because of Dropwizard
  14. 14. @Timed instantaneous view
  15. 15. Metrics over time? • You need a time series DB (Graphite) • You need a graphing frontend (Graphite) • You need to ship your data to the DB, you have to configure a GraphiteReporter: background thread periodically sending metrics to graphite
  16. 16. Graphite Sample
  17. 17. Extra: Graphite, Metrics • Default, Reporter ships all metrics to Graphite • Metrics are per JVM, Graphite can aggregate • Custom metrics without annotations • Graphite has an API, so custom dashboards
  18. 18. What is a Provider? An interface used by a framework, where various impls can be plugged in at runtime to customize your experience It’s as Jersey as Taylor Ham
  19. 19. Providers • ResourceMethodDispatchProvider • InjectableProvider • InjectableValuesProvider • MessageBodyReader • MessageBodyWriter
  20. 20. ResourceMethodDispatchProvider & Adapter //Responsibility is creating a RequestDispatcher public interface ResourceMethodDispatchProvider { RequestDispatcher create(AbstractResourceMethod abstractResourceMethod); } //Responsibility is daisy chaining adapters public interface ResourceMethodDispatchAdapter { ResourceMethodDispatchProvider adapt(ResourceMethodDispatchProvider provider); }
  21. 21. ResourceMethodDispatchProvider
  22. 22. Default Jersey ResourceMethodDispatchProviders ResourceMethodDispatcherFactory, which is a ResourceMethodDispatchProvider • Set of default ResourceMethodDispatchProviders which are iterated in order, calling create • Provider can’t create RequestDispatcher, return null • Continue until non null RequestDispatcher returned: *Invoker //void return & params [0] = {com.sun.jersey.server.impl.model.method.dispatch.VoidVoidDispatchProvider@5061} //void return, params are HttpRequestContext.class, HttpResponseContext.class [1] = {com.sun.jersey.server.impl.model.method.dispatch.HttpReqResDispatchProvider@5062} //Consumes mimelist is multipart form-data [2] = {com.sun.jersey.server.impl.model.method.dispatch.MultipartFormDispatchProvider@5063} //Non GET, delegates to AbstractResourceMethodDispatchProvider [3] = {com.sun.jersey.server.impl.model.method.dispatch.FormDispatchProvider@5064} //Delegates to AbstractResourceMethodDispatchProvider [4] = {com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider@5065}
  23. 23. AbstractResourceMethodDispatchProvider Impls are required to create a InjectableValuesProvider from the ResourceMethod which will be used to generate the argument list from the HttpContext Args are built by the InjectableValuesProvider by using it’s List<AbstractHttpContextInjectable> to transform to to an array of objects, by extracting a Typed object from the HttpContext The a List<AbstractHttpContextInjectable> is generated by examining each Parameter of the ResourceMethod, for each parameter the list of InjectableProvider(s) in InjectableProviderFactory are examined to see if the InjectableProvider can produce an Injectable. The first one able to produce an Injectable is used
  24. 24. MessageBodyReader/Writer • JacksonMessageBodyProvider from Jackson • Enables using Jackson to parse request entities into objects and generate response entities from objects • Supports Jackson & JAXB annotations in that order • Wired in AbstractServerFactory