Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 657
junfei wang Avatar asked Mar 15 '17 04:03

junfei wang


Video Answer


1 Answers

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 e.g.

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;
        });
    });
}
like image 162
starikoff Avatar answered Oct 02 '22 23:10

starikoff