Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

How to re-try calls to Rest API from OSGi service

Hemalatha
Level 2
Level 2

I wants to re-try the Get call to some third party API which is errored out due to some exceptions. Is there any java framework available for re-try policy instead of manually calling it again in try-catch blocks. 

1 Accepted Solution
Manjunath_K
Correct answer by
Community Advisor
Community Advisor

Hi @Hemalatha,

Have you tried adding custom java code instead of looking for new framework to achieve this? if not, try adding similar logic as mentioned below with any additional condition as per your requirement & it will work.

 

int retryCount = 3;
int statusCode = 0;
HttpClient httpclient = new HttpClient(httpConnectionManager);

do{
try{
--retryCount;
GetMethod getMethod = new GetMethod("API End Point");
getMethod.setQueryString("Query Parameters");
statusCode = httpclient.executeMethod(getMethod);
}catch(){

}catch(){

}
}while(statusCode!= 200 && retryCount!= 0); //break loop if API return 200 status code OR 3 retry count completed

 

Hope this helps!

View solution in original post

4 Replies
SureshDhulipudi
Community Advisor
Community Advisor

you can use scheduler to call third party api with frequent intervals or as per schedule. 

If the process stops with the error / exception, the same can not run again.

Jörg_Hoh
Employee
Employee

There are a number Java frameworks around, which can do that. But I wouldn't look for the inclusion of a new framework if you want write that with a handful of lines java code 🙂

 

(You can put into a Sling Job; if this one terminates with an exception, it is retried... But I am not sure if that fits into your application design.)

Manjunath_K
Correct answer by
Community Advisor
Community Advisor

Hi @Hemalatha,

Have you tried adding custom java code instead of looking for new framework to achieve this? if not, try adding similar logic as mentioned below with any additional condition as per your requirement & it will work.

 

int retryCount = 3;
int statusCode = 0;
HttpClient httpclient = new HttpClient(httpConnectionManager);

do{
try{
--retryCount;
GetMethod getMethod = new GetMethod("API End Point");
getMethod.setQueryString("Query Parameters");
statusCode = httpclient.executeMethod(getMethod);
}catch(){

}catch(){

}
}while(statusCode!= 200 && retryCount!= 0); //break loop if API return 200 status code OR 3 retry count completed

 

Hope this helps!

View solution in original post

puradawid
Level 3
Level 3

There are a couple of libraries like that: they are mostly connected with some resilience packages, but I wouldn't bother in this case (although maybe you're interested in something like resilience4j, but in my opinion that's very large framework and you need some library, like this Guava Retrying

I am using it from time to time to my scripts that do some automation on AEM instances, and it does it very well. I am done with Apache HttpClient for years now in favor of some simple APIs like OkHttp or so, so I can't give you a code snippet just now. You should check documentation, it's pretty simple fluent-API to follow.