3

I have spring batch application with spring MVC. In this application, I have to call Google API. There is a restriction of max 4 req per sec for API. Now I have to call google API from inside the spring batch. So I have two questions.

q1: How can I implement rest call to Google API. I know about Rest Template but I want that there is any better approach like feign client that we use in microservices.

q2: how can I restrict 4 calls per second.

In case you have any question. Please let me know

Vimit Dhawan
  • 597
  • 1
  • 7
  • 25
  • What do you want to happen in case there are 5 calls per second? Drop the 5th call (eg. return an error) or wait another second so you are no longer rate limited? Possibly you might be interested in something like this: https://stackoverflow.com/questions/31883739/throttling-method-calls-using-guava-ratelimiter-class – g00glen00b Jan 15 '19 at 13:47
  • What Google API do you need to consume? – Tom Van Rossom Jan 15 '19 at 15:20
  • Actually, I used this logic in spring batch where I have continuous 15K hit to Google API. But API has a limit of 4 requests per second so I don't want to make more hit to Google API because maybe there is some cost issue. – Vimit Dhawan Jan 15 '19 at 17:47
  • @TomVanRossom https://maps.googleapis.com – Vimit Dhawan Jan 15 '19 at 17:47
  • Take a look at this opensource java library https://github.com/googlemaps/google-maps-services-java – Tom Van Rossom Jan 16 '19 at 08:04

3 Answers3

3

You can limit API call per second by using a RateLimiter. There is one implemented in Guava

You need to create the RateLimiter and tell how many calls per second.

final RateLimiter rateLimiter = RateLimiter.create(4.0); // rate is "4 permits per second"

Every time you want to limit, you need to acquire a permit. If all permits are used, executions waits.

rateLimiter.acquire(1);

It is also possible to specify a timeout on how long to wait for a permit.

Tom Van Rossom
  • 1,440
  • 10
  • 18
0

in our spring-boot project we use OkHttpClient3 as http client. We also make rest calls to lots of different public APIs. Some of them restricts calls per second. As a solution we implemented an Interceptor called DelayInterceptor.

Basically; create a Java Class that implements okhttp3.Interceptor. In it's public Response intercept(Chain chain) method look for the host you are requesting (to differentiate between calls made to different public APIs) using chain.request().url().host() and if you made a call to this host already use Thread.sleep(amount);

Since our project relatively new, we did not analyze possible drawbacks, but so far it works.

P.S: You can also look into AsyncHttpClient project, which already has a solution for this problem (even though I could not find with a simple google search).

oldborn
  • 86
  • 2
0

I have created RateLimiter that can be used to limit API. It's generic solutions for the thread you can improve and implement for your API.

Rate Limiting API

Vimit Dhawan
  • 597
  • 1
  • 7
  • 25