2017-01-09 29 views
1

我面臨一個問題,我不能將處理結果存儲到Json數組中。每當數組爲空時。我試圖用未來,但它仍然同樣的問題,這裏是我的代碼:如何將處理程序結果存儲到JsonArray vertx中?

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())); 
     } 
    }); 
} 
+0

您異步API'getCaraTypeDocBin'執行錯誤。你能證明嗎? – zella

+0

感謝@zella的回覆,我在問題 – OLH

回答

2

與期貨異步系統的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()); 
      } 
     } 
    ); 
} 

您正在使用期貨是錯誤的。您例如簡單,你沒有異步鏈(其中計算結果根據前面的結果等),所以不是期貨,你可以簡單的使用回調:

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()); 
     } 
    }); 
} 

UPD你需要從多個異步收集結果調用doc 。不知道如何用你的回調風格API來實現它。但隨着期貨這不是問題:

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()); 
       } 
      }); 
    } 
+0

謝謝@zella的解釋,我從數據庫中得到完美的結果,我想要的是創建一個包含結果並將其發送給客戶端的新JsonArray。 – OLH

+0

@HousseinO我是不注意的:)我upd答案 – zella

0

GET.getCaraTypeDocBin()異步運行,對不對?我認爲那裏有一些耗時的事情,比如打遠程API?因此,循環將以毫秒運行,啓動所有請求,然後「logger.debugs」將會發生,並且只有這樣,回調纔會開始執行。caraRefTypeDocBin.add(res.result());

如果我是對的,您應該看到之前記錄的結果空陣列。

+0

中加入了'getCaraTypeDocBin'的實現,謝謝@Edward對你的迴應,是的,它是異步運行的。我想將結果存儲到JsonArray中,以便將其發送到客戶端。 – OLH

相關問題