Rated Executor

Created: 2013/10/06 14:53:58+0000 Revised: 2013/11/10 16:29:56+0000 Revisions:  3 2 1

A colleague was working on a demo that polled the Twitter API. He had to solve the problem where he needed to ensure that the poll did not exceed the rate limits imposed on polling. This problem can easily be generalised into a generic problem, accessing a resource at no greater than a fixed rate. I didn't know of a generalised solution to the problem so I decided to implement one. A rated executor.

A rate limited executor or a rated executor is an executor that executes tasks no faster than a fixed rate but as soon as it can otherwise. The ScheduledExecutorService in the Java API provides similar behaviour but the rate is task not executor specific.

I've implemented an executor that provides this functionality. It has support for Runnable and Callable tasks. It supports repeated execution of the same task and returns Future objects to represent the pending execution. It is available on GitHub and as a Maven artifact. The documentation is available at the Maven generated site.

There are two internal implementations of the executor. One is based on the scheduled executor, the other is implemented using Threads. The one implemented using threads allows the task to be cancelled using interruptions. The internal executor is wrapped in an executor that wraps the tasks before passing them to the internal executor. This allows the behaviour on completed execution to be modified. This allows the internal executor to be decoupled from the Futures and the submission options.

For those of you interested in linked data for this project, see here: