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 ?
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.
Have a look at Apache Camel and his implementation of throttler http://camel.apache.org/throttler.html.
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