How to throttle webservice calls in a Java web application
Asked Answered
M

3

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 ?

Monet answered 20/10, 2011 at 7:29 Comment(0)
B
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.

Businesslike answered 20/10, 2011 at 8:26 Comment(4)
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?Monet
@Monet 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.Businesslike
@ 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. :)Monet
@Monet 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.Businesslike
C
3

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

Corker answered 20/10, 2011 at 8:39 Comment(2)
Do you have any quick code sample for this Camel throttler implementation?Monet
grepcode.com/file/repo1.maven.org/maven2/org.apache.camel/… ?Unguent
J
0

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

Jugular answered 21/8, 2013 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.