I am looking for a good solution or probably an API to solve the following problem:
No I am looking for an algorithm or an API which does exactly this task.
You can use a ScheduledExecutorService to schedule tasks for a given period of time.
For example, to schedule 100 tasks per second you can say:
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(nThreads);
scheduler.scheduleAtFixedRate(mailSender, 0, 10, TimeUnit.MILLISECONDS);
Obviously, you need to track how many tasks have executed and turn off the scheduler after the job is done.
Token bucket algorithm is very easy to implement and use yet very powerful. You can control the throughput at runtime and queue some requests to handle peeks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With