First you must build the application:
java -Djava.net.preferIPv4Stack=true -jar target/protobuf-eventbus-howto-1.0.0-SNAPSHOT-fat.jar io.vertx.howtos.protobuf.eventbus.ReceiverVerticle -cluster
When it is ready, you will see: INFO: Succeeded in deploying verticle
.
Now start the sender in another terminal:
java -Djava.net.preferIPv4Stack=true -jar target/protobuf-eventbus-howto-1.0.0-SNAPSHOT-fat.jar io.vertx.howtos.protobuf.eventbus.SenderVerticle -cluster
When it is ready, you will see: INFO: Succeeded in deploying verticle
.
After some time, you will see in the sender console:
Sending request = Jane Doe (1445840961)
Received reply = Hello Jane Doe (654163465)
And in the receiver console:
Received request = Jane Doe (449456520)
Sending reply = Hello Jane Doe (522259462)
In clustered mode, the system hash code that is printed is not important: objects living in distinct virtual machines are, obviously, different.
What about local mode?
To run the sender and the receiver in the same virtual machine, we can use a third verticle whose only purpose is to deploy them.
MainVerticle.java
package io.vertx.howtos.protobuf.eventbus;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
public class MainVerticle extends VerticleBase {
@Override
public Future<?> start() {
return Future.join(
vertx.deployVerticle(new ReceiverVerticle()),
vertx.deployVerticle(new SenderVerticle())
);
}
}
Open a terminal, build the project again and run the executable JAR.
./mvnw clean package
java -jar target/protobuf-eventbus-howto-1.0.0-SNAPSHOT-fat.jar
When it is ready, you will see: INFO: Succeeded in deploying verticle
.
After some time, you will see in the console:
Sending request = Jane Doe (346056258)
Received request = Jane Doe (346056258)
Sending reply = Hello Jane Doe (1483137857)
Received reply = Hello Jane Doe (1483137857)
Pay attention to the system hash code.
Notice that the request object is the same in both the sender and receiver.
This is also true about the reply object.