Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] Injecting http client #78

Closed
axelerod opened this issue Dec 14, 2015 · 7 comments
Closed

[question] Injecting http client #78

axelerod opened this issue Dec 14, 2015 · 7 comments

Comments

@axelerod
Copy link

Hi.
Can't find solution, how can I instantiate and inject HttpClient to objects in guice module. Is it possible?

@jhaber
Copy link
Member

jhaber commented Dec 14, 2015

I don't understand the question, you're trying to bind an HTTP client in Guice? Or access things from Guice when building the HTTP client? What type of HTTP client? Are you sure this is related to the dropwizard-guice library?

@axelerod
Copy link
Author

Hi, Jonathan.
My case is to create managed instance, like here:
http://www.dropwizard.io/0.9.1/docs/manual/client.html
and use it injecting in Guice objects. Type of client is not important.

On 14 December 2015 at 21:45, Jonathan Haber notifications@github.com
wrote:

I don't understand the question, you're trying to bind an HTTP client in
Guice? Or access things from Guice when building the HTTP client? What type
of HTTP client? Are you sure this is related to the dropwizard-guice
library?


Reply to this email directly or view it on GitHub
#78 (comment)
.

@jhaber
Copy link
Member

jhaber commented Dec 14, 2015

Ah I think I understand now. This is possible, but because the Guice injector is created during dropwizard's initialize phase and the Environment isn't available until the run phase, the binding can't be created eagerly otherwise it will fail.

Assuming you're running Guice in Stage.DEVELOPMENT and followed this documentation for adding a HttpClientConfiguration field to your dropwizard configuration object, you should be able to add something like this to your Guice module: (replacing ExampleConfiguration with your configuration class)

@Provides
@Singleton
public HttpClient providesHttpClient(ExampleConfiguration config, Environment environment) {
  return new HttpClientBuilder(environment).using(config.getHttpClientConfiguration()).build();
}

After which you should be able to inject HttpClient into other classes

@axelerod
Copy link
Author

Hi, thanks for quick responses!
Going to try this!

-----Исходное сообщение-----
От: "Jonathan Haber" notifications@github.com
Отправлено: ‎15.‎12.‎2015 0:52
Кому: "HubSpot/dropwizard-guice" dropwizard-guice@noreply.github.com
Копия: "Oleksii Burov" aleksei.burov@gmail.com
Тема: Re: [dropwizard-guice] [question] Injecting http client (#78)

Ah I think I understand now. This is possible, but because the Guice injector is created during dropwizard's initialize phase and the Environment isn't available until the run phase, the binding can't be created eagerly otherwise it will fail.
Assuming you're running Guice in Stage.DEVELOPMENT and followed this documentation for adding a HttpClientConfiguration field to your configuration object, you should be able to add something like this to your Guice module:
@provides
@singleton
public HttpClient providesHttpClient(ExampleConfiguration config, Environment environment) {
return new HttpClientBuilder(environment).using(config.getHttpClientConfiguration()).build()
}—
Reply to this email directly or view it on GitHub.

@mrwilby
Copy link

mrwilby commented May 30, 2016

Is there any better approach to this than running in "development" ? There seems to be a lot of posts about inability to use Environment during the bootstrap phase, but no clearly documented solution - just in-passing mentions of governator's LazySingleton or then reconfiguring to run in "development" stage, which smacks of a hack.

Did I miss some documentation that solves this issue 'properly'? TIA

@jhaber
Copy link
Member

jhaber commented Jun 2, 2016

@mrwilby with dropwizard-guice the binding needs to be created lazily (since the Injector is creating during the initialize phase and the environment isn't available until the run phase). AFAIK there is no way to do what you want 'properly', the only workarounds I know of are the ones you mention. However, you should check out dropwizard-guicier which creates the injector during the run phase (with Stage.PRODUCTION by default), ditches AutoConfig entirely, and is just generally better designed in my opinion. With dropwizard-guicier you could add a module like this:

public class ExampleModule extends DropwizardAwareModule<ExampleConfiguration> {

    @Override
    public void configure(Binder binder) {
        HttpClient httpClient = new HttpClientBuilder(getEnvironment())
            .using(getConfiguration().getHttpClientConfiguration())
            .build();

        binder.bind(HttpClient.class).toInstance(httpClient);
    }
}

@jhaber jhaber closed this as completed Jun 2, 2016
@mrwilby
Copy link

mrwilby commented Jun 2, 2016

Thanks @jhaber . I actually discovered guicer the other day from reading another of your posts, and made the switch already. Guicier is much better for my use cases! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants