Vertx事件总线不能发送消息到不同的Verticle
问题描述:
我对Vertx很新,但我对测试与Spring的集成感兴趣。我用Spring引导来提升这个项目,并部署了两个Verticle。我希望他们使用事件总线相互通信,但是失败了。这是我做过什么:Vertx事件总线不能发送消息到不同的Verticle
-
在主要应用:
@SpringBootApplication 公共类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); }
}
-
在APIVerticle:
@Component 公共类MyRestAPIVerticle延伸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!"); } }); }
}
-
最后ServerVerticle:
@Service 公共类MyRestAPIServer扩展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);
}
但后,我开始它,并访问/第1页,消息无法从ServerVerticle在发送到APIVerticle所有。如果我将事件总线消费者移动到与发件人相同的垂直方向上,则可以接收事件。
发送消息在两个Verticle之间有什么不对吗?我怎样才能使它工作?
在此先感谢。
答
您在不同的vertx实例进行部署:
Vertx.vertx().deployVerticle(MyRestAPIVerticle);
Vertx.vertx().deployVerticle(myRestAPIServer);
试一下:
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(MyRestAPIVerticle);
vertx.deployVerticle(myRestAPIServer);