vertx未来失败但被视为成功()

时间:2018-06-07 10:15:08

标签: java vert.x

我有一个方法:

    public void getVmsAdminToken(HttpClient httpClient, handler<AsyncResult<String>> handler) {
           httpClient.postAbs(url, h -> h.bodyHandler(bh -> {
                try {
                    switch (h.statusCode()) {
                        case 200:
                            JsonObject vmsResponse = bh.toJsonObject();                       
                            handler.handle(Future.succeededFuture(Json.encode(vmsResponse)));
                            break;
                        default:
                            LOG.error("VMS call failed {}", h.statusCode());
                            handler.handle(Future.failedFuture(500 + ""));
                            break;
                    }
                } catch (Throwable t) {
                    LOG.error("Exception in getVmsAdminToken", t);
                    handler.handle(Future.failedFuture(500 + ""));
                }
            }))
                    .setTimeout(timeOutMs)
                    .putHeader("content-type", "application/json")
                    .putHeader("stub", apiKey)
                    .end(vehicleReqBody.encode());
    }

我在以下方法调用中使用它:

private void getAdminToken(RoutingContext ctx, RedisFleetStorage storage, HttpClient httpClient) {

    getVmsAdminToken(fleetId, user, vehicle, httpClient, replyVms -> {
                                            if (reply.succeeded()) {
                                               // why succeeded?!!
                                            }
                                        });
}

即使getVmsToken失败,执行也会落入if (reply.succeeded())

为什么会这样?

1 个答案:

答案 0 :(得分:1)

您应该检查与 HTTP 调用结果相同的AsyncResult对象:

private void getAdminToken(RoutingContext ctx, RedisFleetStorage storage, HttpClient httpClient) {

    getVmsAdminToken(fleetId, user, vehicle, httpClient, replyVms -> {
                                            if (replyVms.succeeded()) {
                                               // do you thing
                                            }
                                        });
}