简体   繁体   English

VertX3 Verticle部署:未触发CompletionHandler

[英]VertX3 verticle deployment: CompletionHandler not triggered

When I run the following piece of code: 当我运行以下代码时:

import io.vertx.core.*;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class TestVerticle extends AbstractVerticle {
    final Logger logger = LoggerFactory.getLogger(TestVerticle.class.getName());

    public static final String ADDRESS = "oot.test";

    public void start(Future<Void> startFuture) {
        logger.info("starting test verticle");
        MessageConsumer<JsonObject> consumer = vertx.eventBus().consumer(ADDRESS);
        consumer.handler(message -> {
            final JsonObject body = message.body();
            logger.info("received: " + body);
            JsonObject replyMessage = body.copy();
            replyMessage.put("status", "processed");
            message.reply(replyMessage);
        });
        logger.info("started test verticle");
    }
}
public class Scratchpad {
    private final static Logger logger = LoggerFactory.getLogger(Scratchpad.class);

    public static void main(String[] args) throws InterruptedException {
        Vertx vertx = Vertx.vertx();
        logger.info("deploying test verticle");
        Handler<AsyncResult<String>> completionHandler = result -> {
            System.out.println("done");
            if (result.succeeded()) {
                logger.info("deployment result: " + result.result());
            } else {
                logger.error("failed to deploy: " + result);
            }
        };
        TestVerticle testVerticle = new TestVerticle();
        vertx.deployVerticle(testVerticle, completionHandler);

        logger.info("deployment completed");
    }
}

I expect the CompletionHandler content to be executed and therefore I should get something sent to stdout (at least "done", although the logging should be working as well) but nothing happens. 我希望执行CompletionHandler内容,因此我应该将某些内容发送到stdout(至少“已完成”,尽管日志记录也应该正常工作)但没有任何反应。 All the other logging information shows up correctly on my screen. 所有其他日志记录信息在我的屏幕上正确显示。 What am I doing wrong? 我究竟做错了什么?

After: 后:

logger.info("started test verticle");

Add: 加:

startFuture.complete();

See Asynchronous Verticle start and stop section of the docs about the asynchronous start method : 请参阅有关异步启动方法的文档的异步Verticle启动和停止部分:

This version of the method takes a Future as a parameter. 此版本的方法将Future作为参数。 When the method returns the verticle will not be considered deployed. 当方法返回时,不会将Verticle视为已部署。

Some time later, after you've done everything you need to do (eg start other verticles), you can call complete on the Future (or fail) to signal that you're done. 一段时间之后,在你完成了你需要做的所有事情之后(比如启动其他的Verticle),你可以在Future上调用完成(或者失败)来表示你已经完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM