Using Guice

Open Bidder supports Guice for interceptors to inject dependencies bound by Open Bidder and for users to bind their own dependencies.

Injecting classes bound by Open Bidder

Open Bidder uses a Guice injector to instantiate user interceptors. This is the same injector used for internal dependencies, so each class that is bound by Open Bidder can be injected into your interceptors. To inject these classes, annotate your interceptor's constructor with Inject, including the dependencies you would like to inject as parameters. For example, suppose your interceptor needs to call the Google Cloud Storage API; this requires authentication, but Open Bidder already does that for you—all you need to do is inject the authentication factory and use it from your code:

public class InterceptorWithInjection implements BidInterceptor {
  private final OAuth2CredentialFactory credentialFactory;

  public InterceptorWithInjection(OAuth2CredentialFactory credentialFactory) {
    this.credentialFactory = credentialFactory;

  public void execute(InterceptorChain<BidRequest, BidResponse> chain) {
    // Implementation... can use the credentialFactory

Classes are bound in Open Bidder with Module. The injector itself is instantiated in BidderServer, so that can also be used to determine which classes can be injected into an interceptor.

User-defined bindings

When writing even moderately complex interceptors, using Guice to manage your dependencies can be helpful for code modularity and testing. You can define your own Guice modules by extending Module. To actually use these custom modules, you need to create a custom bidder server by extending BidderServer and overriding the getModules() method. An example of this can be seen in Create the Bidder Server Project. When the bidder server starts, the Modules provided in the getModules() method will be added to the Guice injector.

Using Guice to add Request Receivers

The bidder server handles requests to specific endpoints via classes that implement the RequestReceiver interface, configured via Guice. You can use this to implement dynamic webpages, e.g. for some custom administration UI, or even to implement your own connector for some exchange. Notice that Open Bidder is highly optimized for scalable, real-time request processing, so we don't support traditional Java web APIs such as Servlets, JSP, JSF etc. You can implement any HTTP processing, but this requires use of Open Bidder's own HTTP APIs.

For a concrete example, consider that you want to create a bidder with a context /sample/ping that responds to requests with the string "pong". All APIs you need are already included with the dependency open-bidder-api, so start by implementing the request receiver.

public class PingRequestReceiver implements HttpReceiver {
  @Override public void receive(HttpReceiverContext ctx) {

Then, create a Guice module that binds this receiver:

public class PingModule implements Module {
  public void configure(Binder binder) {
    Multibinder.newSetBinder(binder, HttpRoute.class).addBinding().toInstance(
        HttpRoute.get("ping", "/sample/ping", new PingRequestReceiver(), Feature.ADMIN));

Finally, we create a bidder server project and extend BidderServer to add our custom module:

public class ExampleServer extends BidderServer {
  public ExampleServer(String[] args) {

  public static void main(String[] args) {
    new ExampleServer(args).main();

  protected ImmutableList<Module> getModules() {
    return ImmutableList.<Module>builder()
        .add(new PingModule())

If you were to run this locally, visiting http://localhost:8080/sample/ping would cause "pong" to print to your browser. Adding custom request receivers to bidders is a powerful way of adding functionality without needing to modify the Open Bidder source directly.

Enviar comentarios sobre…