How to register multiple handlers for a Verticle?

511 views
Skip to first unread message

boro...@gmail.com

unread,
Oct 27, 2016, 12:21:38 AM10/27/16
to vert.x
Hello,

I'm trying to figure out how to register multiple handlers for a single Verticle address? 
According to the documentation:

Publish / subscribe messaging
The event bus supports publishing messages.
Messages are published to an address. Publishing means delivering the message to all handlers that are registered at that address.

I tried the following, but when I call publish from another Verticle the last handler (3) is always executed. 

public void start() {
   
MessageConsumer<String> consumer = vertx.eventBus().consumer("MYADDRESS");
   
    consumer
.handler(message -> System.out.println("(1) received a message"));
    consumer
.handler(message -> System.out.println("(2) received a message"));
    consumer
.handler(message -> System.out.println("(3) received a message"));
}

I understand that each handler I set below is overriding the previous one, but I just can't find and example online of adding multiple handlers. I'm sure I'm doing something fundamentally wrong, just need an example please.

Thanks,

p.s. I'm running vert.x 3.3.3

Mumuney Abdlquadri

unread,
Oct 27, 2016, 1:21:51 AM10/27/16
to ve...@googlegroups.com
I believe if your create different consumer objects per handler that should suffice.

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/a483ed8f-072f-4c97-be51-0eb7bd7ca3be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

boro...@gmail.com

unread,
Oct 27, 2016, 1:41:13 AM10/27/16
to vert.x
I knew it would be something simple :) 
I tried and it worked. Would be useful to have a small example in the docs for others though. 

Thanks a lot Mumuney.


On Wednesday, October 26, 2016 at 10:21:51 PM UTC-7, Mumuney Abdlquadri wrote:
I believe if your create different consumer objects per handler that should suffice.
On Thu, Oct 27, 2016 at 5:21 AM, <boro...@gmail.com> wrote:
Hello,

I'm trying to figure out how to register multiple handlers for a single Verticle address? 
According to the documentation:

Publish / subscribe messaging
The event bus supports publishing messages.
Messages are published to an address. Publishing means delivering the message to all handlers that are registered at that address.

I tried the following, but when I call publish from another Verticle the last handler (3) is always executed. 

public void start() {
   
MessageConsumer<String> consumer = vertx.eventBus().consumer("MYADDRESS");
   
    consumer
.handler(message -> System.out.println("(1) received a message"));
    consumer
.handler(message -> System.out.println("(2) received a message"));
    consumer
.handler(message -> System.out.println("(3) received a message"));
}

I understand that each handler I set below is overriding the previous one, but I just can't find and example online of adding multiple handlers. I'm sure I'm doing something fundamentally wrong, just need an example please.

Thanks,

p.s. I'm running vert.x 3.3.3

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.

Mumuney Abdlquadri

unread,
Oct 27, 2016, 2:22:15 AM10/27/16
to ve...@googlegroups.com
you are welcome.

you might have a point. so I made pull request to the examples repo.

To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages