我有一个具有JMS Publisher的Spring Boot应用程序。发布者使用了发布者类中运行良好的重试逻辑。但是我想将其更改为Spring Retry Template,并将其放入连接工厂配置中。
我在弄清楚如何调用将RetryTemplate添加到Publisher类时遇到了麻烦。
这是Configuration类:
@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方法。对于如何实现此重试逻辑的任何指导将不胜感激。
最佳答案
除非在方法上使用@EnableRetry
,否则不需要@Retryable
。
目前尚不清楚为什么在这里有while (true)
,或者为什么最初在重试模板的execute方法之外进行调用。
为什么不只是
private void send(final MessageCreator messageCreator) throws JMSException {
publisherRetryTemplate.execute(arg0 -> {
publisherJmsTemplate.send(messageCreator);
return null;
});
}
?
您不清楚“它不执行RetryTemplate方法”是什么意思。
关于java - 为发布者实现Spring Retry Logic困难,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57207202/