We’re big fans of the Dropwizard framework. It’s straightforward approach to services allows us to create fast and highly available systems very easily. While we’ve used a number of different Java frameworks when building out our platform, Dropwizard is quickly becoming our de-facto standard for all new services.
As we built out our new market data service, we found a need to add a task. Dropwizard’s documentation is fantastic, encompassing everything from application structure to internals and semantics, but tasks aren’t covered particularly well. We’ve submitted a pull request to improve the docs, but opted to document our learnings here as well.
Task is defined as an abstract class that you must subclass. The main thing to point out is the
execute method which is called when the task is invoked. Let’s define a sample task to see how it works. For this example, we’ll create a task called
TruncateDatabaseTask. First, we’ll define the class:
Dropwizard requires that each task have a name. The name has a special purpose as it’s also used to compose the path that the task is accessible from within Dropwizard. The
Task defines a basic constructor that we can use, but let’s define it explicity via a constructor of our own (more on this later). We’ll also implement the
execute method even though we’re not doing anything yet.
At this point, we have a fully composed Dropwizard task. Now we just need to add it to our application:
Start your application, and you’ll see that the task is now registered alongside the garbage collection task that Dropwizard adds automatically
Let’s make this a more practical example. The value of defining a constructor is that we can dependency inject anything we need into the task from our Dropwizard application:
Most of our Dropwizard tasks look a lot like the one above. We found these tasks valuable as schedulable components of our application without building custom scripts to tie back to the current runtime. If you use Sinatra or Express to build service APIs definitely check out Dropwizard.