当前位置:   article > 正文

SpringBoot整合RabbitMQ,自动创建交换器和队列代码实现_rabbitmq自动创建队列

rabbitmq自动创建队列

1.依赖:

  1. <!-- spring boot 整合rabbit MQ -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-amqp</artifactId>
  5. </dependency>

2. yml配置

  1. amqp:
  2. queue: DIRECT_QUEUE
  3. exchange: DIRECT_EXCHANGE
  4. routingkey: DIRECT_QUEUE_KEY

3. 配置类

  1. package com.xx.mq.config;
  2. import org.springframework.amqp.core.Binding;
  3. import org.springframework.amqp.core.BindingBuilder;
  4. import org.springframework.amqp.core.DirectExchange;
  5. import org.springframework.amqp.core.Queue;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. @Configuration
  10. public class ApiRabbitMqConfig {
  11. @Value("${amqp.queue}")
  12. private String queue;
  13. @Value("${amqp.exchange}")
  14. private String exchange;
  15. @Value("${amqp.routingkey}")
  16. private String routingKey;
  17. /**
  18. * 创建队列
  19. *
  20. * @return
  21. */
  22. @Bean
  23. public Queue getApiQueue() {
  24. /*
  25. * name(必填): 创建消息队列 队列名称:CHANNEL_API_QUEUE
  26. * durable: 是否持久化,默认false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
  27. * exclusive: 默认false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
  28. * autoDelete: 是否自动删除,默认false,当没有生产者或者消费者使用此队列,该队列会自动删除。
  29. */
  30. return new Queue(queue, true);
  31. }
  32. /**
  33. * 创建直流交换器
  34. *
  35. * @return
  36. */
  37. @Bean
  38. public DirectExchange getApiDirectExchange() {
  39. /*
  40. * name(必填): 交换器名称
  41. * durable: 是否持久化,默认true
  42. * autoDelete: 是否自动删除,默认false
  43. */
  44. return new DirectExchange(exchange, true, false);
  45. }
  46. /**
  47. * 交换器绑定队列
  48. *
  49. * @return
  50. */
  51. @Bean
  52. public Binding bindingQueueToExchange() {
  53. /*
  54. * 将队列绑定到交换器上, 并设置路由键
  55. */
  56. return BindingBuilder.bind(getApiQueue()).to(getApiDirectExchange()).with(routingKey);
  57. }
  58. }

4. 生产者代码Producer

  1. /**
  2. * 消息生产者
  3. */
  4. @Component
  5. @Slf4j
  6. public class Producer {
  7. @Value("${amqp.exchange}")
  8. private String exchange;
  9. @Value("${amqp.routingkey}")
  10. private String routingKey;
  11. @Autowired
  12. private RabbitTemplate rabbitTemplate;
  13. public void sendMessage(Message message) {
  14. log.info("MQ异步推送exchange:{},routingKey:{},message:{}", exchange, routingKey, message);
  15. rabbitTemplate.convertAndSend(exchange, routingKey, message);
  16. }
  17. }

5.消费者代码

  1. @Component
  2. @Slf4j
  3. public class APIChannelListener {
  4. @Autowired
  5. private MessageConverter messageConverter;
  6. @Autowired
  7. private APIChannelService apiChannelService;
  8. /**
  9. * 监听API服务进行投保单保存并转核保
  10. * @param message 消息
  11. */
  12. @RabbitListener(queues = "${amqp.queue}")
  13. public void proposalSaveAndSubmit(Message message) {
  14. try {
  15. ProposalVo proposalVo = (ProposalVo) messageConverter.fromMessage(message);
  16. log.info("消费消息: {}", JSONObject.toJSONString(proposalVo));
  17. // 业务
  18. ResBean resBean = apiChannelService.addUser(User, true);
  19. log.info("响应结果: {}", JSONObject.toJSONString(resBean));
  20. } catch (Exception e) {
  21. log.info("错误信息:{}", e.getMessage());
  22. }
  23. }
  24. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/639670
推荐阅读
相关标签
  

闽ICP备14008679号