'How do I create a hot flux that will provide data and replenish itself as needed

Since SecureRandom is slow, I was wondering how to make it so that I have a Flux that I can request the next random number, but it is prepopulated by say 100 items initially and is periodically replenished if is is less than 100 and obtains one dynamically if one is not available.

Something like

private Flux<byte[]> tokenFlux;
@PostConstruct
public void initFlux(){
  tokenFlux = Flux.<byte[]>generate(s-> {
    final byte[] bytes = new byte[32];
    secureRandom.nextBytes(bytes);
    s.next(bytes);
  })
  // some magic here
  .cache()
  // maybe more magic here
  ;
}

...

public foo() {

  tokenFlux.next()`

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source