Let's now assemble and deploy a small reactive system using the verticles created in the previous section:
package com.packt.learnjava.ch16_microservices;
import io.vertx.rxjava.core.RxHelper;
import io.vertx.rxjava.core.Vertx;
public class ReactiveSystemDemo {
public static void main(String... args) {
String address = "One";
Vertx vertx = Vertx.vertx();
RxHelper.deployVerticle(vertx, new MessageRcvVert("1", address));
RxHelper.deployVerticle(vertx, new MessageRcvVert("2", address));
RxHelper.deployVerticle(vertx, new MessageRcvVert("3", "Two"));
RxHelper.deployVerticle(vertx, new HttpServerVert(8082));
}
}
As you can see, we are going to deploy two verticles that use the same One address to receive messages and one verticle that uses the Two address. If we run the preceding program, the screen will display the following messages:
Let's now start sending HTTP requests to our system. First, let's send the same HTTP GET request three times:
As we have mentioned already, if there are several verticles registered with the same address, the rxSend() method uses a round-robin algorithm to select the verticle that should receive the next message. The first request went to the receiver with ID="1", the second request went to the receiver with ID="2", and the third request went to the receiver with ID="1" again.
We get the same results using the HTTP POST request for the /some/path/send path:
Again, the receiver of the message is rotated using the round-robin algorithm.
Now, let's publish a message to our system twice:
Since the receiver's reply cannot propagate back to the system user, we need to take a look at the messages that are logged on the backend:
As you can see, the publish() method sends the message to all verticles that are registered to the specified address. And note that the verticle with ID="3" (registered with the Two address) never received a message.
Before we wrap up this reactive system demonstration, it is worth mentioning that Vert.x allows you to easily cluster verticles. You can read about this feature in the Vert.x documentation (https://vertx.io/docs/vertx-core/java).