1

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.

lafual
  • 683
  • 1
  • 7
  • 23
  • 2
    Possible duplicate of [Spring Boot Webflux/Netty - Detect closed connection](https://stackoverflow.com/questions/48806452/spring-boot-webflux-netty-detect-closed-connection) – lafual Jun 03 '19 at 03:55
  • 1
    The issue is with Netty not capturing the close from the client. The solution in the link is to continually resend and have the server side discover that the connection is closed. I implemented this using `merge(Flux.interval(...).map(x->new getLastMyObject() )`, which is fine if you have a last object. Otherwise you would need to send fake messages, which would then necessitate the client having to handle/ignore them. (Not ideal) – lafual Jun 03 '19 at 03:59

0 Answers0