Skip to content

Frequently Asked Questions

Jonathan Giles edited this page Aug 25, 2019 · 20 revisions

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

The Java client library asynchronous methods returns reactive types - Flux<T> and Mono<T>, or types extending from these.

These reactive types are referred to as "cold". That is, all underlying work will not be activated without a subscription. When an application invokes an async method, it will return without receiving any result. Only once a subscription is added will work be initiated to receive results.

The simplest way to create a subscription is to call subscribe() on the reactive type, but sometimes even after subscription you might not receive result. Consider the following example:

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

While the subscribe() call causes execution of the underlying work that makes an API call to retrieve the key, sometimes the lambda provided to print out the response value does not get called.

The reason for this is obvious in hindsight: reactive types are asynchronous. The rest of the application continues to operate as per usual, and this includes having the application exit before the results are returned!

Knowing this, the easiest means to block execution and be sure you get the results before your application continues execution (and / or exits) is to use the various block calls available, for example:

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