Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

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

Avatar

Level 4

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

Avatar

Correct answer by
Level 8

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

Avatar

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.

Avatar

Employee Advisor

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

Correct answer by
Level 8

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!

Avatar

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.