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

Avatar

Avatar

Hemalatha

Avatar

Hemalatha

Hemalatha

12-02-2021

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. 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Manjunath_K

MVP

Avatar

Manjunath_K

MVP

Manjunath_K
MVP

12-02-2021

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!

Answers (3)

Answers (3)

Avatar

Avatar

puradawid

Avatar

puradawid

puradawid

12-02-2021

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.

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.1K

Likes

1.1K

Correct Reply

1.1K

Avatar

Jörg_Hoh

Employee

Total Posts

3.1K

Likes

1.1K

Correct Reply

1.1K
Jörg_Hoh
Employee

12-02-2021

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.)

Avatar

Avatar

SureshDhulipudi

MVP

Avatar

SureshDhulipudi

MVP

SureshDhulipudi
MVP

12-02-2021

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.