简体   繁体   中英

Java8 CompletableFuture conditional chaining

I have read many java8 completable future tutorials, most of them are basically same. All talking about some basic method "thenAccept"/"thenApply"/thenCombine" to build a pipeline flow.

But when come to a real work problem, I feel hard to organize different completable futures from different Service. For Example:

    interface Cache{
       CompletableFuture<Bean> getAsync(long id);
       CompletableFuture<Boolean> saveAsync(Bean bean);
    }


   interface DB{
       Completable<Bean> getAsync(long id)
    }

the service logic is quite simple, get data from Cache, if exist return to our client, if not, get it from DB, if exist save it back to Cache, and return it to our client, if neither exist in DB, return "error" to client.

using synchronize API, it will be quite straight ahead. But when using asyncnorized API, there are "many pipelines", manny conditional break. I can not figure out how to implement this using CompletableFuture API.

If you don't care about the result of saving into the cache and if you want to throw an exception on bean not found, then it can be eg

CompletableFuture<Bean> findBeanAsync(long id, Cache cache, DB db) {
    return cache.getAsync(id).thenCompose(bean -> {
        if (bean != null) {
            return CompletableFuture.completedFuture(bean);
        }
        return db.getAsync(id).thenApply(dbBean -> {
            if (dbBean == null) {
                throw new RuntimeException("bean not found with id " + id);
            }
            cache.saveAsync(dbBean);
            return dbBean;
        });
    });
}

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