In order to read all google updates for the whole locations of I have in MyBusinessBusinessInformation.Locations.GetGoogleUpdated, I am using ExecutorService, Supposing mybusinessUpdateLocations is of type MyBusinessBusinessInformation which takes care of authentication as well, and the variable list is of type java.util.List containing required info of the locations to do the request from Google.
The problem is that sometimes my list includes more than 3000 locations and Google returns me Exception. my abstract code is:
ExecutorService ex =Executors.newFixedThreadPool(20);
for(int i=0;i<list.size();i++) {
ex.execute(new Runnable() {
@Override
public void run() {
try {
String locationName ="The location name";
String readMask="storeCode,regularHours,name,languageCode,title,phoneNumbers,categories,storefrontAddress,websiteUri,regularHours,specialHours,serviceArea,labels,adWordsLocationExtensions,latlng,openInfo,metadata,profile,relationshipData,moreHours";
MyBusinessBusinessInformation.Locations.GetGoogleUpdated
updateList=mybusinessUpdateLocations.locations()
.getGoogleUpdated(locationName).setReadMask(readMask);
GoogleUpdatedLocation response = updateList.execute();
} catch (Exception e) {
System.out.println( e);
}
in order to make the program run faster I used ExecutorService , however sometimes the code falls into Exception and I lose the specific location info:
{
"code" : 429,
"details" : [ {
"@type" : "type.googleapis.com/google.rpc.ErrorInfo",
"reason" : "RATE_LIMIT_EXCEEDED"
} ],
"errors" : [ {
"domain" : "global",
"message" : "Quota exceeded for quota metric 'Requests' and limit 'Requests per minute' of service 'mybusinessbusinessinformation.googleapis.com' for consumer 'project_number:XXXXXXXXX'.",
"reason" : "rateLimitExceeded"
} ],
"message" : "Quota exceeded for quota metric 'Requests' and limit 'Requests per minute' of service 'mybusinessbusinessinformation.googleapis.com' for consumer 'project_number:XXXXXXXXXXX'.",
"status" : "RESOURCE_EXHAUSTED"
}
The problem is that how I can put ALL the threads in sleep after a certain number of requests to Google API? for example after every 100 requests I want the executor to sleep for some seconds.
Thread.sleep(30000);
just put one thread on sleep and not all of them. Any help would be appreciated.