简体   繁体   中英

Stay subscribed to Flux/Mono after client disconnect in Spring WebFlux

I have a service where a couple requests can be long running actions. Occasionally we have timeouts for these requests, and that causes bad state because steps of the flux stop executing after the cancel is called when the client disconnects. Ideally we want this action to continue processing to completion.

I've seen WebFlux - ignore 'cancel' signal recommend using the cache method... Are there any better solutions and/or drawbacks to using cache to achieve this?

there are some solutions for that. One could be to make it asyncron. when you get the request from the customer you can put it in a processor

Sinks.Many<QueueTask<T>> queue = Sinks.many().multicast().onBackpressureBuffer()

and when the client comes from the customer you just push it to the queue and the queue will be in background processing the items. But in this case customer will not get any response with the progress of item. only if you send it by socket or he do another request after some times.

Another one is to use Chunked http request.

      @GetMapping(value = "/sms-stream/{s}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
  Flux<String> streamResponse(@PathVariable("s") String s) {
    return service.streamResponse(s);

  }

In this case the connection will be open and you can close it automatically in server when processing is done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM