Drejc
Drejc

Reputation: 14286

vertx access event bus outside verticle class

I'm playing around with vert.x event bus, and everything is working fine in the simplest of examples.

However I would like to send messages to the vert.x event bus outside the Verticle class.

How do I access the event bus from outside the Verticle class?
Can Guice be utilised to provide the event bus?

For instance:

public class A {

   public void executeAndSendMessage() {

      ... some logic ...
      eventBus.send("address", "finished job");
  }
}

Now I can provide the event bus itself in the constructor of this class and keep a reference to it. But this seems a bit cumbersome:

private final EventBus eventBus;

public A(EventBus bus) {
   eventBus = bus;
}

Upvotes: 2

Views: 1130

Answers (1)

Drejc
Drejc

Reputation: 14286

Ok I have managed to use Guice injection and inject the event bus with a provider using: https://github.com/larrytin/vertx-mod-guice

public class TestModule implements VertxModule {

    ...

    @Provides
    public EventBus getEventBus() {
        return vertx.eventBus();
    }
}


public class A() {

    @Inject
    Provider<EventBus> eventBus;

    @GET
    @Path("/foo")
    public String foo() {

        eventBus.get().send("Test-Address", "HELLO");
        return "bar";
    }
}

Upvotes: 2

Related Questions