springboot实现rabbitmq的队列初始化和绑定
配置文件,在rabbit中自动建立exchange,queue和绑定它们的关系
- 代码里初始化exchange
- 代码里初始化queue
- 代码里绑定exchange,queue和routekey
- 配置文件,直接声明vhost
代码里初始化exchange
/**
*rabbitMq里初始化exchange.
*
*@return
*/
@Bean
publicTopicExchangecrmExchange(){
returnnewTopicExchange(EXCHANGE);
}
代码里初始化queue
/**
*rabbitMq里初始化队列crm.hello.
*
*@return
*/
@Bean
publicQueuehelloQueue(){
returnnewQueue(HELLO);
}
代码里绑定exchange,queue和routekey
/**
*绑定exchange&queue&routekey.
*
*@paramqueueMessage队列
*@paramexchange交换机
*@paramroutekey路由
*@return
*/
publicBindingbindingExchange(QueuequeueMessage,TopicExchangeexchange,Stringroutekey){
returnBindingBuilder.bind(queueMessage).to(exchange).with(routekey);
}
配置文件
spring: rabbitmq: host:localhost port:5672 username:guest password:guest virtual-host:lind
完整代码
packagecom.lind.microservice.productCenter.mq;
importorg.springframework.amqp.core.Binding;
importorg.springframework.amqp.core.BindingBuilder;
importorg.springframework.amqp.core.Queue;
importorg.springframework.amqp.core.TopicExchange;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
/**
*amqp配置.
*/
@Configuration
publicclassAmqpConfig{
/**
*交换机.
*/
publicfinalstaticStringEXCHANGE="crm";
/**
*hello队列.
*/
publicfinalstaticStringHELLO="crm.hello";
/**
*建立订单队列.
*/
publicfinalstaticStringLIND_GENERATE_ORDER="crm.generate.order";
/**
*绑定exchange&queue&routekey.
*
*@paramqueueMessage队列
*@paramexchange交换机
*@paramroutekey路由
*@return
*/
publicBindingbindingExchange(QueuequeueMessage,TopicExchangeexchange,Stringroutekey){
returnBindingBuilder.bind(queueMessage).to(exchange).with(routekey);
}
/**
*rabbitMq里初始化exchange.
*
*@return
*/
@Bean
publicTopicExchangecrmExchange(){
returnnewTopicExchange(EXCHANGE);
}
/**
*rabbitMq里初始化队列crm.hello.
*
*@return
*/
@Bean
publicQueuehelloQueue(){
returnnewQueue(HELLO);
}
/**
*rabbitMq里初始化队列crm.generate.order.
*
*@return
*/
@Bean
publicQueueorderQueue(){
returnnewQueue(LIND_GENERATE_ORDER);
}
}
队列发布者
packagecom.lind.microservice.productCenter.mq;
importjava.util.Date;
importorg.springframework.amqp.core.AmqpTemplate;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.context.annotation.Configuration;
@Configuration
publicclassHelloPublisher{
@Autowired
AmqpTemplaterabbitTemplate;
@Autowired
AmqpConfigamqpConfig;
publicvoidhello(){
Stringcontext="hello"+newDate();
System.out.println("HelloPublisher:"+context);
amqpConfig.bindingExchange(
amqpConfig.helloQueue(),
amqpConfig.crmExchange(),
"crm.hello.#"
);
this.rabbitTemplate.convertAndSend(AmqpConfig.EXCHANGE,AmqpConfig.HELLO,context);
}
}
队列订阅者
packagecom.lind.microservice.productCenter.mq;
importorg.springframework.amqp.rabbit.annotation.RabbitHandler;
importorg.springframework.amqp.rabbit.annotation.RabbitListener;
importorg.springframework.stereotype.Component;
@Component
@RabbitListener(queues=AmqpConfig.HELLO)
publicclassHelloSubscriber{
@RabbitHandler
publicvoidprocess(Stringhello){
System.out.println("HelloSubscriber:"+hello);
}
}
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。