0
votes

I was hoping to get some hints for a small Spring Boot Application I'm building.

The application itself should function much like consuming a Kafka topic (or any pub/sub pattern). I however have no tools like Kafka available so it should all be done within the application itself.

The application will have a RestController that should expose a “hot” stream of Events when they happen.

@GetMapping(produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<Event> getApis() {
    return … 
}

The application will itself consume a stream of events from another application.

Where I need some hints is - how can I treat each consumer of this API as a new subscriber - and publish event to them as I get them in? Implementing a full pub-sub pattern keeping track of subscribers and when they stop consuming the endpoint should of course be possible. But I’m wondering if there isn’t an easier solution using some of Springs Reactive functionality. The Events I get in doesn't need to be stored - just forwarded to each consumer subscribing at the moment of the event.

Thanks

1

1 Answers

1
votes

Before returning your Flux<Event> from the controller handler, make sure that you're always returning:

  • the same instance, which is kept for example as an attribute in the controller
  • to apply the Flux.share() on it, so that all subscribers share the same hot stream of events