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?
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.