Skip to content

Frequently Asked Questions

Anu Thomas Chandy edited this page Aug 23, 2019 · 20 revisions

My app uses the async client libraries, but it never seems to get any results

AsyncClient methods returns reactor types - Flux<T> or Mono<T> or types extending from one of these reactor types.

These reactor type instances are "cold" i.e. underlying work will not be activated without a subscription.

When the application that invokes a AsyncClient method exit without receiving any result, then check there is a subscription to initiate the work.

The simplest way to do subscription is to call subscribe() on the reactor type, but sometime even after subscription you might not receive result. Consider following example:

   Single<Response<Key>> result = asyncClient.getKey();
   result.doOnNext(response -> {
       System.out.println(response.value());
   })
   .subscribe();

While the subscribe() call cause execution of underlying work that makes API call to retrieve the key, sometime the lambda provided to doOnNext doesn’t get called.

The reason for this is - reactive type instances from AsyncClient method are "asynchronous".

"asynchronous" in this context means - the execution of underlying work as a result of subscription runs in a separate thread often in "IO thread". The doOnNext is supposed to be called from this "IO thread".

The subscribe() call happens from calling thread, (e.g. "Main Thread"). subscribe() immediately return without waiting for "IO Thread" to complete. If the "Main Thread" continue and exit then "IO Thread" may never get a chance to call the doOnNext lambda with result.

Solution is - Block the calling thread till the work and invocation of doOnNext lambda completes.

   Single<Response<Key>> result = asyncClient.getKey();
   result.doOnNext(response -> {
       System.out.println(response.value());
   })
   .blockingLast();
Clone this wiki locally