标签:convert mqc tom amp autowired dmi bean enabled 文件配置
1、下面讲解RabbitMQ 配置:代码如下:
@Configuration
public class RabbitMQConfig implements RabbitListenerConfigurer {
private static final Logger logger = LoggerFactory.getLogger(RabbitMQConfig.class);
// 资源owner账户 ID 信息
@Value("${business.rabbitmq.resourceOwnerId:0}")
private String RESOURCE_OWNER_ID;
@Autowired
private RabbitProperties rabbitProperties;
@Value("${rabbitmq.example}")
private Integer rabbitMQInstance;
@Bean
public ConnectionFactory connectionFactory() {
if (rabbitMQInstance == 1) {
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = new com.rabbitmq.client.ConnectionFactory();
rabbitConnectionFactory.setHost(rabbitProperties.getHost());
rabbitConnectionFactory.setPort(rabbitProperties.getPort());
rabbitConnectionFactory.setVirtualHost(rabbitProperties.getVirtualHost());
AliyunCredentialsProvider credentialsProvider =
new AliyunCredentialsProvider(rabbitProperties.getUsername(), rabbitProperties.getPassword(),
RESOURCE_OWNER_ID);
rabbitConnectionFactory.setCredentialsProvider(credentialsProvider);
rabbitConnectionFactory.setAutomaticRecoveryEnabled(true);
rabbitConnectionFactory.setNetworkRecoveryInterval(5000);
ConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitConnectionFactory);
((CachingConnectionFactory) connectionFactory).setPublisherConfirms(rabbitProperties.isPublisherConfirms());
((CachingConnectionFactory) connectionFactory).setPublisherReturns(rabbitProperties.isPublisherReturns());
logger.info("Create AMQP connection Factory.");
return connectionFactory;
}
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitProperties.getHost(),rabbitProperties.getPort());
connectionFactory.setUsername(rabbitProperties.getUsername());
connectionFactory.setPassword(rabbitProperties.getPassword());
connectionFactory.setPublisherConfirms(rabbitProperties.isPublisherConfirms());
connectionFactory.setPublisherReturns(rabbitProperties.isPublisherReturns());
return connectionFactory;
}
@Bean
public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
return new Jackson2JsonMessageConverter();
}
@Override
public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
}
@Bean
MessageHandlerMethodFactory messageHandlerMethodFactory() {
DefaultMessageHandlerMethodFactory messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory();
messageHandlerMethodFactory.setMessageConverter(consumerJackson2MessageConverter());
return messageHandlerMethodFactory;
}
@Bean
public MappingJackson2MessageConverter consumerJackson2MessageConverter() {
return new MappingJackson2MessageConverter();
}
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
factory.setConnectionFactory(connectionFactory);
return factory;
}
}
2、yml 文件配置:
截图如下:
代码配置如下:
spring:
rabbitmq:
host: 127.0.1
username: admin
password: 21323
virtual-host: test
listener:
simple:
retry:
max-attempts: 5 #最大重试次数
enabled: true
initial-interval: 20s
default-requeue-rejected: false
rabbitmq:
example: 1 # 1:阿里云AMPQ,0:rabbitMQ
3、配置生产者
@Configuration
public class RabbitConfiguration {
@Bean(name = "mq.test.01")
FanoutExchange exchange() {
return new FanoutExchange("mq.test.01", false, true);
}
}
标签:convert mqc tom amp autowired dmi bean enabled 文件配置
原文地址:https://blog.51cto.com/7218743/2544676