There are many things to like about the
Dropwizard Framework, but if you’re like me,
you might want to “own your main” function. A normal Dropwizard
application gives you a run hook as part of its Application parent
class, but in the end your code is still subservient to the Dropwizard
framework code.
One pattern that is very good for organizing small logical services in
your application is to use
Guava’s Services
to break your application into service level groupings (And avoid the
drinking the microservice kool-aid prematurely). Guava services give
you a common operational interface to coordinate all your in-process
logical components. In this model, the Dropwizard web server is no
more important than my periodic polling service, or my separated RPC
service, and so on. I’d like my Dropwizard web stack to be a peer to
the other services that I’m running inside my application. It only
takes two steps to make this work.
Step 1: Create the Guava Service
Create a new class that extends AbstractIdleService. When we
implement the startUp method in the service, we will need to handle
the key bootstrap setup that normally is handled within the Dropwizard
framework when you invoke the server command.
Step 2: Reinitialize logging inside the Dropwizard Application
Start your Guava Service
Now you have a nicely contained Guava service that you can manage
alongside your other Guava service that aren’t necessarily Dropwizard
related. You can startAsync and stopAsync the service to start and
stop the web server, even while other services are still running.