成功部署Verticle但我收到此错误:结果已完成:成功

时间:2016-11-11 17:28:55

标签: java vert.x

我成功部署了垂直但我仍然遇到了这个错误:结果已经完成:成功。我不明白为什么,我需要一些解释,这是我的部署类:

public class Deploy {
private static Logger logger = LogManager.getLogger(Deploy.class);

private static void deployAsynchronousVerticalByIndex(Vertx vertx, int indexCurrentDeploy, JsonArray verticalArray, Future<Void> startFuture, JsonObject jsonObjectConfig) {
    JsonObject currentVertical = verticalArray.getJsonObject(indexCurrentDeploy);
    currentVertical.forEach(entry -> {
        logger.debug("Starting deploy of class: " + entry.getKey() + ", With the config: " + entry.getValue() + ".");

        DeploymentOptions optionsDeploy = new DeploymentOptions().setConfig(jsonObjectConfig);
        ObservableFuture<String> observable = RxHelper.observableFuture();
        vertx.deployVerticle(entry.getKey(), optionsDeploy, observable.toHandler());

        observable.subscribe(id -> {
            logger.info("Class " + entry.getKey() + " deployed.");
            if (indexCurrentDeploy + 1 < verticalArray.size()) {
                deployAsynchronousVerticalByIndex(vertx, indexCurrentDeploy + 1, verticalArray, startFuture, jsonObjectConfig);
            } else {
                logger.info("ALL classes are deployed.");
                startFuture.complete();
            }
        }, err -> {
            logger.error(err, err);
            startFuture.fail(err.getMessage());
        });
    });
}

public static void deployAsynchronousVertical(Vertx vertx, JsonArray verticalArray, Future<Void> startFuture, JsonObject jsonObjectConfig) {
    deployAsynchronousVerticalByIndex(vertx, 0, verticalArray, startFuture, jsonObjectConfig);
}

}

2 个答案:

答案 0 :(得分:3)

那是因为你在Verticle之间重用你的未来,并且在那里有竞争条件。

解决这个问题的最简单方法是:

if (startFuture.isComplete()) {
   startFuture.complete();    
}

但这实际上只会掩盖这个问题。

将你的observable从循环中提取出来,所以它实际上只会为每个顶点听一次。

    JsonObject currentVertical = verticalArray.getJsonObject(indexCurrentDeploy);
    ObservableFuture<String> observable = RxHelper.observableFuture();
    currentVertical.forEach(entry -> {
    logger.debug("Starting deploy of class: " + entry.getKey() + ", With the config: " + entry.getValue() + ".");

        DeploymentOptions optionsDeploy = new DeploymentOptions().setConfig(jsonObjectConfig);

        vertx.deployVerticle(entry.getKey(), optionsDeploy, observable.toHandler());
    });

    observable.subscribe(id -> {
        logger.info("Class " + id + " deployed.");
        if (indexCurrentDeploy + 1 < verticalArray.size()) {
            deployAsynchronousVerticalByIndex(vertx, indexCurrentDeploy + 1, verticalArray, startFuture, jsonObjectConfig);
        } else {
            logger.info("ALL classes are deployed.");

            if (startFuture.isComplete()) {
                startFuture.complete();
            }
        }
    }, err -> {
        logger.error(err, err);
        startFuture.fail(err.getMessage());
    });

答案 1 :(得分:0)

,除了从循环中提取您的可观察对象之外, 最好使用io.vertx.core.Promise而不是io.vertx.core.Future。 并使用 promise.try XXXX()

根据io.vertx.core.Promise.java文档:

@Future已弃用。而是使用Promise。

tryFail就像是失败,但是当诺言已经存在时返回false 完成而不是引发IllegalStateException。它返回 否则为真。

所以... 代替

List<TextPosition>

使用

if (startFuture.isComplete()) 
    startFuture.complete();
if (startFuture.isFail()) 
    startFuture.fail(err.getMessage());