我是Vertx的新手,但我对测试它与Spring的集成非常感兴趣。我使用Spring启动来增强项目,并部署了两个Verticle。我希望他们使用事件总线相互通信,但失败了。这就是我做的:
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);
}
}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!");
}
});
}
}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);
}但是在我启动它并访问/ page1之后,消息无法从ServerVerticle发送到APIVerticle。如果我将事件总线使用者移动到与发件人相同的Verticle中,则可以接收事件。
这两个Verticle之间发送消息有什么不对吗?我怎样才能使它工作?
提前致谢。
Vertx事件总线不会像您尝试的那样在不同的Vertx实例之间共享(但是群集Vert.x应用程序可以执行此操作)。在您的情况下,将其更改为在MySpringVertxApplication
中使用如下所示的单个Vert.x实例。
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(MyRestAPIVerticle.class.getName());
vertx.deployVerticle(MyRestAPIServer.class.getName());
您将它们部署在单独的vertx实例中:
Vertx.vertx().deployVerticle(MyRestAPIVerticle);
Vertx.vertx().deployVerticle(myRestAPIServer);
试试这个:
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(MyRestAPIVerticle);
vertx.deployVerticle(myRestAPIServer);