配置:
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import java.util.HashMap;
import java.util.Map;
/**
* Author black
* Date 2018/8/31
* Time 15:42
*/
@Configuration
public class RabbitRetryConfig {
@Autowired
private RabbitProperties properties;
@Bean(name = "rabbitRetry")
public RetryTemplate attemptsRetry() {
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setListeners(RetryConfig.getRetryListener());
Map<Class<? extends Throwable>, Boolean> retryableExceptions = new HashMap<>();
//设置重试异常和是否重试
retryableExceptions.put(AmqpException.class, true);
//设置重试次数和要重试的异常
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(5,retryableExceptions);
retryTemplate.setRetryPolicy(retryPolicy);
FixedBackOffPolicy backoffPolicy = new FixedBackOffPolicy();
//设置重试间隔 mills
backoffPolicy.setBackOffPeriod(5000);
retryTemplate.setBackOffPolicy(backoffPolicy);
return retryTemplate;
}
@Bean(name = "mqConnectionFactory")
public CachingConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setUsername(properties.getUsername());
connectionFactory.setPassword(properties.getPassword());
connectionFactory.setHost(properties.getHost());
connectionFactory.setAddresses(properties.getAddresses());
connectionFactory.setPort(properties.getPort());
connectionFactory.setVirtualHost(properties.getVirtualHost());
connectionFactory.setPublisherConfirms(true);
return connectionFactory;
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
template.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
if(!ack){
throw new AmqpException(correlationData.getId() + " ack failed,cause:" + cause);
}
}
});
template.setMandatory(true);
template.setReturnCallback(new RabbitTemplate.ReturnCallback() {
public void returnedMessage(Message message, int replyCode, String replyText,
String exchange, String routingKey) {
throw new AmqpException("Send failed," + "replyCode:" + replyCode + ",replayText:" + replyText + ",exchange:" + exchange + ",routingKey:" + routingKey);
}
});
template.setRetryTemplate(attemptsRetry());
return template;
}
}