有什么办法可以避免在vertx中编写异步代码时重复代码?

时间:2018-12-26 20:02:21

标签: java vert.x

我正在编写类似这样的函数,但我有几个。如果我们看到它们中的响应处理程序都是相同的代码行。

private void get(JsonObject request, Message<JsonObject> message) {

    webClient.get(webClientPort,
            webClientHost, "/document/" + request.getString("key"))
            .putHeader(HttpHeaders.CONTENT_TYPE.toString(), "application/json")
            .send(res -> {
                if (res.succeeded()) {
                    message.reply(res.result().bodyAsString());
                } else {
                    message.fail(0, Json.encode(new JsonObject().put("error", res.cause())));
                }
            });

}


private void add(JsonObject request, Message<JsonObject> message) {

    webClient.post(webClientPort,
            webClientHost, "/document/" + request.getString("key"))
            .putHeader(HttpHeaders.CONTENT_TYPE.toString(), "application/json")
            .sendJson(request.getJsonObject("document").encodePrettily(), res -> {
                if (res.succeeded()) {
                    message.reply(res.result().bodyAsString());
                } else {
                    message.fail(0, Json.encode(new JsonObject().put("error", res.cause())));
                }
            });

}

是否有避免这种重复代码并将其移到公共位置的方法?

res -> {
                if (res.succeeded()) {
                    message.reply(res.result().bodyAsString());
                } else {
                    message.fail(0, Json.encode(new JsonObject().put("error", res.cause())));
                }
            }

2 个答案:

答案 0 :(得分:2)

难道不能只是封装该逻辑另一个函数并通过方法引用对其进行调用?

private void processResponse(HttpServerResponse res, Message<JsonObject> message) {
    if (res.succeeded()) {
        message.reply(res.result().bodyAsString());
    } else {
        message.fail(0, Json.encode(new JsonObject().put("error", res.cause())));
    }
}

然后,您可以在代码的两个地方都使用方法引用来调用它:

.send(res -> this.processResponse(res, message));

.sendJson(request.getJsonObject("document").encodePrettily(), res -> this.processResponse(res, message));

答案 1 :(得分:2)

我能够做到这一点,并且奏效了。感谢@dovmo的帮助。您的意见帮助我达成了解决方案。

private void get(String key, Message<JsonObject> message) {
    webClient.get(webClientPort,
            webClientHost, "/document/" + key)
            .putHeader(HttpHeaders.CONTENT_TYPE.toString(), "application/json")
            .send(processResponse(message));
}

private void delete(String key, JsonObject body, Message<JsonObject> message) {
    webClient.delete(webClientPort,
            webClientHost, "/document/" + key)
            .putHeader(HttpHeaders.CONTENT_TYPE.toString(), "application/json")
            .send(processResponse(message));
}

private Handler<AsyncResult<HttpResponse<Buffer>>> processResponse( Message<JsonObject> message) {

    Handler<AsyncResult<HttpResponse<Buffer>>> handler = reply -> {
        if (reply.succeeded()) {
            message.reply(reply.result().bodyAsString());
        } else {
            message.fail(0, Json.encode(new JsonObject().put("error", reply.cause())));
        }
    };

    return handler;
}