Skip to main content

Ignoring Cancel Signals and Continuing Processing in a Spring WebFlux App

In a Spring WebFlux application, request processing is reactive, meaning that when a client cancels a request (e.g., due to a timeout or manual cancellation), Spring immediately stops execution by propagating a cancel signal in the reactive stream.

However, in some cases, we want to ensure that processing continues, even if the client disconnects. This is especially important when:

  • Database updates or external API calls must be completed to avoid an inconsistent state.
  • Long-running operations should not be interrupted.
  • Logging, auditing, or side effects must still be processed.

How to Keep Processing After a Cancel Signal

To decouple HTTP request processing from the application logic, we can use the .cache() operator. This ensures that the execution continues, even if the client drops the connection.

Understanding .cache() in Reactor


<T> Flux<T> cache() <T> Flux<T> cache(Duration ttl) <T> Flux<T> cache(int history) <T> Flux<T> cache(int history, Duration ttl)

The .cache() operator buffers elements emitted by the source Flux or Monoallowing re-subscription without re-executing the original operation. 

Source cannot be cancelled, so by applying .cache(), we can ensure that our logic continues even if the subscriber (HTTP client) disconnects.

Spring WebFlux Example


import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Mono; import java.time.Duration; @RestController public class BackgroundProcessingController { @GetMapping("/process") public Mono<String> processRequest(@RequestParam String taskId) { return performLongRunningTask(taskId) .cache(); // Ensures execution continues even if the client disconnects } private Mono<String> performLongRunningTask(String taskId) { return Mono.fromCallable(() -> { // Simulating a long-running operation Thread.sleep(5000); System.out.println("Processing completed for task: " + taskId); return "Task " + taskId + " completed"; }).subscribeOn(reactor.core.scheduler.Schedulers.boundedElastic()); } }


Explanation of the Code:

  1. performLongRunningTask(taskId) simulates a time-consuming operation.
  2. .cache() ensures that the operation continues even if the client cancels the request.
  3. subscribeOn(Schedulers.boundedElastic()) moves blocking work to an appropriate thread pool.

Final Thoughts

By using .cache(), we effectively decouple the HTTP request lifecycle from our business logic, ensuring that processing continues in the background even if the client disconnects.

Comments

Popular posts from this blog

Send WhatsApp Messages Automatically Using Apple Shortcuts

Apple Shortcuts is a great automation tool, but when it comes to WhatsApp, the default Send Message via WhatsApp  action has some limitations. It only allows sending messages to individual contacts (not groups) and always requires user interaction to press the send button. In this tutorial, I’ ll show you an alternative method that works for both individual contacts and groups without requiring manual confirmation. Steps to Automate WhatsApp Messages in Shortcuts 1. Send a Message Manually First Before setting up the shortcut, open the WhatsApp app and manually send a message to the contact or group you want to automate. This step ensures that WhatsApp registers the recipient in the action list later. 2. Add a Custom WhatsApp Action in Shortcuts Now, let’s configure the Shortcut: 1. Open the Shortcuts app and create a new shortcut. 2. Tap Add Action and search for WhatsApp . 3. Instead of selecting a specific action, tap on the WhatsApp app name itself. 4. Scrol...

Use Your Own Router When Your ISP Router Doesn’t Support Bridge Mode

Many internet service providers (ISPs) require you to use their provided router, and in some cases, these routers don’t support bridge mode. This can be frustrating if you want to use your own high-performance router for better speed, security, or customization. A common workaround is to place your second router in the DMZ (Demilitarized Zone) of the ISP-provided router. In this guide, I’ll explain how to set up your ISP’s router to work with your own router while minimizing issues. How This Works 1. Setting Up DMZ Mode Since your ISP’s router does not support full bridge mode, we’ll use DMZ to forward all traffic to your second router. This allows your better router to handle most of the network management, firewall, and routing tasks. 2. Dealing with Double NAT What is Double NAT? Your ISP’s router continues to perform NAT (Network Address Translation) for devices connected to it. Your second router will also perform NAT for devices connected to it. This is called double ...