rupweb
rupweb

Reputation: 3328

vertX eventBus consumer listens to all addresses

I'd like to write a catch all eventBus consumer. Is this possible?

eB = vertx.eventBus();

MessageConsumer<JsonObject> consumer = eB.consumer("*"); // What is catch all address ???

consumer.handler(message -> {
    Log.info("Received: " + message.body().toString());
});

Upvotes: 7

Views: 5295

Answers (3)

ph0enix
ph0enix

Reputation: 774

A solution to your problem might be an interceptor.

vertx.eventBus().addInterceptor( message -> {
          System.out.println("LOG: " + message.message().body().toString());
});

This handler will write every message that comes to the event-bus in vertx.

Reference is here: http://vertx.io/docs/apidocs/io/vertx/rxjava/core/eventbus/EventBus.html#addInterceptor-io.vertx.core.Handler-

Also, version of vertx-core that I'm using is 3.3.2, I think interceptor functionality is not available in older versions (e.g. 3.0.0).

Upvotes: 5

paul
paul

Reputation: 13481

I dont know if that´s possible but referring to the documentation, you can put a listener to the events to know when a publish, send, open_socket, close_socket is invoked

   sockJSHandler.bridge(options, be -> {
     if (be.type() == BridgeEvent.Type.PUBLISH || be.type() == BridgeEvent.Type.RECEIVE) {
       Log.info("Received: " + message.body().toString());
     }
     be.complete(true);
   });

Upvotes: 1

Will
Will

Reputation: 6731

Having looked through the Java code, I don't think this is possible.

Vert.x stores event bus consumers in a MultiMap looking like:

AsyncMultiMap<String, ServerID>

where the String key is the consumer address.

And as you'd guess, Vert.x just does a map.get(address) to find out the relevant consumers.

Update after OP comment

While I think your use case is valid, I think you're going to have to roll something yourself.

As far as I can see, Vert.x doesn't store consumers of send and publish separately. It's all in one MultiMap. So it would be inadvisable to try to register consumers for all events.

If someone does an eventBus.send(), and Vert.x selects your auditing consumer, it will be the only consumer receiving the event, and I'm going to guess that's not what you want.

Upvotes: 1

Related Questions