I am currently maintaining application written in Java with Vertx framework. I would like to implement sending messages between 2 application instances (primary and secondary) using EventBus (over the network). Is it possible? In the Vertx documentation I do not see the example how I can achieve that. https://vertx.io/docs/vertx-core/java/#event_bus I see that there are send(...) methods in EventBus with address - but address can be any String. I would like to publish the events to another application instance (for example from Primary to Secondary).
Asked
Active
Viewed 62 times
1 Answers
1
It is possible using a Vert.x cluster manager.
Choose one of the supported cluster managers in the classpath of your application.
In your main method, instead of creating a standalone Vertx
instance, create a clustered one:
Vertx.clusteredVertx(new VertxOptions(), res -> {
if (res.succeeded()) {
Vertx vertx = res.result();
} else {
// failed!
}
});
Deploy a receiver:
public class Receiver extends AbstractVerticle {
@Override
public void start() throws Exception {
EventBus eb = vertx.eventBus();
eb.consumer("ping-address", message -> {
System.out.println("Received message: " + message.body());
// Now send back reply
message.reply("pong!");
});
System.out.println("Receiver ready!");
}
}
In a separate JVM, deploy a sender:
public class Sender extends AbstractVerticle {
@Override
public void start() throws Exception {
EventBus eb = vertx.eventBus();
// Send a message every second
vertx.setPeriodic(1000, v -> {
eb.request("ping-address", "ping!", reply -> {
if (reply.succeeded()) {
System.out.println("Received reply " + reply.result().body());
} else {
System.out.println("No reply");
}
});
});
}
}
That's it for the basics. You may need to follow individual cluster manager configuration instructions in the docs.

tsegismont
- 8,591
- 1
- 17
- 27
-
Where exactly will sender send the message? I see that it is sending message to "ping-address" but somehow it has to go through the network to the receiver on the given host + port. Is maybe sender broadcasting this message over the network? – Zygmuntix Feb 07 '23 at 15:52
-
1Choosing a consumer for the address is the job of the cluster manager. In point to point messaging it picks all registered consumers in a round-robin fashion. This is explained in the docs. – tsegismont Feb 07 '23 at 15:56