Bean Creation Retry

As mentioned previously, @Retryable advice is integrated right at the container level. This is useful as it is common problem in Microservices and environments like Docker where there may be a delay in services becoming available.

The following snippet is taken from the Neo4j driver support and demonstrates how bean creation can be wrapped in retry support:

  1. @Factory (1)
  2. public class Neo4jDriverFactory {
  3. ...
  4. @Retryable(ServiceUnavailableException.class) (2)
  5. @Bean(preDestroy = "close")
  6. public Driver buildDriver() {
  7. ...
  8. }
  9. }
1A factory bean is created that defines methods that create beans
2The @Retryable annotation is used to catch ServiceUnavailableException and retry creating the driver before failing startup.