This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Retry on a specific condition

 
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello forum veterans,

I would like to retry a REST Call with the following conditions. Can anyone here help me on this ?

Scenario: I am making a REST Call that returns a list.

The retry should happen in the below cases:

1. When the list returned from REST Call is empty/null.
2. When the list is not empty/null and it does not contain the value that I am looking for.

Note that in case of any exception after making the REST Call, I don't want to retry.

I am looking for an elegant way to handle this.

Regards,
Ranagal
 
Saloon Keeper
Posts: 15276
350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This'll just keep going until it's found the value you're looking for, or an exception occurs.

The problem with this approach is that you'll be firing a lot of requests if it takes the server some time to update the list. You'll probably want to use a ScheduledExecutorService to schedule your REST requests after a certain delay, to prevent your application from swamping the server in requests.
 
Ranagal Adapla
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah. That is correct. I am aware of @Retry annotation. But I dont know how to customize it and use it for this scenario.
Also, for now the backoff time is 10seconds. I forgot to mention that in the original post.

By the way, thanks for the reply and your time.
 
Stephan van Hulst
Saloon Keeper
Posts: 15276
350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using Spring Batch? Are the REST calls made from your web service, or from a client application? How are you making the REST calls in the first place?
 
Ranagal Adapla
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Are you using Spring Batch? Are the REST calls made from your web service, or from a client application? How are you making the REST calls in the first place?



I am making the REST Call like below:

new RestTemplate().exchange(restUrl, httpMethod, httpRequestEntity, responseType, requestParams);
 
Stephan van Hulst
Saloon Keeper
Posts: 15276
350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what's the problem in using @Async and @Retryable?
 
Ranagal Adapla
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:So what's the problem in using @Async and @Retryable?



Sure. But I am not sure how I can customize it for my scenario. If you could help me on this, that would be great. Thanks in advance.
 
Stephan van Hulst
Saloon Keeper
Posts: 15276
350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd need to tell us what customization you need.
 
Ranagal Adapla
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You'd need to tell us what customization you need.



The retry should happen in the below cases:

1. When the list returned from REST Call is empty/null.
2. When the list is not empty/null and it does not contain the value that I am looking for.

Note that in case of any exception after making the REST Call, I don't want to retry.

The above is how I need to customize
 
Stephan van Hulst
Saloon Keeper
Posts: 15276
350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Upon further analysis, I don't think @Retryable is the right tool for this. That annotation is good if you want to retry when an exception occurs. You want to retry as part of normal execution flow.

Use @Async and CompletableFuture instead. It might look something like this:

Please note that I wrote this without testing it. It might contain bugs, or it might not work at all.
 
Ranagal Adapla
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your time and thoughts on this sir. I will use this as reference and test for my scenario.
 
Ranagal Adapla
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written something like this to test. And I have two questions on this:

1. It is not delaying for 5sec as mentioned in the code.
2. I would like to limit this retry for 3 times. And I don't see it anywhere.

Can you please help me with that ?

 
Stephan van Hulst
Saloon Keeper
Posts: 15276
350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll see if I can work out what the problem is tomorrow. In the mean time, please do the following things for me:

  • Let me know how you are calling that service. Are you calling it from a web controller? Can you show us?
  • Please remove the quoting of my posts from your last two posts. They're not necessary and they make the topic a bit harder to read.
  •  
    Ranagal Adapla
    Ranch Hand
    Posts: 39
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Let me know how you are calling that service. Are you calling it from a web controller? Can you show us?
    -> As a standard in our project, we have created a RESTRepository to make external REST Calls.  And we have written a method in the that repository and then from there, we are making the API call like below:
    new RestTemplate().exchange(restUrl, httpMethod, httpRequestEntity, responseType, requestParams);
    Please remove the quoting of my posts from your last two posts. They're not necessary and they make the topic a bit harder to read
    -> Done
     
    Ranagal Adapla
    Ranch Hand
    Posts: 39
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:I'll see if I can work out what the problem is tomorrow.



    Just a reminder sir
     
    A magnificient life is loaded with tough challenges. En garde tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic