SpringBoot RabbitMQ Retry(重发)配置
1.RabbitMQ application.properties
#database config
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
#redis config
spring.redis.database=2
spring.redis.hostname=192.168.1.197
spring.redis.password=walrus
spring.redis.port=6379
#redis pool config
spring.redis.pool.maxActive=200
spring.redis.pool.maxWait=12000
spring.redis.pool.maxIdle=50
spring.redis.pool.minIdle=10
#rabbit mq config
spring.application.name=springboot-rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=test
spring.rabbitmq.password=test
# open sender confirm
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.virtual-host=/test
2.RabbitMQConfig.java
package org.ssgroup.secondkill.config.mq;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.config.RetryInterceptorBuilder;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.ConnectionFactory;
@Component
public class RabbitMQConfig {
private static int SESSION_CACHE_SIZE = 25;
private static int CORE_POOL_SIZE = 64;
private static int MAX_POOL_SIZE = 1024;
private static int QUEUE_CAPACITY = 256;
@Autowired
private RabbitMQProperties rabbitMQProperties;
@Bean
public ConnectionFactory connectionFactory() {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost(rabbitMQProperties.getHost());
connectionFactory.setPort(rabbitMQProperties.getPort());
connectionFactory.setUsername(rabbitMQProperties.getUsername());
connectionFactory.setPassword(rabbitMQProperties.getPassword());
connectionFactory.setVirtualHost(rabbitMQProperties.getVirtualHost());
connectionFactory.setConnectionTimeout(1000);
return connectionFactory;
}
@Bean
public CachingConnectionFactory rabbitConnectionFactory() {
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(connectionFactory());
cachingConnectionFactory.setChannelCacheSize(SESSION_CACHE_SIZE);
cachingConnectionFactory.setExecutor(taskExecutor());
return cachingConnectionFactory;
}
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory containerFactory = new SimpleRabbitListenerContainerFactory();
containerFactory.setConnectionFactory(rabbitConnectionFactory());
containerFactory.setConcurrentConsumers(1);
containerFactory.setMaxConcurrentConsumers(20);
containerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
//containerFactory.setMessageConverter(new Jackson2JsonMessageConverter());
containerFactory.setTaskExecutor(taskExecutor());
containerFactory.setChannelTransacted(true);
containerFactory.setAdviceChain(
RetryInterceptorBuilder
.stateless()
.recoverer(new RejectAndDontRequeueRecoverer())
.retryOperations(retryTemplate())
.build()
);
return containerFactory;
}
@Bean
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setBackOffPolicy(backOffPolicy());
retryTemplate.setRetryPolicy(retryPolicy());
return retryTemplate;
}
@Bean
public ExponentialBackOffPolicy backOffPolicy() {
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
backOffPolicy.setInitialInterval(1000);
backOffPolicy.setMaxInterval(10000);
return backOffPolicy;
}
@Bean
public SimpleRetryPolicy retryPolicy() {
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(3);
return retryPolicy;
}
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.initialize();
executor.setCorePoolSize(CORE_POOL_SIZE);
executor.setMaxPoolSize(MAX_POOL_SIZE);
executor.setQueueCapacity(QUEUE_CAPACITY);
return executor;
}
}
3.OrderReceiver.java
@Component
public class OrderReceiver {
private static Logger logger = LoggerFactory.getLogger(OrderReceiver.class);
@Autowired
private OrderMapper orderMapper;
@RabbitListener(queues= {RabbitMQQueueConfig.ORDER_QUEUE_NAME})
public void orderReceiver(Orders order, Message message, Channel channel) throws Exception {
logger.info("订单消息入库... | id : {}",order.getId());
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
orderMapper.save(order);
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
}
}