简体   繁体   中英

return something as soon as a response to an async request is received using Elasticsearch's Java High Level Rest Client

I'm using Elasticsearch's Java High Level Rest Client, I want the createProfileDocument method to return something as soon as I get a response to the Async request (as if the return statement was inside the onResponse method), I have made this work around (code below) but I believe there is a better way to do it, which i didn't find in documentations. Here's my code:

private IndexResponse response = null;

public String createProfileDocument(ProfileDocument document) throws Exception {
    UUID uuid = UUID.randomUUID();
    document.setId(uuid.toString());
    IndexRequest indexRequest = new IndexRequest("profiles", "doc", document.getId())
            .source(convertProfileDocumentToMap(document));
    ActionListener<IndexResponse> listener;
    listener = new ActionListener<IndexResponse>() {
        @Override
        public void onResponse(IndexResponse indexResponse) {
            response = indexResponse;
            //I want it to behave as if the return statement was here
        }

        @Override
        public void onFailure(Exception e) {
            e.printStackTrace();
        }
    };
    client.indexAsync(indexRequest, RequestOptions.DEFAULT, listener);
    //waiting for response, shouldn't be done this way
    while (response == null) {

    }
    IndexResponse responseClone = response;
    response = null;
    return responseClone.getResult().name().toString();
}

Two options: Switch to the synchronous version of the same call

IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);

Or if you want to keep using the async version. You can use PlainActionFuture in package org.elasticsearch.action.support;

PlainActionFuture<IndexResponse> future = new PlainActionFuture<>();
client.indexAsync(indexRequest, RequestOptions.DEFAULT, future);
IndexResponse response = future.actionGet();

I prefer the async request, especially for the long running job (eg reindex). And the async way handle the failure more gracefully. Here's an example:

    PlainActionFuture<BulkByScrollResponse> future = new PlainActionFuture<>();
    restHighLevelRestClientHelper.reindex(INDEX_NAME, NEW_INDEX_NAME, future);
    try {
        // onResponse
        if (future.isDone()) {
            // Set 1 hour as the limit, if time out throw exception.
            BulkByScrollResponse response = future.actionGet(TimeValue.timeValueMinutes(60));
            System.out.println("The request took " + response.getTook());
        } else {
            // onFailure
            throw new Exception("Something wrong with the ES query.");
        }
    } catch(Exception e) {
        throw new Exception("The request timed out.");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM