Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between the send and publish methods of Vertx's EventBus?

I'm having my first contact with Vertx's EventBus and I realized there are two ways to submit a message. Used the send or publish method. I ask: What is the practical difference between using these two methods and in what scenario do they use each one?

like image 794
cazuzaneto Avatar asked Jun 22 '19 11:06

cazuzaneto


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.

What is event loop in Vertx?

The whole purpose of the event loop is to react to events which are delivered to the event loop by the operating system. Event loop processes those events by executing handlers. To explain how the event loop operates, let's imagine a typical HTTP server application serving multiple client connections at the same time.

What is Handler in Vertx?

This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. @FunctionalInterface public interface Handler<E> A generic event handler. This interface is used heavily throughout Vert. x as a handler for all types of asynchronous occurrences.

How do you make a verticle on a Vertx?

You can deploy a verticle using one of the deployVerticle method, specifying a verticle name or you can pass in a verticle instance you have already created yourself. Deploying Verticle instances is Java only. Verticle myVerticle = new MyVerticle(); vertx. deployVerticle(myVerticle);


1 Answers

Both send and publish are used to send a message to an event bus address. However there are some differences between the two.

By using publish:

  • A message is sent to one or multiple listeners
  • All handlers listening against the address will be notified
  • No answer is expected from handlers

By using send:

  • A message is sent to one and only one handler registered against the event bus address.
  • If multiple handlers are registered, only one will be notified. The receiver will be selected by a "round-robin algorithm" as per the docs.
  • The receiver can answer the message, this answer can be empty or contain a response body. A response timeout can also be specified.

In practical usage, publish is quite useful to inform that an event has occured, whereas send is quite handy for asking a treatment where the response matters.

Conceptually, publish uses the publish/subscribe pattern whereas send uses the request/response pattern.

like image 153
Murat Avatar answered Jan 04 '23 04:01

Murat