HK2 Dependency Injection – Using Iterable provider

HK2 is a light weight, dynamic dependency injection framework. If you don’t want to rely on spring framework for managing the dependencies then you can use this.

In this post, I am going to explain how to use the Iterable provider with an example.

Assume that you have a Jersey REST application and it has an endpoint which is responsible for sending out some details to different systems such as Kafka, Database. There is a class called Emitter which is responsible for emitting the data. There are two classes KafkaProducer which produce the data to Kafka and another one DatabaseProducer which writes the data into DB and both these classes implement the MessageProducer interface.

I am not going to give the exact code to be used for doing this. My main motive is to show how we can use Iterable provider here.

MessageProducer.java


public interface MessageProducer {
    public void produce(String message);
}

DBProducer.java
DBProducer class sends out the message to DB


public class DBProducer implements MessageProducer {

    @Override
    public void produce(String message) {
        //Write code to send out the message to DB
        System.out.println("Producing the message to DB");
    }
}

KafkaProducer.java
KafkaProducer class sends out the message to Kafka



public class KafkaProducer implements MessageProducer {

    @Override
    public void produce(String message) {
        //Write code to send out the message to kafka
        System.out.println("Producing the message to Kafka");
    }
}

DependencyBinder.java
DependencyBinder class to be used for binding. Hence you need to pass this class as an init parameter to the Jersey servlet(Parameter name: javax.ws.rs.Application)


import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;

import javax.inject.Singleton;

public class DependencyBinder extends ResourceConfig {

    public DependencyBinder() {
       //the resource class is available in com/resource folder
        packages(true, "com.resource");
        register(new AbstractBinder() {
            @Override
            protected void configure() {
                bind(Emitter.class).to(Emitter.class).in(Singleton.class);
                bind(KafkaProducer.class).to(MessageProducer.class).in(Singleton.class);
                bind(DBProducer.class).to(MessageProducer.class).in(Singleton.class);
            }
        });
    }

 

Finally the Emitter class. This class has to send the message to different systems. So we can’t inject the each class here. We can use IterableProvider to achieve this. So the HK2 framework gets all the classes which implement the interface MessageProducer and injects into this class. In this example, it will return DBProducer and KafkaProducer instances.


import org.glassfish.hk2.api.IterableProvider;

import javax.inject.Inject;

public class Emitter {

    @Inject
    private IterableProvider messageProducers;

    public void emitMessage() {

        // Write code to prepare the message
        String message = "Its a new message";
        for (MessageProducer messageProducer : messageProducers) {
            messageProducer.produce(message);
        }
    }
} 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s