HK2 Integration for Dropwizard

Adds support for Health Checks, LifeCycles, Metrics, and Tasks to be injected by HK2

License

License

Categories

Categories

DropWizard Container Microservices HK2 Application Layer Libs Dependency Injection
GroupId

GroupId

co.wrisk.dropwizard
ArtifactId

ArtifactId

dropwizard-hk2
Last Version

Last Version

0.0.6
Release Date

Release Date

Type

Type

jar
Description

Description

HK2 Integration for Dropwizard
Adds support for Health Checks, LifeCycles, Metrics, and Tasks to be injected by HK2
Project URL

Project URL

https://github.com/WriskHQ/dropwizard-hk2
Source Code Management

Source Code Management

https://github.com/WriskHQ/dropwizard-hk2

Download dropwizard-hk2

How to add to project

<!-- https://jarcasting.com/artifacts/co.wrisk.dropwizard/dropwizard-hk2/ -->
<dependency>
    <groupId>co.wrisk.dropwizard</groupId>
    <artifactId>dropwizard-hk2</artifactId>
    <version>0.0.6</version>
</dependency>
// https://jarcasting.com/artifacts/co.wrisk.dropwizard/dropwizard-hk2/
implementation 'co.wrisk.dropwizard:dropwizard-hk2:0.0.6'
// https://jarcasting.com/artifacts/co.wrisk.dropwizard/dropwizard-hk2/
implementation ("co.wrisk.dropwizard:dropwizard-hk2:0.0.6")
'co.wrisk.dropwizard:dropwizard-hk2:jar:0.0.6'
<dependency org="co.wrisk.dropwizard" name="dropwizard-hk2" rev="0.0.6">
  <artifact name="dropwizard-hk2" type="jar" />
</dependency>
@Grapes(
@Grab(group='co.wrisk.dropwizard', module='dropwizard-hk2', version='0.0.6')
)
libraryDependencies += "co.wrisk.dropwizard" % "dropwizard-hk2" % "0.0.6"
[co.wrisk.dropwizard/dropwizard-hk2 "0.0.6"]

Dependencies

runtime (1)

Group / Artifact Type Version
io.dropwizard : dropwizard-core jar 2.0.0-rc12

test (3)

Group / Artifact Type Version
junit : junit jar 4.12
io.dropwizard : dropwizard-testing jar 2.0.0-rc12
io.dropwizard : dropwizard-client jar 2.0.0-rc12

Project Modules

There are no modules declared in this project.

Dropwizard HK2 Integration

This bundle binds and integrates Dropwizard's HealthCheck, Metric, MetricSet, LifeCycle, LifeCycle.Listener, and Managed objects with HK2, allowing these components to be registered directly with Jersey and fully injected with any other services also known to HK2.

To use this bundle, add it to your application in the initialize method:

@Override
public void initialize(Bootstrap<YourConfig> bootstrap) {
    bootstrap.addBundle(new HK2Bundle<>());
}

Then, update your health checks, metrics, and managed components to use the custom Jersey component marker interfaces: InjectableHealthCheck, InjectableManaged, InjectableLifeCycle, InjectableLifeCycleListener, InjectableMetric, or InjectableMetricSet. These are necessary for Jersey to identify the components as a valid Jersey component, and just extend the Dropwizard interfaces of similar name. Here is an example health check:

@Named("TestHealthCheck")
public class TestInjectableHealthCheck extends InjectableHealthCheck {
    private final TestConfig config;

    @Inject
    public TestInjectableHealthCheck(TestConfig config) {
        this.config = config;
    }

    public TestConfig getConfig() {
        return config;
    }

    @Override
    protected Result check() throws Exception {
        return Result.healthy();
    }
}

Health checks and Metrics can also make use of the @Named() annotation to identify the name under which the component should be registered. If a name is not provided, a warning will be printed and a name generated in the format of ClassName.RandomUUID under which the component will be registered.

Lastly, register your components directly with Jersey:

@Override
public void run(TestConfig testConfig, Environment environment) throws Exception {
    environment.jersey().register(YourInjectableHealthCheck.class);
    environment.jersey().register(YourInjectableLifeCycle.class);
}

They will now be injected and registered properly with Dropwizard after HK2 and Jersey are initialized.

co.wrisk.dropwizard

Wrisk

Versions

Version
0.0.6
0.0.5