2

有以下带有 Retry 的 Spring Integration 配置:

<int:chain input-channel="sdCreationChannel" output-channel="debugLogger">
    <int:poller fixed-delay="500" />
    <int:filter ref="sdIntegrationExistingRequestSentFilter" method="filter"/>
    <int:service-activator ref="sdCreationServiceImpl" method="processMessage">
        <int:request-handler-advice-chain>
            <ref bean="retryAdvice"/>
        </int:request-handler-advice-chain>
    </int:service-activator>
</int:chain>

<bean id="retryAdvice" 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="${integration.retry.initial.delay}"/>
                    <property name="multiplier" value="${integration.retry.backoff.multiplier}"/>
                </bean>
            </property>
            <property name="retryPolicy">
                <bean class="org.springframework.retry.policy.SimpleRetryPolicy">
                    <property name="maxAttempts" value="${integration.retry.max.attempts}" />
                </bean>
            </property>
        </bean>
    </property>
</bean>

简化的Java代码如下:

@Component("sdCreationServiceImpl")
public class SDCreationServiceImpl implements SDCreationService {

@Autowired
private NotifySD  notifySD;
@Override
public void processMessage(IntegrationPayload integrationPayload) {
List<ConfirmationCode> sdConfCodes = findCodesFromPayLoad(integrationPayload);
    notifySD.activateConfirmationCodes(sdConfCodes);

}  

重试此代码的问题在于,每次重试时可能会部分处理 List sdConfCodes,因此每次我们需要发送以处理较少数量的元素。组织此代码的最佳方法是什么?

根据 Artem Bilan 的建议(谢谢!),我在 SDCreationServiceImpl 中创建了带有变量列表的第二个方法,即 activateConfirmationCodes,然后在 XML 规范中将该方法指向为 sdCreationServiceImpl 的方法。

@Component("sdCreationServiceImpl")
public class SDCreationServiceImpl implements SDCreationService {
@Autowired
private NotifySD  notifySD;
List<ConfirmationCode> sdConfCodes = new ArrayList<ConfirmationCode()>;
@Override
public void processMessage(IntegrationPayload integrationPayload) {
sdConfCodes = findCodesFromPayLoad(integrationPayload);
}  

public void activateConfirmationCodes()
{
    notifySD.activateConfirmationCodes(sdConfCodes);
}

然后服务激活器的 XML 规范如下:

<int:service-activator ref="sdCreationServiceImpl" method="activateConfirmationCodes">
<int:request-handler-advice-chain>
<ref bean="retryAdvice"/>
</int:request-handler-advice-chain>
</int:service-activator>

是的,这个方法 activateConfirmationCodes 在 Retry 中被调用,但是第一个方法 processMessage 根本没有被调用。是否可以指定一种方法在第一次尝试时调用,而另一种方法用于重试? 其次,这种设计的列表变成了单例,这会给多线程带来问题,对吗?此列表是否可以仅与特定消息的 bean 相关联?

4

1 回答 1

2

从大上看,您的问题出在哪里还不清楚。从另一面让我分享一些我的想法,也许我会猜到你的目标。

拥有List<ConfirmationCode>aspayload允许我们随时修改它。所以,让我们假设我们有列表作为10元素。在第一次尝试中,我们处理了其中的 3 个。第四个失败了。我们必须去重试抛出一些适当的异常。但是我们回到重试感知方法的开头,所以使用相同的参数。如果我们从集合中删除那些成功的项目,下一次重试迭代根本不会处理它们。

从一方面您可以实现该区分findCodesFromPayLoad()服务,并activateConfirmationCodes()为最后一个应用重试。

从另一面,您可以在 中将项目标记为已处理activateConfirmationCodes(),因此下一个findCodesFromPayLoad(integrationPayload)不会返回它们。

换句话说,有足够的方法可以在不更改消息的情况下修改集合。

于 2016-01-11T20:22:16.317 回答