7

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());
});
Dale K
  • 25,246
  • 15
  • 42
  • 71
rupweb
  • 3,052
  • 1
  • 30
  • 57
  • You might want to post what it is you want to achieve - is this for some logger, audit, metric or similar like handler? – Will Sep 28 '15 at 10:56
  • yes to all the above and for handlers that work using different products like ui.apples.braeburn, ui.apples.cox, ui.oranges.valencia and then you can either listen to different apples or oranges, or indeed just ui.* – rupweb Sep 28 '15 at 11:09

3 Answers3

5

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).

kuza
  • 2,761
  • 3
  • 22
  • 56
ph0enix
  • 763
  • 2
  • 8
  • 23
1

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);
   });
paul
  • 12,873
  • 23
  • 91
  • 153
1

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.

Will
  • 6,561
  • 3
  • 30
  • 41
  • thanks for comment Will, "As far as I can see, Vert.x doesn't store consumers of send and publish separately" lol that could be kind of (un) fun ;) – rupweb Sep 28 '15 at 11:50
  • I am using eventBus.publish() not send() to get around your guess (which was correct) – rupweb Sep 28 '15 at 11:51