Vertx Eventbus not working in Java

北慕城南 提交于 2019-12-01 08:20:35

问题


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

回答1:


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.




回答2:


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()));


来源:https://stackoverflow.com/questions/30983863/vertx-eventbus-not-working-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!