dropwizard internals

24
Dropwizard (DW) Guts =

Upload: carlo-rtr

Post on 27-Aug-2014

430 views

Category:

Software


0 download

DESCRIPTION

Dropwizard

TRANSCRIPT

  • Dropwizard (DW) Guts =
  • 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
  • InstrumentedHandler Jetty Handler Handlers are chained together to process request, generating a response Provides instrumentation around request types, response code, and inflight status
  • Jersey Resource & ResourceMethod
  • Questions? What pre & post processing happens before calling your ResourceMethod? How do you define that? How are arguments obtained to called your method?
  • WebApplicationImpl Contains a set of resources, each referenced by an absolute URI template Maintains a set of tuples 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
  • RequestDispatcher public interface RequestDispatcher { public void dispatch(Object resource, HttpContext context); } RequestDispatcher maybe chained together, Decorating functionality
  • 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)
  • 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
  • TimedRequestDispatcher: @Timed
  • RequestDispatcher Impls
  • Metric Registry Singleton, per JVM All metrics are created and registered here Codahale.Metrics
  • @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
  • @Timed instantaneous view
  • 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
  • Graphite Sample
  • 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
  • What is a Provider? An interface used by a framework, where various impls can be plugged in at runtime to customize your experience Its as Jersey as Taylor Ham
  • Providers ResourceMethodDispatchProvider InjectableProvider InjectableValuesProvider MessageBodyReader MessageBodyWriter
  • 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); }
  • ResourceMethodDispatchProvider
  • Default Jersey ResourceMethodDispatchProviders ResourceMethodDispatcherFactory, which is a ResourceMethodDispatchProvider Set of default ResourceMethodDispatchProviders which are iterated in order, calling create Provider cant 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}
  • 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 its List to transform to to an array of objects, by extracting a Typed object from the HttpContext The a List 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
  • 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