Reputation: 29537
I am curious about an elusive - but potentially very powerful - DropWizard feature called Bundles. According to the docs:
A Dropwizard bundle is a reusable group of functionality, used to define blocks of an application’s behavior.
Given that DropWizard (DW) is extremely well documented, I’m shocked that this is really the only explanation on bundles. I see a few examples of them in the wild:
But what I don’t understand is: bundles seem to just be code packaged and distributed in JARs. So why can’t I just write “raw” (non-“bundle”-compliant) Java classes to do what I need, slap them in a JAR, then include that JAR on my build/compile classpath, ad then use them in my DW app? Of what use is a DW bundle, and when should one use them?
Upvotes: 26
Views: 5935
Reputation: 173
DW bundles are similar to "raw" JAR in the manner that both can be used to provide reusable functionality. However, the difference lies in the additional support that DW bundles enjoy. Bundles can be made configurable i.e. they can use the DW configuration file for configuration parameters
Upvotes: 1
Reputation: 11
Let say you work for your Platform Team in your company, and your developers use Kafka as message-bus.
Then, you can ask the dev team to add the below configuration in their DW app, and you can write the bundle to parse the broker-uris, and build the Producer Instance. With Integration with any Dependency Injection frameworks, all your have to do is @InJect the Producer Instance when you need.
kafka-broker-uris: <your kafka broker urls>
We can do the same thing using a library, and call a method that gets an broker-uri and return a producer object..
But, I believe, the main benefit comes from the Bundle knowing how to parse the configuration, and pre-builds necessary objects during the Initialize phase of DW lifecycle. Also, if you want to set a standard in your company on how the config file has to be defined, then it is good to go with a bundle.
Upvotes: 0
Reputation: 402
Bundles are like addons to Dropwizard that make it very easy to add small pieces of functionality. For example, if you use the assets bundle, you can attach a UI to your API for testing purposes and it will run on the same port and is very easy to use. Another example would be the Migrations Bundle that easily ties Liquibase into Dropwizard so you can run database migrations with the same jar. This also works well since your API could be accessing some sql database which has connection parameters defined in a yml file, the migrations would be able to run on the same database.
Upvotes: 8