RabbitMq--與SpringBoot整合
1 Spring Boot對RabbitMQ的使用做了自動配置,更加的簡化了我們的使用。
新增依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
在application.properties檔案中配置RabbitMQ的連線資訊:
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=tangtang
spring.rabbitmq.password=123123
spring.rabbitmq.virtual-host=/tang
會自動建立連線org.springframework.amqp.rabbit.connection.ConnectionFactory物件。
注入到想要的位置。
/**
* RabbitMq配置中心,由SpringBoot提供的相關包自動整合
*
* @author Tang 2017年11月19日
*/
@Configuration
@PropertySource(value = "classpath:rabbitmq.properties")
public class RabbitMqConfig {
private final String QUEUE_NAME = "RABBITMQ-TEXT-QUEUE";
/**
* 在全域性配置中按要求配置,會自動將連線注入到這裡
*/
@Autowired
private ConnectionFactory connectionFactory;
// 管理
@Bean
public RabbitAdmin rabbitAdmin() {
return new RabbitAdmin(connectionFactory);
}
// 宣告佇列
@Bean
public Queue rabbitTextQueue() {
// 預設就是自動宣告的
return new Queue(QUEUE_NAME, true);
}
/**
* 配置監聽連線
*/
@Bean(name = "rabbitListenerContainerFactory")
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
ConnectionFactory connectionFactory, RabbitProperties config) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
RabbitProperties.Listener listenerConfig = config.getListener();
factory.setAutoStartup(listenerConfig.isAutoStartup());
if (listenerConfig.getAcknowledgeMode() != null) {
factory.setAcknowledgeMode(listenerConfig.getAcknowledgeMode());
}
if (listenerConfig.getConcurrency() != null) {
factory.setConcurrentConsumers(listenerConfig.getConcurrency());
}
if (listenerConfig.getMaxConcurrency() != null) {
factory.setMaxConcurrentConsumers(listenerConfig
.getMaxConcurrency());
}
if (listenerConfig.getPrefetch() != null) {
factory.setPrefetchCount(listenerConfig.getPrefetch());
}
if (listenerConfig.getTransactionSize() != null) {
factory.setTxSize(listenerConfig.getTransactionSize());
}
return factory;
}
/**
* 具體監聽佇列及使用配置
*
* @param message
* @throws Exception
*/
@RabbitListener(queues = QUEUE_NAME, containerFactory = "rabbitListenerContainerFactory")
public void handleMessage(String message) throws Exception {
// 具體處理
}
}
相關文章
- ElasticSearch與SpringBoot的整合與JPA方法的使用ElasticsearchSpring Boot
- springboot+shiro 整合與基本應用Spring Boot
- RabbitMQ簡介以及與SpringBoot整合示例MQSpring Boot
- SpringBoot與Shiro整合-許可權管理Spring Boot
- flowable6.4 與springboot2 整合Spring Boot
- SpringBoot(19)---SpringBoot整合ApolloSpring Boot
- SpringBoot(17)---SpringBoot整合RocketMQSpring BootMQ
- SpringBoot(十六)_springboot整合JasperReSpring Boot
- SpringBoot整合系列-整合JPASpring Boot
- SpringBoot 整合 rabbitmqSpring BootMQ
- SpringBoot 整合 apolloSpring Boot
- SpringBoot 整合 elkSpring Boot
- SpringBoot 整合 elasticsearchSpring BootElasticsearch
- SpringBoot整合elasticsearchSpring BootElasticsearch
- RocketMQ整合SpringBootMQSpring Boot
- springboot整合redis?Spring BootRedis
- SpringBoot整合MinIOSpring Boot
- Springboot整合pagehelperSpring Boot
- ElasticSearch 整合 SpringBootElasticsearchSpring Boot
- springboot 整合LogBackSpring Boot
- SpringBoot整合ESSpring Boot
- springBoot 整合 mybatisSpring BootMyBatis
- SpringBoot整合NacosSpring Boot
- SpringBoot 整合 RedisSpring BootRedis
- SpringBoot整合DubboSpring Boot
- springboot 整合jeagerSpring Boot
- 【SpringBoot】整合RedisSpring BootRedis
- Springboot整合MybatisSpring BootMyBatis
- SpringBoot整合SwaggerSpring BootSwagger
- SpringBoot 整合 dockerSpring BootDocker
- flowable 整合 springbootSpring Boot
- Springboot整合RabbitMQSpring BootMQ
- springBoot整合flowableSpring Boot
- Springboot整合MyabitsSpring Boot
- SpringBoot整合RedisSpring BootRedis
- springBoot整合thymeleafSpring Boot
- springboot整合druidSpring BootUI
- SpringBoot整合MQTTSpring BootMQQT
- springboot+mybatis+mybaits plus 整合與基本應用Spring BootMyBatisAI