Black_Rider February 2016

how to write own async hander in java vertx

I want to write a function take some arguments and an Handler<E> object. After doing some computation based on other arguments, I need to return result on handler object.

To explain it better, I am giving an example:

    public void getPlaces(LatLong latLong, int redius, Collection<PlaceType> places)
{
    if(redius <= 0 || redius > 50000)
    {
        throw new IllegalAccessError("redis can be 0 to 50000 meters only");
    }
    String uri = String.format(GOOGLE_PLACE_API_URI, latLong.getLatitude(), latLong.getLongitude(), redius, PROJECT_API_KEY, Joiner.on("|").join(places));


    HttpClientRequest request = httpClient.getAbs(uri, response ->{
        logger.info("Received response with status code={} message={}", response.statusCode(), response.statusMessage());
        response.bodyHandler( buffer -> {
            JsonObject result = buffer.toJsonObject();
            String nextPageToken = result.getString("next_page_token");
            JsonArray resultArray = result.getJsonArray("results");
            System.out.println("nextPageToken="+nextPageToken+" result="+resultArray);
        });

    }).setTimeout(30000);

    request.exceptionHandler(e -> {
        logger.error("Caught exception while making request for google place APIs. Error message={} e=", e.getMessage(), e);
    });
    request.end();
}

    public void getPlaces(LatLong latLong, int redius, Collection<PlaceType> places, Handler<JsonObject> response)
{
    // COMPLETE THIS USING ABOVE FUNCTION
    getPlaces(latLong, redius, places);
}

Answers


cdelmas February 2016

I'm not sure to understand your need, but I guess you want to call the handler with the result of your computation. So this code should do what you want:

public void getPlaces(LatLong latLong, int redius, Collection<PlaceType> places, Handler<JsonObject> handler) {

    if(redius <= 0 || redius > 50000) {
        throw new IllegalAccessError("redis can be 0 to 50000 meters only");
    }

    String uri = String.format(GOOGLE_PLACE_API_URI, latLong.getLatitude(), latLong.getLongitude(), redius, PROJECT_API_KEY, Joiner.on("|").join(places));

    HttpClientRequest request = httpClient.getAbs(uri, response ->{
        logger.info("Received response with status code={} message={}", response.statusCode(), response.statusMessage());
        response.bodyHandler( buffer -> {
            JsonObject result = buffer.toJsonObject();
            String nextPageToken = result.getString("next_page_token");
            JsonArray resultArray = result.getJsonArray("results");
            System.out.println("nextPageToken="+nextPageToken+" result="+resultArray);
            handler.handle(result); // call the result handler
        });

    }).setTimeout(30000);

    request.exceptionHandler(e -> {
        logger.error("Caught exception while making request for google place APIs. Error message={} e=", e.getMessage(), e);
    });
    request.end();
}

Hope this will help.

Post Status

Asked in February 2016
Viewed 2,460 times
Voted 12
Answered 1 times

Search




Leave an answer