How to throttle webservice calls in a Java web application

Badal picture Badal · Oct 20, 2011 · Viewed 9.4k times · Source

My requirement is very simple to understand.

I want to call a web service from my Java web application with restriction of maximum 10 webservice calls per minute. Just after 1 minute, I can establish another 10 connection, regardless the state of previous 10 webservice calls (finished or unfinished).

Can somebody guide me the approach to implement this? Any tutorial or helpful links ?

Answer

sfussenegger picture sfussenegger · Oct 20, 2011

We use RequestThrottler (gist) that's inspired by this blog post.

Usage:

private static final int MAX_CALLS = 10;
private static final int PER_INTERVAL = 60000; // 60s
private static final int MAX_WAIT = 2000; // 2s

private RequestThrottler _throttler = new RequestThrottler(MAX_CALLS, PER_INTERVAL);
private SomeWebService _service = new SomeWebService();

public void callService() {
    throttler.startRequest(MAX_WAIT);
    _service.call();
}

Not that you might have to take care of possible congestion, especially if you plan to wait indefinitely as part of web requests.