14

Consider a code:

private WebClient webClient;

public void some(MyObject myObject) {
    return webClient.post()
            .uri("/log/my-path")
            .body(BodyInserters.fromObject(myObject))
            .retrieve()
            .bodyToMono(Void.class)
            .subscribeOn(Schedulers.single());
}

This code "waits" when response "appears" and then completes. (E.g. retrieve() is called). but how not to wait for response? For example, I make request and return Mono.empty without waiting for response. How to do that?

P.S. A technique when request is sent without wating for response is called "fire and forget".

UPDATED

  1. Any mono functions like then does not work. Because they invocated after bodyToMono which "waits" until http response is comming. E.g. nothing (event async) is called until bodyToMono completes.
  2. "remove" return statment also does not work. May be in some main functions it works but not in Spring application. This does not wok because Mono just created in that case, but nobody run it. Any Mono methods like map, flatMap etc. is just funciton "setting" but not Mono running.
4
  • remove the 'return' statement and and it will be fire in the background. But if you wanted to return Mono.empty then use .then() which is Mono<Void> Nov 5, 2018 at 4:15
  • 1
    remove the 'return' statement and and it will be fire in the background. No, mono will not be run at all in that case.
    – Cherry
    Nov 15, 2018 at 15:01
  • Bro, have you solved it?
    – Mr.Ustiik
    Apr 16, 2021 at 9:02
  • just remove return and add .subscribe(). the flow will start on Schedulers.single() thread. Feb 17, 2022 at 20:52

1 Answer 1

8

There are several ways to do that. You could do the following:

public void some(MyObject myObject) {
    return webClient.post()
            .uri("/log/my-path")
            .body(BodyInserters.fromObject(myObject))
            .retrieve()
            .bodyToMono(Void.class)
            .subscribe();
}

This has an important problem: even if you're not interested in the response itself, you might want to have specific properties like:

  • Being notified in case of errors and handling those
  • Making sure that, in case of a batch/CLI app, something is holding onto that request and prevents your application from exiting, thinking that everything is done

In that case, you can chain the response with a Mono<Void> type, which is completed as Mono.empty() or an error:

public Mono<Void> some(MyObject myObject) {
    return webClient.post()
            .uri("/log/my-path")
            .body(BodyInserters.fromObject(myObject))
            .retrieve()
            .bodyToMono(Void.class)
            .then();
}

In other parts of your application, you can then chain again with other publishers:

Mono<Void> requestSent = some(myObject);
Mono<Other> other = requestSent.then(otherMono);
2
  • 1
    then does not work. The problem here is bodyToMono "blocks" execution. E.g. it "wait" for data retrivement and only after that mono can be finished. It there a way to skip blocking?
    – Cherry
    Nov 15, 2018 at 14:58
  • 1
    Doing that is supposed to read a bit of data and cancel+close the connection. The downside is, the connection is not returned to the connection pool. We've fixed several issues in that space (see jira.spring.io/browse/SPR-17482, jira.spring.io/browse/SPR-17054). So at that point, it's best for you to create a sample application with the latest Spring version that shows the issue and create a new issue on jira.spring.io Nov 15, 2018 at 16:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.