难以为发布者实现 Spring 重试逻辑



我有一个带有JMS发布服务器的Spring Boot应用程序。 发布者使用的重试逻辑在发布者类中工作正常。 但我想将其更改为 Spring 重试模板并将其放入连接工厂配置中。

我在弄清楚如何调用将 RetryTemplate 添加到发布器类时遇到问题。

这是配置类:

@Configuration
@EnableRetry
public class PurchasedTransServicePublisherConfig {
@Value("${java.naming.factory.initial.publisher}")
private String context;
@Value("${java.naming.provider.url.publisher}")
private String providerURL;
@Value("${fedex.jms.LDAP.entryName.publisher}")
private String ldapEntryName;
private @Value("${jms.username.publisher:#{null}}") String jmsUserName;
private @Value("${jms.password.publisher:#{null}}") String jmsPassword;
@Value("${jms.destinationname.publisher}")
private String destinationName;
private static final Logger LOGGER = LoggerFactory.getLogger(PurchasedTransServicePublisherConfig.class);
@Autowired(required = false)
FxgCipherInitializer jmsParams;
@Bean
public JmsTemplate publisherJmsTemplate(final ConnectionFactory publisherConnectionFactory) {
final JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory(publisherConnectionFactory);
jmsTemplate.setPubSubDomain(true);
jmsTemplate.setDefaultDestinationName(destinationName);
jmsTemplate.setSessionTransacted(true);
return jmsTemplate;
}
@Bean
public ConnectionFactory publisherConnectionFactory(final JndiTemplate publisherJndiTemplate) throws NamingException {
final ConnectionFactory connectionFactory = (ConnectionFactory) publisherJndiTemplate.getContext().lookup(ldapEntryName);
final UserCredentialsConnectionFactoryAdapter ucf = new UserCredentialsConnectionFactoryAdapter();
ucf.setUsername(((null != jmsParams) ? jmsParams.getUsername() : jmsUserName));
ucf.setPassword((null != jmsParams) ? jmsParams.getPassword() : jmsPassword);
ucf.setTargetConnectionFactory(connectionFactory);
return ucf;
}
@Bean
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(2000l);
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(2);
retryTemplate.setRetryPolicy(retryPolicy);
return retryTemplate;
}

@Bean
public JndiTemplate publisherJndiTemplate() {
final JndiTemplate jndiTemplate = new JndiTemplate();
final Properties jndiProps = new Properties();
jndiProps.setProperty(Context.INITIAL_CONTEXT_FACTORY, context);
jndiProps.setProperty(Context.PROVIDER_URL, providerURL);
jndiTemplate.setEnvironment(jndiProps);
return jndiTemplate;
}  
}

工作配置和 RetryTemplate 配置之间的唯一更改是添加了注释@EnableRetry和方法retryTemplate

发布者类最初使用以下逻辑成功重试发送消息:

private void send(final MessageCreator messageCreator) throws JMSException {
int sendAttempts = 0;
while (true) {
try {
jmsTemplate.send(messageCreator);
LOGGER.info("Message Successfully Published");
break;
}  catch (RuntimeException e) {
LOGGER.error("Caught Runtime Exception: {}", e.getMessage());
sendAttempts++;
handleJmsExceptionRetry(e, sendAttempts);
}
}
}

我尝试像这样实现重试模板:

private void send(final MessageCreator messageCreator) throws JMSException {
while (true) {
try {
publisherJmsTemplate.send(messageCreator);
LOGGER.info("Message Successfully Published");
break;
}  catch (RuntimeException e) {
LOGGER.error("Caught Runtime Exception: {}", e.getMessage()); 
publisherRetryTemplate.execute(arg0 -> {
publisherJmsTemplate.send(messageCreator);
return null;
});
}
}
}

我创建的用于对此进行单元测试的测试方法如下:

@Test
public void testPublishTmsTrip_WhenPublishFailsMultipleTimes() {
Mockito.doThrow(RuntimeException.class).when(mockJmsTemplate).send(mockMessageCreator);
boolean testBoolean = tmsTripPublisher.publishTmsTripMessageEvent("TEST message");
assertFalse(testBoolean);
}

问题是当它到达publisherRetryTemplate.execute...时,它不执行RetryTemplate方法。 有关如何实现此重试逻辑的任何指导将不胜感激。

除非在方法上使用@Retryable,否则不需要@EnableRetry

目前尚不清楚为什么您在那里while (true),或者为什么您最初在重试模板的执行方法之外调用。

为什么不只是

private void send(final MessageCreator messageCreator) throws JMSException {
publisherRetryTemplate.execute(arg0 -> {
publisherJmsTemplate.send(messageCreator);
return null;
});
}

目前尚不清楚"它不执行 RetryTemplate 方法"是什么意思。

看起来您将声明式方法与编程方法混合在一起。 只要不使用用@Retryable@Recover注释的方法,配置类中就不需要@EnableRetry

如果对RetryTemplateBean 使用编程重试,则无需对 catch 块中的异常调用RetryTemplate.execute()方法。 在定义 BeanRetryTemplate时,只需指定异常,哪个实例将启动execute()回调中指定的新操作。

例如,如果要在运行时异常上重试该操作,但不想对某些自定义异常执行该操作,则必须在策略映射中指定它:

new ExceptionClassifierRetryPolicy().
setPolicyMap(ImmutableMap.<Class<? extends Throwable>, RetryPolicy> builder()
.put(CustomException.class, new NeverRetryPolicy())
.put(RuntimeException.class, new SimpleRetryPolicy(MAX_RETRY_ATTEMPTS))
.build());

您不需要有一个 try-catch 块来重试请求。重试模板本身按配置重试请求(2 次重试,超时时间为 2000 毫秒)。

只需使用

private boolean send(final MessageCreator messageCreator) {       
try {
publisherRetryTemplate.execute(arg0 -> {
publisherJmsTemplate.send(messageCreator);
LOGGER.info("Message Successfully Published");
return null;
});
} catch (Exception e) {
LOGGER.error("Caught Exception: {}", e);
// Error handling logic here
return false;
}
}

最新更新