Rxjava2: Map exception into Single<Boolean> - rx-java2

I'm trying to write a method the returns Single<Boolean> and it's functionality is to check whether an HTTP server is alive.
so it sends a ping to server and it works fine in case server is online, but in case server is down it returns an exception which is propagated to subscribe.
I would like to catch the exception and map it into false and return internally from isAlive method.
Here is my attempt:
public Single<Boolean> isAlive()
{
return client().get(HEALTH_CHECK_PATH) //
.rxSend() //
.onErrorReturnItem(null) //
.map(response -> {
return response.statusCode() == HttpResponse.HTTP_STATUS_NO_CONTENT;
});
}
I tried to return null and on the map check for null and return a boolean accordingly - map Single doesn't handle null....any idea?
Thanks.
EDIT-1
I've tried:
.onErrorResumeNext(err -> Single.just(Boolean.FALSE))
it doesn't compile..

Solved by doing:
public Single<Boolean> isAlive()
{
return client().get(HEALTH_CHECK_PATH) //
.rxSend() //
.map(response -> response.statusCode() == HttpResponse.HTTP_STATUS_NO_CONTENT)//
.onErrorResumeNext(err -> Single.just(Boolean.FALSE));
}

Related

Reactive programming - Return response when the result is empty

I have a reactive code below which does the following.
query items table by ID - (itemRepository.findByd(itemId))
Create a map from the result from Step1
Invoke retrieveItemsQty from DB
Here, it works fine when the Step1 returns one or more result and retrieveItemsQty method fails when the result is empty. My requirement is to return back when the first step result(itemRepository.findByd) is empty. Not sure how to do this?
private Mono<Long> updateItemsQty(final Long itemId) {
return itemRepository.findByd(itemId).collectList()
.zipWhen((items) -> Mono.just(items.stream()
.collect(Collectors.toMap(ItemQty::getId, ItemQty::getQty))))
.map((tuple) -> tuple.getT2())
.zipWhen((items) -> qtyRepository
.retrieveItemsQty(items.keySet()).collectList())
.zipWhen((tuple) -> reduceItemQty(tuple.getT2(), tuple.getT1(), itemId))
.flatMap((response) -> {
return Mono.just(itemId);
});
}
I tried switchIfEmpty and defaultIfEmpty like the below.
return itemRepository.findByd(itemId).collectList()
.switchIfEmpty(). /// Looks like the return statement is not allowed here.
.zipWhen((items) -> Mono.just(items.stream()
.collect(Collectors.toMap(ItemQty::getId, ItemQty::getQty))))
In case you want to keep the current flow, the easiest way would be to use filter
return itemRepository.findByd(itemId)
.collectList()
.filter(items -> !items.isEmpty())
...
but I would suggest to simplify the flow to make it more readable and don't overuse reactive operators where you don't really need them. For example, something like
return itemRepository.findByd(itemId)
.collectList()
.flatMap(items -> {
if (items.isEmpty()) {
return Mono.empty();
}
Map<Long, Integer> itemMap = items.stream()
.collect(Collectors.toMap(ItemQty::getId, ItemQty::getQty));
return retrieveItemsQty(itemMap.keySet())
.collectList()
.flatMap(availableQty -> reduceItemQty(availableQty, itemMap, itemId));
})
.then(Mono.just(itemId));
you could simplify even futher and move inner body into a separate method

In Spring WebClient used with block(), get body on error

