Spring Integration & Retry: Do I need a separate r

2019-02-21 05:00发布

问题:

I've got a spring integration pipeline and I've got a number of different service activators that I want to enable retry for.

I want to use the same retry policy (i.e. the number of retries, back-off policy, etc). Can I just have one bean that implements the retry policy and use it for several different service activators, or does each service activator need its own retry bean? In other words, can I just make one bean "retryWithBackupAdviceSession" and set it the request-hadler-advice-chain for several service activators? Or does each one need its own?

Here's an example of the retry policy I'm using.

<bean id="retryWithBackoffAdviceSession" class="org.springframework.integration.handler.advice.RequestHandlerRetryAdvice">
            <property name="retryTemplate">
                <bean class="org.springframework.retry.support.RetryTemplate">
                    <property name="backOffPolicy">
                        <bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
                            <property name="initialInterval" value="2000" />    <!-- 2 seconds -->
                            <property name="multiplier" value="2" />            <!-- double the wait each time -->
                            <property name="maxInterval" value="30000"/>        <!-- maximum of 30 seconds -->
                        </bean>
                    </property>
                    <property name="retryPolicy">
                        <bean class="org.springframework.retry.policy.SimpleRetryPolicy">
                            <property name="maxAttempts" value="3"/>
                        </bean>
                    </property>
                </bean>
            </property>
            <property name="recoveryCallback">
                <bean class="org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer">
                    <constructor-arg ref="myErrorChannel"/>
                </bean>
            </property>
        </bean>

As a follow up-question, if my service activator is running in an executor channel, does it somehow keep track of the retries per-thread? Or is there something I need to do to ensure that there isn't cross-talk between the different threads retrying on different messages on the same thread-safe service activator?

回答1:

You go right way: the RequestHandlerRetryAdvice is thread-safe, so you can use the same beand from several places.



回答2:

I had to implement Retry mechanism in my project as well and I created my own implementation.

Retry using AOP

This works like a charm.

You can just annotate your methods with the @Retry annotation, provide some config you want and its done.