user3006967
user3006967

Reputation: 3545

Vertx event bus can not send message to different verticle

I am pretty new to Vertx, but I am pretty interested in testing its integration with Spring. I used Spring boot to boost the project, and deployed two verticles. I want them to communicate with each other using event bus, but failed. This is what I did:

  1. In Main application:

    @SpringBootApplication public class MySpringVertxApplication { @Autowired MyRestAPIServer myRestAPIServer; @Autowired MyRestAPIVerticle MyRestAPIVerticle;

    public static void main(String[] args) {
    SpringApplication.run(MySpringVertxApplication.class, args);
    }
    
    @PostConstruct
    public void deployVerticles(){
    System.out.println("deploying...");
    
    Vertx.vertx().deployVerticle(MyRestAPIVerticle);
    Vertx.vertx().deployVerticle(myRestAPIServer);
    }
    

    }

  2. In APIVerticle:

    @Component public class MyRestAPIVerticle extends AbstractVerticle {

    public static final String ALL_ACCOUNT_LISTING = "com.example.ALL_ACCOUNT_LISTING";
    
    @Autowired
    AccountService accountService;
    
    EventBus eventBus;
    
    @Override
    public void start() throws Exception {
    super.start();
    
    eventBus = vertx.eventBus();
    MessageConsumer<String> consumer = eventBus.consumer(MyRestAPIVerticle.ALL_ACCOUNT_LISTING);
    consumer.handler(message -> {
        System.out.println("I have received a message: " + message.body());
        message.reply("Pretty Good");
      });
    consumer.completionHandler(res -> {
        if (res.succeeded()) {
          System.out.println("The handler registration has reached all nodes");
        } else {
          System.out.println("Registration failed!");
        }
      });
    }
    

    }

  3. Finally ServerVerticle:

    @Service public class MyRestAPIServer extends AbstractVerticle {

    HttpServer server;
    HttpServerResponse response;
    
    EventBus eventBus;
    @Override
    public void start() throws Exception {
    
    server = vertx.createHttpServer();
    Router router = Router.router(vertx);
    
    eventBus = vertx.eventBus();
    
    router.route("/page1").handler(rc -> {
        response = rc.response();
        response.setChunked(true);
    
        eventBus.send(MyRestAPIVerticle.ALL_ACCOUNT_LISTING, 
            "Yay! Someone kicked a ball",
            ar->{
            if(ar.succeeded()){
                System.out.println("Response is :"+ar.result().body());
            }
            }
            );
    
    });
    
    server.requestHandler(router::accept).listen(9999);
    

    }

But after I started it, and visit to /page1, message can not be sent from ServerVerticle to APIVerticle at all. If I move event bus consumer into same verticle as Sender, then event can be received.

Are there anything wrong here in sending message between two verticles? How can I make it work?

Thanks in advance.

Upvotes: 1

Views: 1766

Answers (2)

cy3er
cy3er

Reputation: 1699

You deployed them in separate vertx instance:

Vertx.vertx().deployVerticle(MyRestAPIVerticle);
Vertx.vertx().deployVerticle(myRestAPIServer);

Try this:

Vertx vertx = Vertx.vertx();
vertx.deployVerticle(MyRestAPIVerticle);
vertx.deployVerticle(myRestAPIServer);

Upvotes: 1

Hegdekar
Hegdekar

Reputation: 1167

Vertx event bus is not shared across different Vertx instances like how you are trying ( But clustered Vert.x applications can do it). In your case change it to use a single Vert.x instance like below in your MySpringVertxApplication.

Vertx vertx = Vertx.vertx();
vertx.deployVerticle(MyRestAPIVerticle.class.getName());
vertx.deployVerticle(MyRestAPIServer.class.getName());

Upvotes: 0

Related Questions