2017-01-09 14 views
1

私は問題に直面しています。ハンドラの結果をJson Arrayに格納することはできません。私は将来を使用しようとしましたが、それはまだ同じ問題が、ここに私のコードは次のとおりです。非同期、右ハンドラの結果をJsonArray vertxに格納する方法は?

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

あなたはasync api 'getCaraTypeDocBin'が正しく実装されていません。それを見せてもらえますか? – zella

+0

ご回答いただきありがとうございます。@ zellaさんのお返事ありがとうございます。質問の中に 'getCaraTypeDocBin'の実装を追加しました。 – OLH

答えて

2

はこのような何かを記述する必要があります。あなたの例シンプルで先物の代わりに、あなたは簡単な使用コールバックできるように、(結果は前の結果などに基づいて計算)非同期チェーンを持っていない:

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私は慎重だった:)私は答えを返す – zella

0

GET.getCaraTypeDocBin()実行:

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実装ですか?そして、リモートAPIを打つような、時間のかかるものがあると思いますか?したがって、ループはミリ秒単位で実行され、すべての要求が実行され、「logger.debugs」が実行され、コールバックの処理が開始されます。caraRefTypeDocBin.add(res.result());

私が正しいとすれば、空の配列

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

あなたは間違った先物を使用している先物と非同期システムAPIの

+0

お返事ありがとうございます。はい、それは非同期で実行されます。私は結果をクライアント側に送るためにJsonArrayに結果を保存します。 – OLH

関連する問題