7

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 ?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Badal
  • 4,078
  • 4
  • 28
  • 28

3 Answers3

5

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.

sfussenegger
  • 35,575
  • 15
  • 95
  • 119
  • This seems good. Do I need to make the class singleton, as multiple user can hit at the same time. Or can I place this code that you posted in simple java class and use it in my servlet? – Badal Oct 20 '11 at 11:31
  • @Badal you can have as many instances as you want, e.g. for different web services. Therefore, I wouldn't make it a singleton. You definitely have to share the instance among all calls to a protected service. How to do that depends on your application though. For instance, if you're using Spring, you should make RequestThrottler a field of a bean of even a bean itself. Alternatively, you could store it in the application scope of your web application or do the throttling inside a filter. You see, lots of options here. – sfussenegger Oct 20 '11 at 11:49
  • @ sfussenegger: To make myself clear, let me explain you. Let say 500 user hit my server, so I will have 500 servlet instance. Each will create SomeWebService object, and throttler object (so I will have 500 service object as well as 500 throttler object). Now RequestThrottler class will take care of Maximum hit per minute. PLease goide me is my understanding right? and by the way, Thank you so much for your help. :) – Badal Oct 20 '11 at 12:07
  • @Badal you typically have only one Servlet instance shared among all requests. Therefore, you should create RequestThrottler in the Servlet's init(..) method. SomeWebService isn't strictly required to be a single instance only (depends on thread-safety and initialisation overhead of the object). Therefore you'd have one Servlet, one RequestThrottler instance, and typically one SomeWebService instance. – sfussenegger Oct 21 '11 at 09:58
3

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

wesoly
  • 552
  • 2
  • 11
  • Do you have any quick code sample for this Camel throttler implementation? – Badal Oct 21 '11 at 03:44
  • http://grepcode.com/file/repo1.maven.org/maven2/org.apache.camel/camel-core/2.9.2/org/apache/camel/impl/ThrottlingInflightRoutePolicy.java#ThrottlingInflightRoutePolicy.throttle%28org.apache.camel.impl.Route%2Corg.apache.camel.impl.Exchange%29 ? – mestachs Jul 21 '12 at 11:08
0

An open source project can be used for that: http://code.google.com/p/valogato/

Viktor
  • 1,325
  • 2
  • 19
  • 41