Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to throttle webservice calls in a Java web application

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 ?

like image 781
Badal Avatar asked Oct 20 '11 07:10

Badal


2 Answers

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.

like image 112
sfussenegger Avatar answered Oct 18 '22 19:10

sfussenegger


Have a look at Apache Camel and his implementation of throttler http://camel.apache.org/throttler.html.

like image 20
wesoly Avatar answered Oct 18 '22 19:10

wesoly