简体   繁体   English

运行线程时的异常处理

[英]Exception handling while running threads

I have a an application which has a kafka consumer and updates Elastic search according to the data it receives.我有一个应用程序,它有一个 kafka 消费者,并根据它收到的数据更新 Elastic 搜索。

My issue is that when ever ES goes down, the kafka consumer stops completely and doesn't restart.我的问题是,当 ES 出现故障时,kafka 消费者会完全停止并且不会重新启动。

I believe its due to how my ES code is running:我相信这是由于我的 ES 代码是如何运行的:

public CompletionStage<SearchResponse> executeSearch(SearchRequest searchRequest) {
        CompletableFuture<SearchResponse> f = new CompletableFuture<>();

        client.searchAsync(searchRequest, RequestOptions.DEFAULT, new ActionListener<SearchResponse>() {
            @Override
            public void onResponse(SearchResponse searchResponse) {
                f.complete(searchResponse);
            }

            @Override
            public void onFailure(Exception e) {

                throw new Exception();  // I am guessing because of this
            }
        });
        return f;
} 

If I change my onFailure method to:如果我将onFailure方法更改为:

            public void onFailure(Exception e) {

                f.complete(null);
            }

It works perfectly but I dont understand why throwing an exception leads to this.它工作得很好,但我不明白为什么抛出异常会导致这种情况。

Any help would be greatly appreciated.任何帮助将不胜感激。

For those that need a solution, I changed my code to below for it to work with exception:对于那些需要解决方案的人,我将我的代码更改为以下,以便它可以处理异常:

public void onFailure(Exception e) {
    f.completeExceptionally(new Exception());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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