Let's say I have:
list.stream()
.map(someService::someRateLimitedApiCall) //does not implement Runnable
.filter(Optional::isPresent)
.map(Optional::get)
.sleep(1000) //is something like this possible?
.min...;
The API service only allows limited number of transactions per second, and I am seeking to introduce a delay in between calls.
If not, is there a way to add an executor with a fixed delay within the iteration of the stream?
(To be clear, I am not violating the terms of the external API and will not abuse the service.)
Rather than use peek, why not just put the delay in the map operation which calls the API?
.map(e -> {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
return Optional.empty();
}
return someRateLimitedApiCall(e);
})