I have an application that receives events (using Spring Application Listeners) and I want to create a Flux of some historical events (that might be read from persistence) and an infinite stream of incoming events. The goal is to provide a public method that returns a Flux of all events that have ever been observed, i.e., the concatenation of the initial historical events, all events observed and all future events. The following service is supposed to provide the API in terms of the method stream().
@Service
public class EventService implements ApplicationListener<Event>{
private Flux<Integer> eventStream = getHistoricalEvents();
private Flux<Integer> getHistoricalEvents() {
return Flux.just(1,2,3,4,5);
}
@Override
public void onApplicationEvent(Event event) {
eventStream = eventStream.concatWithValues(event.hashCode());
}
public Flux<Integer> stream(){
return eventStream;
}
}
This works to achieve the first two goals, i.e., clients receive the initial set of historical events and all events that have been observed so far.
How can I add future events? Ideally, I am looking for a solution that achieves all that idiomatically as I am new to reactive programming.