Vertx中的异步性,列表在设置之前返回空

时间:2017-06-19 08:13:34

标签: java asynchronous vert.x

在我的MainVerticle中,我设置了路由器

router.get("/persons").handler(ctx -> apiService.getPersons(ctx, client));

我想两次调用同一个主机,首先我需要获取一个人员ID列表,然后调用同一主机的另一个方法来获取人员详细信息。 。所以,在这段代码中,我正在另一个中调用。由于异步性,personList为空,因为Vertx不等待第二次调用的回复,第二次调用的回复稍后出现,但当然personList为空。在我的测试用例中,迭代器有两个元素。我应该如何实现它以确保列表返回设置良好?

        public void getPersons(RoutingContext routingContext, WebClient client){
    logger.info("getpersons");
    routingContext.response().headers().add("content-type", "application/json");
    String wshost= ConfigUtils.getStringConfig(vertx,"personws.url");
    String url = wshost+"/persons";
    client.getAbs(url).send(response -> {
        try {
            if (response.succeeded() && response.result().statusCode()==200) {
                List<Person> personList = new ArrayList<Person>();
                logger.info("Server content " + response.result().bodyAsString());

                Iterator<Object> iterator = response.result().bodyAsJsonArray().iterator();
                while(iterator.hasNext()){
                    Object obj = iterator.next();
                    JsonObject jsonPerson = (JsonObject) obj ;
                    logger.debug("personId: {}", jsonPerson.getString("id"));
                    Person person = new Person();
                    person.setId(jsonPerson.getString("id"));

                    String url2 = wshost+"/persons/"+jsonPerson.getString("id");
                    logger.info("Calling ws to get person details for : {}", jsonPerson.getString("id"), url2);
                     client.getAbs(url2).send(response2 -> {
                        if (response2.succeeded() && response2.result().statusCode()==200) {

                            JsonObject jsonDetails = response2.result().bodyAsJsonObject();
                            person.setDetail(jsonDetails);
                            personList.add(person);
                            logger.info("person is {} ", person);
                            logger.info("iterator.hasNext():{}", iterator.hasNext());


                        }
                        else{
                            routingContext.response().setStatusCode(500);
                            routingContext.response().end(buildError("Failed to get json ", 600).encode());
                        }

                    });

                };
                logger.info("personList: {} ", personList);
                logger.info("personList size {} ", personList.size());
                routingContext.response().setStatusCode(200).end(new JsonArray(personList).toString());
            } else {
                logger.error("Cannot get persons");
                routingContext.response().setStatusCode(500);
                routingContext.response().end(buildError("Failed to get persons", 100).encode());
            }
        } catch (Exception e) {
            e.printStackTrace();
            routingContext.response().setStatusCode(500);
            routingContext.response().end(buildError("Exception in API- Failed to get get persons for fleet "+routingContext.request().headers().get("fleet"), 300).encode());
        }

    });

}

1 个答案:

答案 0 :(得分:0)

首先,你不要只召唤你的主人两次。您在初始请求中按照每个人的数量将其称为n + 1次。

实现所需目标的最简单方法是计算您希望得到的人数:

                int expected = response.result().bodyAsJsonArray().size();

                Iterator<Object> iterator = response.result().bodyAsJsonArray().iterator();
                while(iterator.hasNext()){
                    Object obj = iterator.next();
                    JsonObject jsonPerson = (JsonObject) obj ;
                    logger.debug("personId: {}", jsonPerson.getString("id"));
                    Person person = new Person();
                    person.setId(jsonPerson.getString("id"));

                    String url2 = wshost+"/persons/"+jsonPerson.getString("id");
                    logger.info("Calling ws to get person details for : {}", jsonPerson.getString("id"), url2);
                    client.getAbs(url2).send(response2 -> {
                        if (response2.succeeded() && response2.result().statusCode()==200) {

                            JsonObject jsonDetails = response2.result().bodyAsJsonObject();
                            person.setDetail(jsonDetails);
                            personList.add(person);
                            logger.info("person is {} ", person);
                            logger.info("iterator.hasNext():{}", iterator.hasNext());

                            // That's the last one
                            if (personList.size() == expected) {
                                // Only now complete the request
                                routingContext.response().setStatusCode(200).end(new JsonArray(personList).toString());
                            }
                        }
                        else{
                            routingContext.response().setStatusCode(500);
                            routingContext.response().end(buildError("Failed to get json ", 600).encode());
                        }

                    });

                };
                logger.info("personList: {} ", personList);
                logger.info("personList size {} ", personList.size());
               // Moved inside callback

其他方式是使用CompositeFuture: http://vertx.io/docs/vertx-core/groovy/#_concurrent_composition