gpt4 book ai didi

java - 如何将处理程序结果存储到 JsonArray vertx 中?

转载 作者:行者123 更新时间:2023-11-30 02:41:30 24 4
gpt4 key购买 nike

我遇到一个问题,我无法将处理程序结果存储到 Json 数组中。每次数组为空时。我尝试使用 Future 但它仍然是同样的问题,这是我的代码:

 static void getCaraReferTypeDocBin(RoutingContext routingContext){
String ids = routingContext.request().getParam("ids");
logger.debug(ids);
String[] idsArray = ids.split(",");
JsonArray caraRefTypeDocBin = new JsonArray();
for (int i = 0; i <idsArray.length ; i++) {
GET.getCaraTypeDocBin(Integer.parseInt(idsArray[i]), res->{
if (res.succeeded()){
logger.debug(res.result());
caraRefTypeDocBin.add(res.result());
}else{
logger.debug(res.cause().getMessage());
}
});

}
logger.debug(caraRefTypeDocBin);
}

这是getCaraReferTypeDocBin实现:

 public static void getCaraTypeDocBin(int id ,Handler<AsyncResult<JsonArray>> resultHandler) {
JsonArray pIn = new JsonArray();
pIn.add(new JsonObject().put("pos", 2).put("type", OracleTypes.NUMBER).put("val", id));
JsonArray pOut = new JsonArray().add(new JsonObject().put("pos", 1).put("type", OracleTypes.CURSOR));
DB.cursor(SQL.LIST_CARA_TYPE_DOC_BIN,pIn,pOut, res -> {
if (res.succeeded()) {
try {
resultHandler.handle(Future.succeededFuture(res.result().getJsonArray("1")));
}catch (Exception e){
logger.error(e);
resultHandler.handle(Future.failedFuture(Error.ERROR_OCCURED.toString()));
}
} else {
resultHandler.handle(Future.failedFuture(res.cause().getMessage()));
}
});
}

最佳答案

在异步系统中,带有 futures 的 api 应该这样写:

private Future<String> loadFromDb() {
Future<String> f = Future.future();
//some internal loading from db
String result = "fromDb";
//when loading completes, pass it to future result
f.complete(result);
return f;
}

以及它如何使用:

private void handleSo(RoutingContext routingContext) {

loadFromDb()
.map(new Function<String, JsonArray>() {
@Override
public JsonArray apply(String fromDb) {
//map to json
return new JsonArray(...);
}
})
.setHandler(
new Handler<AsyncResult<JsonArray>>() {
@Override
public void handle(AsyncResult<JsonArray> result) {
routingContext.response().end(result.result().toString());
}
}
);
}

您错误地使用了 future。您的示例很简单,并且没有异步链(其中结果根据先前的结果等进行计算),因此您可以简单地使用回调来代替 futures:

private void loadFromDb(Handler<String> handler) {
String result = "fromDb";
handler.handle(result);
}

private void handleSo(RoutingContext routingContext) {
loadFromDb(new Handler<String>() {
@Override
public void handle(String fromDb) {
routingContext.response().end(new JsonArray(...).toString());
}
});
}

更新您需要从多个异步调用收集结果doc 。不知道如何用回调风格的 api 来实现它。但对于 future 来说,这不是问题:

private void handleSo(RoutingContext routingContext) {

List<Future> futures = new ArrayList<>();
for (int i = 0; i < 10; i++) {
//make 10 async calls
futures.add(loadFromDb()
.map(new Function<String, JsonObject>() {
@Override
public JsonObject apply(String fromDb) {
return new JsonObject().put("data", fromDb);
}
}));
}
CompositeFuture.all(futures)
.map(new Function<CompositeFuture, JsonArray>() {
@Override
public JsonArray apply(CompositeFuture compositeFuture) {
JsonArray array = new JsonArray();
List<JsonObject> jsons = compositeFuture.result().list();
jsons.forEach(jsonObject -> array.add(jsonObject));
return array;

}
})
.setHandler(new Handler<AsyncResult<JsonArray>>() {
@Override
public void handle(AsyncResult<JsonArray> res) {
routingContext.response().end(res.result().toString());
}
});
}

关于java - 如何将处理程序结果存储到 JsonArray vertx 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41539441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com