Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between CompletableFuture's thenApply and handle?

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces what is the difference between thenApply and handle of CompletableFuture. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Background

1. In the process of transforming mybatis-r2dbc, the result should be returned as CompletableFutrue. 2. When querying individual field information and not wrapping it as pojo, you cannot return a null object, because the sink.next method does not support null objects. But there is a null return in the actual scenario. 3. In order to avoid that sink.next cannot return null, the null object is wrapped as a custom enumeration class. Mybatis-r2dbc receives the enumerated object for further processing, converting the enumerated object back to null. The processing is done in the CompletableFuture.handleAsync method.

problem

Using handleAsync to deal with the problem of returning null does solve the problem that sink.next cannot return null objects. But it also introduces another problem, that is, when Exception occurs in the underlying driver, the returned object is also null. After using handleAsync processing, the exception will be swallowed up and cannot be thrown out. Even after using handleAsync, all the driven exceptions will be swallowed up and the returned data will be null, and the upper application will not be able to perceive the cause of the error.

Solve

Replace the handleAsync method with thenApplyAsync.

Compare thenApplyAsync and handleAsync

Compare the difference between thenApplyAsync and handleAsync: thenApplyAsync is executed after the current stage is completed normally. If an exception is thrown, the exception does not enter. All methods that start with then are the same. HandleAsync enters regardless of whether an exception is thrown or not, and the returned result cannot continue to throw an exception. Using this method will engulf the previous phase of the exception.

On the CompletableFuture thenApply and handle what is the difference to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report