为什么我无法从第一个请求vert.x获得结果?

时间:2021-02-14 18:03:52
public class Ussd {

    private static Logger logger = LogManager.getLogger(Ussd.class);
    private  static  String query;

    public static void ussdMessages(RoutingContext routingContext){
        String codeService = routingContext.getBodyAsJson().getString("codeService");
        query=getServiceQuery(codeService);
        System.out.println("Query : "+query);
    }

    public  static String  getServiceQuery(String codeService){
        JsonObject params = new JsonObject();
        params.put("QUERY", Queries.DB_SELECT_SERVICE_QUERY);
        params.put("PARAMS", new JsonArray().add(codeService));
        System.out.println(params);
        DB.select(params, res -> {
            if (res.succeeded()) {
               query = res.result().getJsonArray("results").getJsonArray(0).getString(0);
                System.out.println(query);
            } else {
                query = res.cause().getMessage();
            }
        });
        return query;
    }
}

The method getServiceQuery returns my exact result and I stock it into static query but after recall inside method ussdMesages, it returns null from the first request using Postman. For the second request, it returns the correct result. I don't know why.

getServiceQuery方法返回我的确切结果,并将其保存到静态查询中,但在方法ussdMesages中调用后,它使用Postman从第一个请求返回null。对于第二个请求,它返回正确的结果。我不知道为什么。

1 个解决方案

#1


1  

Your getServiceQuery method does async work(DB.select), so in your case you are returning the query before it getting processed. Change the return type of the function from String to Future.

您的getServiceQuery方法执行异步工作(DB.select),因此在您的情况下,您将在处理之前返回查询。将函数的返回类型从String更改为Future。

public class Ussd {

    private static Logger logger = LogManager.getLogger(Ussd.class);
    private  static  String query;

    public static void ussdMessages(RoutingContext routingContext){
        String codeService = routingContext.getBodyAsJson().getString("codeService");
        getServiceQuery(codeService).setHandler(r -> {
                System.out.println("query : "+r.result());
            });
    }

    public static Future<String> getServiceQuery(String codeService){
        Future<String> future = Future.future();
        JsonObject params = new JsonObject();
        params.put("QUERY", Queries.DB_SELECT_SERVICE_QUERY);
        params.put("PARAMS", new JsonArray().add(codeService));
        System.out.println(params);
        DB.select(params, res -> {
            if (res.succeeded()) {
               query = res.result().getJsonArray("results").getJsonArray(0).getString(0);
                System.out.println(query);
                future.complete(query);
            } else {
                future.fail(res.cause().getMessage());
            }
        });
        return future;
    }
}

#1


1  

Your getServiceQuery method does async work(DB.select), so in your case you are returning the query before it getting processed. Change the return type of the function from String to Future.

您的getServiceQuery方法执行异步工作(DB.select),因此在您的情况下,您将在处理之前返回查询。将函数的返回类型从String更改为Future。

public class Ussd {

    private static Logger logger = LogManager.getLogger(Ussd.class);
    private  static  String query;

    public static void ussdMessages(RoutingContext routingContext){
        String codeService = routingContext.getBodyAsJson().getString("codeService");
        getServiceQuery(codeService).setHandler(r -> {
                System.out.println("query : "+r.result());
            });
    }

    public static Future<String> getServiceQuery(String codeService){
        Future<String> future = Future.future();
        JsonObject params = new JsonObject();
        params.put("QUERY", Queries.DB_SELECT_SERVICE_QUERY);
        params.put("PARAMS", new JsonArray().add(codeService));
        System.out.println(params);
        DB.select(params, res -> {
            if (res.succeeded()) {
               query = res.result().getJsonArray("results").getJsonArray(0).getString(0);
                System.out.println(query);
                future.complete(query);
            } else {
                future.fail(res.cause().getMessage());
            }
        });
        return future;
    }
}