3

I have a standard Jersey 2.7 client and would like to implement following scenario:

  1. Send JAX-RS request to a server.
  2. The server is unavailable (it has been shot down)
  3. The client retries the last request (I have load balancing on server side so another server should respond)

I have no clue how to implement retry mechanism in Jersey. Any ideas?

Michał Kowalczyk
  • 358
  • 1
  • 5
  • 21
  • And you expect the retry to not error out without something changing in between? A 500 means "something is wrong", I'd expect you to have to go figure out what that something is and fix it before trying again. I've been down that path before, and it only leads to incomprehensible logging because of repeated exceptions. – Gimby Apr 28 '14 at 15:11
  • Now I see that I didn't make myself clear. I have corrected the description. I want to retry when server is unavailable. – Michał Kowalczyk Apr 29 '14 at 07:27
  • Okay, but the same deal: you don't want to keep hammering because the server will be unavailable a second later too most likely. You'd have to delay the retry for a significant amount of time to make this in any way a useful feature. That enters the territory of job scheduling. – Gimby Apr 29 '14 at 09:47
  • When another call will be made, the HR Proxy will switch it to the working server. The problem is that when first server is unavailable, I need to make another call to allow HAProxy to switch the servers. And I don't know how. – Michał Kowalczyk Apr 29 '14 at 12:03
  • 2
    @MichałKowalczyk why not making it in a `while` loop, as long as the response is e.g "503 Service Unavailable", eventually with a Sleep in between? – V G Apr 29 '14 at 14:17
  • I'd expect your client to throw some kind of ConnectException when it discovers that the server is not reachable. You could build client outside of the try/catch and then catch ConnectException and just re-execute the client (probably with a short wait beforehand). – Erik Gillespie May 02 '14 at 20:37
  • Here's another use-case for retries: when using an authentication token (e.g. OAuth2) and the token becomes invalid after a certain time it results in a failed request (e.g. 401). It would be nice to get a new token then automatically retry the unauthorized request. – Chris H. Aug 07 '15 at 21:13

0 Answers0