Giter Site home page Giter Site logo

java-8-completablefuture-demo's Introduction

Best-Price Finder

Toy example application demonstrating CompletableFuture of Java 8.

Adapted from the best-price finder application in Chapter 11 of Java 8 in Action.

Description

The application emulates a client-server application where the client makes calls with a non-neglectable latency to various servers. These calls could for example be HTTP requests.

The client makes use of the new CompletableFuture class of Java 8 to avoid blocking on these calls, and to string together multiple dependent and independent calls (i.e. the result of one call is needed as the input to another call).

Usage

gradle test

If you don't have Gradle installed, or only some incompatible version, use:

./gradlew test

Classes

Client:

  • BestPriceFinder

Servers:

  • Shop
  • DiscountService
  • ExchangeService

Tests:

  • BestPriceFinderTestSingleShop
  • BestPriceFinderTestAllShops

Notes

Java 8 CompletableFuture has in some way similar goals as RxJava, namely the composition of asynchronous programs (an issue of concurrency) in a declarative way. However, RxJava is focused on reacting to streams of data, whereas CompletableFuture is focused on reacting to single tems of data. The reactive stream nature of RxJava can however be imitated in Java 8 by combining the Streams API with CompletableFuture, that is, by working with Streams of CompletableFutures, as we do in our example application.

At the moment, the reactive programming capabilities of RxJava are much more comprehensive than the ones of CompletableFuture, especially due to the high number of stream operators that RxJava provides. However, CompletableFuture may still have its usage justifications, especially if the required asynchronous operations are simple, like single network requests that return a single response.

In Java 9, the Java language takes another step in the direction of the reactive programming paradigm by the introduction of the Flow API. This API implements the Reactive Streams specification and its goal is to improve the support for reactive programming in the core language. The Flow API is, however, not a replacement for RxJava, as it defines mostly interfaces, but no implementations of stream operators. These operators either need to be implemented by the users of the API, or a third-party library like RxJava can be used. Actually, it is planned that the Flow API and RxJava will live in some sort of symbiosis in Java 9.

In summary, thus, it is definitely worth to learn CompletableFuture of Java 8 for simple asynchronous programs. However, it is also worth to learn RxJava for more complex asynchronous programs, and RxJava most likely will still be relevant in future Java versions.

java-8-completablefuture-demo's People

Contributors

weibeld avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.