有以下带有 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 相关联?