spring retry-3 (rabbitTemplate结合retryTemplate使用)

配置:

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;
   }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值