2016-03-06 180 views
0

我對Vertx很新,但我對測試與Spring的集成感興趣。我用Spring引導來提升這個項目,並部署了兩個Verticle。我希望他們使用事件總線相互通信,但是失敗了。這是我做過什麼:Vertx事件總線不能發送消息到不同的Verticle

  1. 在主要應用:

    @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); 
    } 
    

    }

  2. 在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!"); 
        } 
        }); 
    } 
    

    }

  3. 最後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之間有什麼不對嗎?我怎樣才能使它工作?

在此先感謝。

回答

0

您在不同的vertx實例進行部署:

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

試一下:

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