Question

I was wondering how the Dropwizard client module should be implemented.

Source of confusion:

Dropwizard recommends you to separate your project as such:

In general, we recommend you separate your projects into three Maven modules: project-api, project-client, and project-service.

On the Client section, it shows that you can instantiate the httpClient provided by dropWizard within a run method.

@Override
public void run(ExampleConfiguration config,
                Environment environment) {
    final Client client = new JerseyClientBuilder().using(config.getJerseyClientConfiguration())
                                                   .using(environment)
                                                   .build();
    environment.addResource(new ExternalServiceResource(client));
}

I thought that the client module would wrap the httpClient, and any other service can use the client module, without caring which httpClient it is using.

So

  1. how would a client module look like
  2. When would you instantiate an httpClient directly within a service's run method (as done in the snippet of code above)

Thanks!

Was it helpful?

Solution

How would a client module look like

This is heavily dependant on your project scope and structure. For example, in one of my projects which is heavily database dependant, the Client module (or Service class in DropWizard's terminology) contains my DAO instantiations as well as hibernate initializations and a bunch of other init stuff (SQS, etc). I also use the HTTP Client and the Service class is where I initialize it. Reason being is that the Service class is the entry point and this is where you end up instantiating your Resource classes, etc. So having the dependancies instantiated here allow me to pass them into my resources as constructor params. If you were using something like Guice, then the way to go would be different since you have access to injection, etc.

When would you instantiate an httpClient directly within a service's run method (as done in the snippet of code above)

The HttpClient shown in the doc and your question is used when your project requires a Http Client. For example, lets say your DW project or one of the resources you are writing requires you to make a HTTP call to a twitter API. This is where the Http Client comes into play. You can actually use any Http Client library you want, however using the ones provided by DW (Apache Http Client, Jersey Http Client) allows you to create a 'Managed' Http Client there by allowing DW to start up, shut down and clean up the HTTP Client when the service is shutdown. So things like thread pools, connection pools, etc are all cleaned up by DW when you use its managed HTTP Client. In addition, the reason why you create this HTTP Client inside the run method is because you are then able to get a reference to the Configuration object's instance which will allow you to control the HTTP Client's settings via DW's configuration system.

Hope this answers your questions

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top