title: Specifying an exception-specific backoff policy with Spring-Retry
body: <p>I am using <a href="http://docs.spring.io/spring-retry/docs/1.1.2.RELEASE/apidocs/" rel="noreferrer">Spring-Retry</a> for some database operations.  On a <code>SQLRecoverableException</code> I retry three times (this assumes that whatever is causing the exception is non-transient if it fails three times), on a <code>SQLTransientException</code> I retry indefinitely (the program can't do anything without access to the database, so it may as well keep retrying until the user decides to reboot the server), and on any other exception I don't retry.  I use an exponential backoff policy with a base retry of 100ms and a max retry of 30,000ms.</p>
  
  <pre><code>private static final int MAX_RECOVERABLE_RETRIES = 3;
  private static final long INITIAL_INTERVAL = 100;
  private static final long MAX_INTERVAL = 30 * 1000;
  private static final double MULTIPLIER = 2.0;
  
  public static RetryTemplate databaseTemplate() {
      RetryTemplate template = new RetryTemplate();
      ExceptionClassifierRetryPolicy retryPolicy = new ExceptionClassifierRetryPolicy();
      Map&lt;Class&lt;? extends Throwable&gt;, RetryPolicy&gt; policyMap = new HashMap&lt;&gt;();
      NeverRetryPolicy baseException = new NeverRetryPolicy();
      SimpleRetryPolicy recoverablePolicy = new SimpleRetryPolicy();
      recoverablePolicy.setMaxAttempts(MAX_RECOVERABLE_RETRIES);
      AlwaysRetryPolicy transientPolicy = new AlwaysRetryPolicy();
      policyMap.put(Exception.class, baseException);
      policyMap.put(SQLRecoverableException.class, recoverablePolicy);
      policyMap.put(SQLTransientException.class, transientPolicy);
      retryPolicy.setPolicyMap(policyMap);
      template.setRetryPolicy(retryPolicy);
      ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
      backOffPolicy.setInitialInterval(INITIAL_INTERVAL);
      backOffPolicy.setMaxInterval(MAX_INTERVAL);
      backOffPolicy.setMultiplier(MULTIPLIER);
      template.setBackOffPolicy(backOffPolicy);
      return template;
  }
  </code></pre>
  
  <p>Ideally, I would like to use a fixed backoff of 100ms for all <code>SQLRecoverableExceptions</code>, and only apply the exponential backoff policy to <code>SQLTransientExceptions</code>.  I could accomplish this with nested retries, but that will greatly increase the code complexity - given no other option I would prefer to simply apply the exponential backoff to both <code>SQLRecoverableException</code> and <code>SQLTransientException</code> exceptions.</p>
  
  <p>Is there a way for me to apply different backoff policies to different exceptions using a single retry template?</p>