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

Avatar

Avatar
Ignite 3
Level 2
Hemalatha
Level 2

Likes

2 likes

Total Posts

12 posts

Correct reply

0 solutions
Top badges earned
Ignite 3
Ignite 1
Boost 1
Applaud 5
View profile

Avatar
Ignite 3
Level 2
Hemalatha
Level 2

Likes

2 likes

Total Posts

12 posts

Correct reply

0 solutions
Top badges earned
Ignite 3
Ignite 1
Boost 1
Applaud 5
View profile
Hemalatha
Level 2

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
Give Back 3
MVP
Manjunath_K
MVP

Likes

140 likes

Total Posts

188 posts

Correct reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

140 likes

Total Posts

188 posts

Correct reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile
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
Validate 1
Level 3
puradawid
Level 3

Likes

12 likes

Total Posts

31 posts

Correct reply

3 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile

Avatar
Validate 1
Level 3
puradawid
Level 3

Likes

12 likes

Total Posts

31 posts

Correct reply

3 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile
puradawid
Level 3

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
Coach
Employee
Jörg_Hoh
Employee

Likes

1,134 likes

Total Posts

3,161 posts

Correct reply

1,079 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,134 likes

Total Posts

3,161 posts

Correct reply

1,079 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
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
Ignite 1
MVP
SureshDhulipudi
MVP

Likes

163 likes

Total Posts

174 posts

Correct reply

49 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile

Avatar
Ignite 1
MVP
SureshDhulipudi
MVP

Likes

163 likes

Total Posts

174 posts

Correct reply

49 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile
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.