1 year ago
#76119
Atlas
RxJava 3: How to use Futures with Observables/Singles
I'm relatively new to RxJava and I'm curious about how to use Futures with RxJava's Observable and/or Single. What I'm trying to make is a system to connect to a MongoDB database and load MongoDB collections. I would imagine that I could use Observable.fromFuture(new FutureTask<>())
and the FutureTask's Callable would handle the database connection then return true if it connected.
Now just for learning how to use Futures and Observables (or Singles), I created something very simple like so:
Debugger.warning("Begin Future");
FutureObserver<String> observer = Observable.<String>fromFuture(new FutureTask<>(new
Callable<String>() {
@Override
public String call() throws Exception {
return "Hello World";
}
}))
.doFinally(() -> Debugger.warning("Do Finally"))
.subscribeWith(new FutureObserver<>());
try {
Debugger.warning("Observer: " + observer.get());
} catch (Exception e) {
e.printStackTrace();
}
My Debugger simply prints to console. "Begin Future" is printed and the thread is then blocked and nothing happens, indefinitely. I know that when I actually use this within my codebase, I should have .subscribeOn(Schedulers.io())
, but for testing's sake, I left that out. I've spent the past couple days searching YouTube and Google searches for examples of how to use Futures with RxJava 3, and I just can't seem to figure this out. I appreciate any help or direction!
java
observable
rx-java
future
rx-java3
0 Answers
Your Answer