1

私は Vertx にはかなり慣れていませんが、Spring との統合をテストすることに非常に興味があります。Spring Boot を使用してプロジェクトを後押しし、2 つのバーティクルをデプロイしました。イベントバスを使って通信したいのですが、うまくいきません。これは私がしたことです:

  1. メインアプリケーションで:

    @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. 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. 最後に 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);
    

    }

しかし、起動して /page1 にアクセスすると、ServerVerticle から APIVerticle にメッセージをまったく送信できません。イベント バス コンシューマーを Sender と同じ垂直に移動すると、イベントを受信できます。

2 つのバーティクル間でメッセージを送信する際に何か問題がありますか? どうすればそれを機能させることができますか?

前もって感謝します。

4

2 に答える 2