Reputation: 1294
Suppose the following @RestController
:
@GetMapping("listen")
public Flux<Object> listen() {
return Flux.create(sink -> process(sink));
}
And somewhere
sink.next(new Object());
This code has no information about sink state or completion
Tried using isCanceled
, it returns false every time.
Is it possible to detect is FluxSink
is still being used by the client?
Upvotes: 8
Views: 4386
Reputation: 1
You can try deploying it on another server to test, as I've also encountered this issue when deploying and debugging locally.
Upvotes: 0
Reputation: 709
the accepted answer is only work combined with "sever send event", the server send periodical event to client, when the client is disconnected the subscription will be canceled. as the document says https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux-codecs-streaming
Upvotes: 0
Reputation: 7133
In spring-webflux if the client close the connection the subscription will be canceled and disposed. If in the process method you add a callback onCancel and onDispose you will see that.
private <T> void process(final FluxSink<T> sink) {
sink.onCancel(new Disposable() {
@Override
public void dispose() {
System.out.println("Flux Canceled");
}
});
sink.onDispose(new Disposable() {
@Override
public void dispose() {
System.out.println("Flux dispose");
}
});
}
Then send an http request to your endpoint and cancel it before your flux complete. You will see that both callbacks are triggered.
Upvotes: 6