title: Spring integration with retry with ExceptionClassifierRetryPolicy
body: <p>I am using <code>int:request-handler-advice-chain</code> with my service activator. It is working correctly with <code>org.springframework.retry.policy.SimpleRetryPolicy</code> however I would like to use <code>org.springframework.retry.policy.ExceptionClassifierRetryPolicy</code> to allow for a different number of retries based on the exception thrown by the service activator.</p>
  
  <p>The problem I am having is that by the time the exception gets to the <code>ExceptionClassifierRetryPolicy</code> it is a </p>
  
  <blockquote>
    <p>org.springframework.integration.MessageHandlingException </p>
  </blockquote>
  
  <p>Can anyone advise on the best approach for get the cause (i.e my exception) from the <code>MessageHandlingException</code> made available to the <code>ExceptionClassifierRetryPolicy</code>?</p>
  
  <p>Solution thanks to Artem's suggestion below:</p>
  
  <p>Create a subclass of SubclassClassifier that returns the cause in the case of MessagingException</p>
  
  <pre><code>public class MessagingCauseExtractingSubclassClassifier extends SubclassClassifier&lt;Throwable, RetryPolicy&gt; {
  
      private static final Logger LOG = LoggerFactory.getLogger(MessagingCauseExtractingSubclassClassifier.class);
  
      public MessagingCauseExtractingSubclassClassifier(final Map&lt;Class&lt;? extends Throwable&gt;, RetryPolicy&gt; policyMap, final RetryPolicy retryPolicy) {
          super(policyMap, retryPolicy);
      }
  
      @Override
      public RetryPolicy classify(final Throwable throwable) {
          Throwable t = throwable;
          if (t instanceof MessagingException) {
              t = t.getCause();
              LOG.debug("Throwable is instanceof MessagingException so classifying cause type: {}", t.getClass());
          }
          return super.classify(t);
      }
  }
  </code></pre>
  
  <p>Then a new ExceptionClassifierRetryPolicy subclass that uses the new classifier and policyMap</p>
  
  <pre><code>public class MessasgeCauseExtractingExceptionClassifierRetryPolicy extends ExceptionClassifierRetryPolicy {
  
      @Override
      public void setPolicyMap(final Map&lt;Class&lt;? extends Throwable&gt;, RetryPolicy&gt; policyMap) {
          final MessagingCauseExtractingSubclassClassifier classifier = new MessagingCauseExtractingSubclassClassifier(
                  policyMap, new NeverRetryPolicy());
          setExceptionClassifier(classifier);
      }
  }
  </code></pre>
  
  <p>Currently this won't support retying on MessagingException but this is fine for our use case. Otherwise works perfectly. </p>