Vertx Eventbus not working in Java

5.4k Views Asked by At

OS Linux JDK 1.7.0_67 Vert.x 2.1.5

Just getting started for wiring some of the vertx examples found on github.

Due to the manual want to try out a simple Sender <-> Receiver example, over EventBus. Many hours later, no ping, neither a hello world.

Anyone has an idea, what I'm missing on this simple code:

Sender.java

import org.vertx.java.platform.Verticle;

public class Sender extends Verticle {

  public void start(){
    vertx.eventBus().send("ping-address", "Hello");
  }
}

Receiver.java

import org.vertx.java.core.Handler;
import org.vertx.java.core.eventbus.EventBus;
import org.vertx.java.core.eventbus.Message;
import org.vertx.java.platform.Verticle;

public class Receiver extends Verticle {

  public void start() {
    EventBus eb = vertx.eventBus();
    eb.registerHandler("ping-address", new Handler<Message<String>>(){
        @Override
        public void handle(Message<String> message) {
            container.logger().info("Message: "+message.body());
        }
    });

    container.logger().info("PingVerticle started");

  }
}

Running both in seperate JVM instances on the same machine, with following CLI command:

vertx run Receiver.java
> Succeeded in deploying verticle

another console:

vertx run Sender.java
> Succeeded in deploying verticle
2

There are 2 best solutions below

3
On BEST ANSWER

The way you're running the vertices, they won't be able to see each other. They're essentially being started as completely separate processes with clustering disabled (the default). You have to run the vertices in cluster mode in order to get them to locate and communicate with one another. Use the -cluster option to enable Hazelcast clustering. You may also want to specify -host or -port.

If you continue seeing problems with even bus communication after that, enable Hazelcast logging and ensure the two nodes see each other. You may have to alter cluster.xml (the Hazelcast configuration) if your network does not support multicast.

0
On

Might be helpful for those who is using programmatic way of clustering. Please check you have the following values set in your vertx options:

VertxOptions options = new VertxOptions().setClustered(true)
                .setClusterHost("198.12.12.12")
                .setHAEnabled(true)
                .setHAGroup("dev");

 Vertx.clusteredVertx(options, res -> System.out.println(res.succeeded()));