1

I am using Annotation based Spring Retry. Below is my interface:

public interface IRetriever
{
      @Retryable(interceptor="RetryInterceptor") 
      public abstract List<Item> retrieve(final LogData transactionLogData, final RetrieveRequestType rqObject) throws InternalException, OpaqueExternalException;
}

There are three classes that implement this interface to call downstream services. I want to define RetryInterceptors such that for each downstream call, I should be able to define a separate retry Policy. In essence, I want to change RetryInterceptor at runtime.

Is there a way in Spring where I can change RetryInterceptor at runtime and make the code use a particular RetryOperationsInterceptor?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ancoder
  • 81
  • 1
  • 5
  • Why do you use abstract in interface? – Petar Petrov Sep 15 '16 at 07:52
  • 1
    Interceptors are created on bean creation. Changing that (while maybe possible) will be impracticable. I think you're usecase calls for a delegating retry interceptor. Have 1 interceptor and inject your sub-interceptors in there. The interceptor can then delegate to the correct one based on however you make that decission – pandaadb Sep 15 '16 at 09:18
  • Some old code. abstract keyword is obsolete, so should be removed. :). Thanks for pointing it out, will remove it when I build the above functionality. – ancoder Sep 15 '16 at 21:37

1 Answers1

1

You can't (easily) change the interceptor but you can change the RetryTemplate at runtime (setRetryOperations()).

The field in the interceptor is not volatile so it might take some time before all threads see the new template, until the CPU processor cache is clieared.

If you are using stateful retry, the state will be lost. In that case, you could change the retry policy in the existing template (that's in the interceptor).

Gary Russell
  • 166,535
  • 14
  • 146
  • 179