When accessing a Flux
that is returning infinite results via Chrome or JS EventSource
, neither "X" on Chrome or running Javascript EventSource.close()
causes the FluxEmitter
's onCancel()
or onDispose()
to fire. These events only seem to run on Spring shutdown.
How do I get the Flux
to close when the client stops?
Here is an example of the Flux
;
public class MyRepository {
public Flux<MyObject> getFlux() {
return Flux<MyObject>.create(sink -> {
for(...) { // get data we have now in EhCache.
sink.next(myObject);
}
myListener = new MyListener() { // get updates from EhCache events.
public void onEvent(MyEvent e) {
sink.next(e.getNewValue());
}
};
sink.onCancel(() -> {
System.out.println("** CANCELLED **");
sink.complete();
}
sink.onDispose(() -> {
System.out.println("** DISPOSED **");
}
}
);
}
And here is the end point;
@Autowired MyRepository myRepository;
@GetMapping(value="/myUrl", produces=MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<MyObject> getMyObjects() {
myRepository.getFlux();
}
Without the cancel\close and after a number of request from the Flux, Spring starts to die as resources are not being released.
I am using Sping 5.1.5, Boot 2.1.3, Reactor 3.2.6 and Netty 4.1.33.