This is what I'm trying to achieve:
When somebody requests http://localhost/runIt, I would like to return data from cache that would be refreshed every 6 seconds. Below, I have a flux (always same one that is stored in map) that is first time instantiated and starts emitting numbers 0,1,2,3,4... to infinity.
Is it possible to make this Spring MVC Controller method return "1,2" on first request, then on request after 7 seconds to return "3,4" etc. ?
Also, if lastRunIt is not updated for 60 seconds, I would need to terminate the flux.
This code below is something that I had in mind, but it is currently not working at all.
Map<Integer, Flux<String>> itos = new ConcurrentHashMap<>();
Instant lastRunIt;
@GetMapping("runIt")
public Flux<String> runIt(){
lastRunIt = Instant.now();
return itos.computeIfAbsent(1, k ->
Flux.interval(Duration.ZERO, Duration.ofSeconds(3))
.doOnNext(x -> {
//dispose if no request for 60 seconds
if(lastRunIt.plusSeconds(60).isBefore(Instant.now())){
//someDispisable.dispose(); //<--- HOW TO GET Disposable here?
}
System.out.println(x);
})
.cache(Duration.ofSeconds(6))
);
}