Skip to content

Governator Integration

Vyacheslav Rusakov edited this page Mar 9, 2017 · 7 revisions

DEPRECATED Documentation was moved into new docuemntation site

Dropwizard-guicey supports custom Guice Injectors allowing integration with 3rd-party Guice libraries that leverage custom injectors to extend Guice. This guide demonstrates how to fully integrate Netflix Governator with dropwizard-guicey including Governator lifecycle management.

Dependencies

First, we need to include the Netflix Governator dependency within our project. For a Gradle project, a minimum dependency set may look something like this:

dependencies {
    compile "ru.vyarus:dropwizard-guicey:${dwGuiceyVersion}"
    compile "com.netflix.governator:governator:${governatorVersion}"
}

Declaring the ${dwGuiceyVersion} and ${governatorVersion} values are an exercise left for the reader.

Custom InjectorFactory

Our next step is to create a custom Injector Factory similar to the advice in the Governator Quick Start guide:

public class GovernatorInjectorFactory implements InjectorFactory {
    public Injector createInjector(final Stage stage, final Iterable<? extends Module> modules) {
        return LifecycleInjector.builder().withModules(modules).inStage(stage).build().createInjector();
    }
}

As an InjectorFactory, we can then configure the bundle to use the Governator injector:

@Override
void initialize(Bootstrap<TestConfiguration> bootstrap) {
    bootstrap.addBundle(GuiceBundle.<TestConfiguration> builder()
            .injectorFactory(new GovernatorInjectorFactory())
            .enableAutoConfig("package.to.scan")
            .modules(new MyModule())
            .build()
    );
}

Note that auto scan is enabled and managed bean, described below, will be discovered and installed automatically (assuming its inside scanned package).

Governator Lifecycle

Many Governator enhancements are only available when the Governator LifecycleManager is properly started and closed with the application. Dropwizard's managed objects make this a simple task.

ManagedGovernator Class

We first need a class to register as a managed object. Such a trivial class is not included with dropwizard-guicey to avoid unnecessary coupling to Governator:

import io.dropwizard.lifecycle.Managed;
import ru.vyarus.dropwizard.guice.GuiceBundle;
import com.netflix.governator.lifecycle.LifecycleManager;
import javax.inject.Inject;

public class ManagedGovernator implements Managed {

    @Inject
    private LifecycleManager manager;

    @Override
    public void start() throws Exception {
        manager.start();
    }

    @Override
    public void stop() throws Exception {
        manager.close();
    }
}

Now guicey will find managed, create governator injector (using custom factory), create managed instance and register in dropwizard. This will "bind" governator lifecycle to dropwizard lifecycle.

Note: if you need to run this managed before or after some other dropwizard managed use guicey @Order annotation.