Skip to content

Governator integration

Include the Netflix Governator dependency:

compile "com.netflix.governator:governator:1.5.11"

Governator owns injector creation, so we need to create custom guicey InjectorFactory

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();
    }
}

Configure the new factory in the guice bundle:

@Override
public void initialize(Bootstrap<Configuration> bootstrap) {
    bootstrap.addBundle(GuiceBundle.builder()
            .injectorFactory(new GovernatorInjectorFactory())
            .enableAutoConfig("com.mycompany.myapp")
            ...
            .build()
    );
}

Note

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.

Use dropwizard's managed object to control governator lifecycle:

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

public class GovernatorLifecycle implements Managed {

    @Inject
    private LifecycleManager manager;

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

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

Guicey will find this managed bean, create governator injector (using a custom factory), create a managed bean instance and register it in dropwizard. This will "bind" the governator lifecycle to the dropwizard lifecycle.

Note

If you need to control the order which the managed beans are started, use the @Order annotation.