Spring Integration & Retry: Do I need a separate retry bean for each service-activator?

拟墨画扇 提交于 2019-12-01 12:37:39

问题


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.



来源:https://stackoverflow.com/questions/21393517/spring-integration-retry-do-i-need-a-separate-retry-bean-for-each-service-act

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!