I am using WebClient from Spring WebFlux to communicate with a REST API backend from a Spring client.
When this REST API backend throws an exception, it answers with a specific format (ErrorDTO) that I would like to collect from my client.
What I have tried to do is to make my client throw a GestionUtilisateurErrorException(ErreurDTO) containing this body once the server answers with a 5xx HTTP status code.
I have tried several options :
I/ onStatus
#Autowired
WebClient gestionUtilisateursRestClient;
gestionUtilisateursRestClient
.post()
.uri(profilUri)
.body(Mono.just(utilisateur), UtilisateurDTO.class)
.retrieve()
.onStatus(HttpStatus::is5xxServerError,
response -> {
ErreurDTO erreur = response.bodyToMono(ErreurDTO.class).block();
return Mono.error(new GestionUtilisateursErrorException(erreur));
}
)
.bodyToMono(Void.class)
.timeout(Duration.ofMillis(5000))
.block();
This method doesn't work because webclient doesn't allow me to call the block method in the onStatus. I am only able to get a Mono object and I can't go further from here.
It seems like "onStatus" method can't be used in a WebClient blocking method, which means I can throw a custom Exception, but I can't populate it with the data from the response body.
II/ ExchangeFilterFunction
#Bean
WebClient gestionUtilisateursRestClient() {
return WebClient.builder()
.baseUrl(gestionUtilisateursApiUrl)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.filter(ExchangeFilterFunction.ofResponseProcessor(this::gestionUtilisateursExceptionFilter))
.build();
}
private Mono<ClientResponse> gestionUtilisateursExceptionFilter(ClientResponse clientResponse) {
if(clientResponse.statusCode().isError()){
return clientResponse.bodyToMono(ErreurDTO.class)
.flatMap(erreurDto -> Mono.error(new GestionUtilisateursErrorException(
erreurDto
)));
}
return Mono.just(clientResponse);
}
This method works but throw a reactor.core.Exceptions$ReactiveException that I am struggling to catch properly (reactor.core.Exceptions is not catchable, and ReactiveException is private).
This Exception contains in its Cause the exception I need to catch (GestionUtilisateurErrorException) but I need a way to catch it properly.
I also tried to use "onErrorMap" and "onErrorResume" methods but none of them worked the way I needed.
Edit 1 :
I am now using the following workaround even if I feel it's a dirty way to do what I need :
gestionUtilisateursRestClient
.post()
.uri(profilUri)
.body(Mono.just(utilisateur), UtilisateurDTO.class)
.retrieve()
.onStatus(h -> h.is5xxServerError(),
response -> {
return response.bodyToMono(ErreurDTO.class).handle((erreur, handler) -> {
LOGGER.error(erreur.getMessage());
handler.error(new GestionUtilisateursErrorException(erreur));
});
}
)
.bodyToMono(String.class)
.timeout(Duration.ofMillis(5000))
.block();
}
catch(Exception e) {
LOGGER.debug("Erreur lors de l'appel vers l'API GestionUtilisateur (...)");
if(ExceptionUtils.getRootCause(e) instanceof GestionUtilisateursErrorException) {
throw((GestionUtilisateursErrorException) e.getCause());
}
else {
throw e;
}
}
Here, it throws the expected GestionUtilisateursErrorException that I can handle synchronously.
I might implement this in a global handler to avoid writing this code around each call to my API.
Thank you.
Kevin
I've encountered a similar case for accessing the response body that might be of use to you using the Mono.handle() method (see https://projectreactor.io/docs/core/release/api/index.html?reactor/core/publisher/Mono.html).
Here handler is a SynchronousSink (see https://projectreactor.io/docs/core/release/api/reactor/core/publisher/SynchronousSink.html) and can call at most next(T) one time, and either complete() or error().
In this case, I call 'handler.error()' with a new GestionUtilisateursErrorException constructed with the 'erreur'.
.onStatus(h -> h.is5xxServerError(),
response -> {
return response.bodyToMono(ErreurDTO.class).handle((erreur, handler) -> {
// Do something with erreur e.g.
log.error(erreur.getErrorMessage());
// Call handler.next() and either handler.error() or handler.complete()
handler.error(new GestionUtilisateursErrorException(erreur));
});
}
)

Failure handler issue using Vertx web routes and Minity in a Quarkus application

I have tried to use Munity API and Vertx Route together. The complete codes are here.
I defined a router rule like this.
router.put("/posts/:id").consumes("application/json").handler(BodyHandler.create()).handler(handlers::update);
And the handler implementation is like this.
LOGGER.log(Level.INFO, "\npath param id: {0}\nrequest body: {1}", new Object[]{id, body});
var form = fromJson(body, PostForm.class);
this.posts.findById(UUID.fromString(id))
.onFailure(PostNotFoundException.class).invoke(ex -> rc.response().setStatusCode(404).end())
.map(
post -> {
post.setTitle(form.getTitle());
post.setContent(form.getContent());
return this.posts.update(UUID.fromString(id), post);
}
)
.subscribe()
.with(data -> rc.response().setStatusCode(204).end());
In the findById method , it throws an PostNotFoundException.
public Uni<Post> findById(UUID id) {
return this.client
.preparedQuery("SELECT * FROM posts WHERE id=$1")
.execute(Tuple.of(id))
.map(RowSet::iterator)
// .map(it -> it.hasNext() ? rowToPost(it.next()) : null);
.flatMap(it -> it.hasNext() ? Uni.createFrom().item(rowToPost(it.next())) : Uni.createFrom().failure(PostNotFoundException::new));
}
When running the application, and do update(via HTTP PUT method) on the /posts/postid with a none existing post id, it will print a 404 error status as expected, but there is some pause time in the period.
In another handler method, it calls findById like the following and it works well and responds quickly when it is not found.
this.posts.findById(UUID.fromString(id))
.subscribe()
.with(
post -> rc.response().end(toJson(post)),
throwable -> rc.fail(404, throwable)
);

Throw an error when the error comes in the payload of a 200 status code

I have one API that returns errors in several ways (I know it's not very good idea but it's a legacy API). I have to concatenate three API calls only if the previous call is successful, I have working this approach except for some cases that returns errors in payload (With a 200 status). For do this I want to use Spring Web Client and I do the following:
Mono
.when(placeOrderCall)
.doOnSuccess(response -> {
if (!isSuccesful(response)) {
throw new Exception("");
}
})
.then(changeSubscription)
.doOnSuccess(response -> {
if (!isSuccesful(response)) {
throw new Exception("");
}
})
.then(httpCall)
.doOnSuccess(response -> {
if (!isSuccesful(response)) {
throw new Exception("");
}
})
.doOnError(error -> System.out.println("Error"))
.block();
But I can't throw an exception in doOnSuccess...I want to break the flow and send it to doOnError. Is this posible?
Thanks!!!
Since, you haven't mentioned why you are not able to throw exception in doOnSuccess so I am guessing that it is because of checked Exception. So, for this, simple solution is to throw RuntimeException like below -
throw new RuntimeException("");
Please, read reactor documentation part about handle() method.
Your code can be rewritten as following then:
Mono
.when(placeOrderCall)
.handle((res, sink) -> {
if (!isSuccesful(response)) {
sink.error(new Exception(""));
} else {
sink.next(res);
}
})
.then(changeSubscription)
.handle((res, sink) -> {
if (!isSuccesful(response)) {
sink.error(new Exception(""));
} else {
sink.next(res);
}
})
.then(httpCall)
.handle((res, sink) -> {
if (!isSuccesful(response)) {
sink.error(new Exception(""));
} else {
sink.next(res);
}
})
.doOnError(error -> System.out.println("Error"))
.block();
In case any handle method executes sink.error() the subsequent then() functions won't be called and only the last .doOnError() will be invoked.

Repeat Single based on onSuccess() value

I want to repeat a Single based on the single value emitted in onSuccess(). Here is a working example
import org.reactivestreams.Publisher;
import io.reactivex.Flowable;
import io.reactivex.Single;
import io.reactivex.functions.Function;
public class Temp {
void main() {
Job job = new Job();
Single.just(job)
.map(this::processJob)
.repeatWhen(new Function<Flowable<Object>, Publisher<?>>() {
#Override
public Publisher<?> apply(Flowable<Object> objectFlowable) throws Exception {
// TODO repeat when Single emits false
return null;
}
})
.subscribe();
}
/**
* returns true if process succeeded, false if failed
*/
boolean processJob(Job job) {
return true;
}
class Job {
}
}
I understand how repeatWhen works for Observables by relying on the "complete" notification. However since Single doesn't receive that notification I'm not sure what the Flowable<Object> is really giving me. Also why do I need to return a Publisher from this function?
Instead of relying on a boolean value, you could make your job throw an exception when it fails:
class Job {
var isSuccess: Boolean = false
}
fun processJob(job: Job): String {
if (job.isSuccess) {
return "job succeeds"
} else {
throw Exception("job failed")
}
}
val job = Job()
Single.just(job)
.map { processJob(it) }
.retry() // will resubscribe until your job succeeds
.subscribe(
{ value -> print(value) },
{ error -> print(error) }
)
i saw a small discrepancy in the latest docs and your code, so i did a little digging...
(side note - i think the semantics of retryWhen seem like the more appropriate operator for your case, so i've substituted it in for your usage of repeatWhen. but i think the root of your problem remains the same in either case).
the signature for retryWhen is:
retryWhen(Function<? super Flowable<Throwable>,? extends Publisher<?>> handler)
that parameter is a factory function whose input is a source that emits anytime onError is called upstream, giving you the ability to insert custom retry logic that may be influenced through interrogation of the underlying Throwable. this begins to answer your first question of "I'm not sure what the Flowable<Object> is really giving me" - it shouldn't be Flowable<Object> to begin with, it should be Flowable<Throwable> (for the reason i just described).
so where did Flowable<Object> come from? i managed to reproduce IntelliJ's generation of this code through it's auto-complete feature using RxJava version 2.1.17. upgrading to 2.2.0, however, produces the correct result of Flowable<Throwable>. so, see if upgrading to the latest version generates the correct result for you as well.
as for your second question of "Also why do I need to return a Publisher from this function?" - this is used to determine if re-subscription should happen. if the factory function returns a Publisher that emits a terminal state (ie calls onError() or onComplete()) re-subscription will not happen. however, if onNext() is called, it will. (this also explains why the Publisher isn't typed - the type doesn't matter. the only thing that does matter is what kind of notification it publishes).
another way to rewrite this, incorporating the above, might be as follows:
// just some type to use as a signal to retry
private class SpecialException extends RuntimeException {}
// job processing results in a Completable that either completes or
// doesn't (by way of an exception)
private Completable rxProcessJob(Job job) {
return Completable.complete();
// return Completable.error(new SpecialException());
}
...
rxProcessJob(new Job())
.retryWhen(errors -> {
return errors.flatMap(throwable -> {
if(throwable instanceof SpecialException) {
return PublishProcessor.just(1);
}
return PublishProcessor.error(throwable);
});
})
.subscribe(
() -> {
System.out.println("## onComplete()");
},
error -> {
System.out.println("## onError(" + error.getMessage() + ")");
}
);
i hope that helps!
The accepted answer would work, but is hackish. You don't need to throw an error; simply filter the output of processJob which converts the Single to a Maybe, and then use the repeatWhen handler to decide how many times, or with what delay, you may want to resubscribe. See Kotlin code below from a working example, you should be able to easily translate this to Java.
filter { it }
.repeatWhen { handler ->
handler.zipWith(1..3) { _, i -> i }
.flatMap { retryCount -> Flowable.timer(retryDelay.toDouble().pow(retryCount).toLong(), TimeUnit.SECONDS) }
.doOnNext { log.warn("Retrying...") }
}