1

Spring is cool when it works. But I am having trouble understanding the best way to approach intermittent failure/retry issues for singletons wired up.

Let me give you two concrete examples:

  • SessionFactory being wired up
  • Hashicorp Vault being wired up

In both cases we have a remote server, that yes is fairly reliable, but if it happens to hiccup during initialization of a particular container class (say one that holds some Vault secrets or a SessionFactory), then what can one do to have that retried?

MJB
  • 9,352
  • 6
  • 34
  • 49
  • I'm adding to the story and scenario: I start up my app with spring-vault and the Vault container manages to start slower than my app. The app makes the call to the Vault through VaultPropertySource Configuration and it fails. This fails my entire container and app. I haven't seen a retry mechanism option in the docs. Is there one or is it going to be implemented? If I'm looking at this from a wrong angle, please shift my view :) – Daniel Colceag Dec 28 '17 at 14:08

1 Answers1

0

Spring Vault does not provide a retry mechanism. You could hook into ClientRequestFactory with a RetryTemplate using Spring Retry.

Retrying is a form of compensation. In this case (container does not boot properly/isn't healthy at the time of the first request), it looks like you'd be lacking a proper orchestration of startup and health checks. You're assuming the Vault service is healthy/started while your application start but that's not the case.

Retrying will help to some extent but it isn't a solution, it's a patch.

mp911de
  • 17,546
  • 2
  • 55
  • 95
  • I managed to implement the retry mechanism for the case in the question. Now I see spring-retry does not handle 404 errors, how can we configure the RestTemplate or HttpClient to retry on these kind of HTTP statuses ? as in hc.apache.org/httpclient-3.x/exception-handling.html hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/… . How can we provide a custom HttpMethodRetryHandler to the RetryableVaultTemplate clientHttpRequestFactory ? – Daniel Colceag Jan 04 '18 at 00:34
  • I managed to enable the retry mechanism for 404 from ``` @Override public VaultResponse doWithRetry(RetryContext context) { logger.debug("log me doWithRetry"); VaultResponse response = RetryableVaultTemplate.super.read(path); if (response != null) { return response; } throw new RuntimeException("Empty response received. Probably 404."); } }); }``` – Daniel Colceag Jan 04 '18 at 00:50
  • In the VaultTemplate the read method was checking for HttpStatus.NOT_FOUND and returned null. I checked for null and I've thrown a RuntimeException with "Empty response received. Probably 404" message. It would be nice to be able to catch the actual Exception. How can I do that? – Daniel Colceag Jan 04 '18 at 00:53