1

I am using a completablefuture to chain a bunch of async operation, I am wondering how to covert it to use RxJava, especially what's the benefit of using Rxjava vs CompletableFuture?

CompletableFuture cf = CompletableFuture.runAsync(() -> System.out.println("hello");
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
  • I'm voting to close this because it looks to me as an exact duplicate... if not please re-pharse your question – Eugene May 07 '17 at 08:42
  • 1
    RxJava 2 Jdk 8 Interop has a converter for it: https://github.com/akarnokd/RxJava2Jdk8Interop#completionstage-to-rxjava – akarnokd May 07 '17 at 09:26
  • Possible duplicate of [Difference between completableFuture,Future and Observable Rxjava](http://stackoverflow.com/questions/35329845/difference-between-completablefuture-future-and-observable-rxjava) – Lamorak May 10 '17 at 10:07

2 Answers2

1

You can create a Completable from a Future by calling the factory method Completable.fromFuture(cf).

More informations about futures and RxJava are given with Difference between completableFuture,Future and Observable Rxjava as mentioned by a commenter.

RxJava is whole library for reactive programming. At a glance, it will appear to be similar to Java 8's streams, which it is, except it's much more powerful.

Community
  • 1
  • 1
tynn
  • 38,113
  • 8
  • 108
  • 143
0

RxJava targets JRE 1.6, which is why the CompletableFuture is not supported out of the box. There are a few libraries out there that bridge the gap, like future-converter:

The answer given by @tynn is not recommended though - calling .fromFuture(cf) would create a non-asynchronous model, because it relies on calling the blocking Future.get(). Unlike the basic Future, CompletableFuture if fully asynchronous as it provides a callback mechanism. `