Search code examples
javajerseyjersey-2.0event-bushk2

How to enable HK2 TopicDistributionService in jersey 2.15?


I having a problem trying to enable Hk2 Events in jersey.

ResourceConfig:

import org.glassfish.hk2.utilities.TopicDistributionModule;

public class Application extends ResourceConfig {
    public Application() throws IOException {
        super(Application.class);
        register(TopicDistributionModule.class);
    }
}

Resource:

@Path("/")
public class MainResource {
    @Inject
    private Topic<MyClass> myClassTopic;

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String index() {
        return "index";
    }
}

And I get an error:

java.lang.IllegalStateException: There is no implementation of the TopicDistributionService to distribute the message
  at org.jvnet.hk2.internal.TopicImpl.publish(TopicImpl.java:79) ~[hk2-locator-2.3.0.jar:?]
  at ... MainResource.index(MainResource.java:21) ~[classes/:?]
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_20]

Solution

  • TopicDistributionModule is an AbstractBinder, which from my understanding should be registered as an instance, not by class. This

    register(new TopicDistributionModule());
    

    should get rid of the exception, as it register the default TopicDistributionService. From there it's just a matter of making sure you have a "live" subscriber when you publish the event/message