Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vertX eventBus consumer listens to all addresses

Tags:

vert.x

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());
});
like image 406
rupweb Avatar asked Sep 22 '15 10:09

rupweb


People also ask

How does Vertx Eventbus work?

A Vert. x event-bus is a light-weight distributed messaging system which allows different parts of your application, or different applications and services to communicate with each in a loosely coupled way. An event-bus supports publish-subscribe messaging, point-to-point messaging and request-response messaging.

How does Vertx loop work?

Event loop theads and worker threads The API call vertx. executeBlocking() registered two handlers. Vert. x will call the blockingCodeHandler() using a worker thread and the resultHandler() using an event loop thread.

What is the use of Vertx?

As the headline on the Vert. x website (vertx.io) says, “Eclipse Vert. x is a toolkit for building reactive applications on the JVM.” It is event-driven, single-threaded, and non-blocking, which means you can handle many concurrent apps with a small number of threads.

Does Vertx use Netty?

Vert. x uses low level IO library Netty. The application framework includes these features: Polyglot.


2 Answers

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

like image 103
ph0enix Avatar answered Oct 20 '22 04:10

ph0enix


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.

like image 38
Will Avatar answered Oct 20 '22 06:10

Will