当前位置:   article > 正文

SpringCloud学习笔记-异步-RabbitMQ(简单运用)_@rabbitlistener queuestodeclare

@rabbitlistener queuestodeclare
  1. 依赖
    1. <!--RabbitMQ-->
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter-amqp</artifactId>
    5. </dependency>

     

  2. 修改配置文件
    1. spring:
    2. datasource:
    3. driver-class-name: com.mysql.jdbc.Driver
    4. username: root
    5. password: 123456
    6. url: jdbc:mysql://127.0.0.1:3306/SpringCloud?characterEncoding=utf-8&useSSL=false
    7. rabbitmq:
    8. host: localhost
    9. port: 5672
    10. username: guest
    11. password: guest

  3. 创建队列
  4. 接收类
    1. package com.hx.order.message;
    2. import lombok.extern.slf4j.Slf4j;
    3. import org.springframework.amqp.rabbit.annotation.RabbitListener;
    4. import org.springframework.stereotype.Component;
    5. /**
    6. * 接收mq消息
    7. */
    8. @Slf4j
    9. @Component
    10. public class MqReceiver {
    11. @RabbitListener(queues = "myQueue")
    12. public void process(String message) {
    13. log.info("MqReceiver:{}"+message);
    14. }
    15. }

     

  5. 发送类
    1. package com.hx.order;
    2. import org.junit.Test;
    3. import org.springframework.amqp.core.AmqpTemplate;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.stereotype.Component;
    6. import java.util.Date;
    7. /**
    8. * 发送mq消息测试
    9. */
    10. @Component
    11. public class MqSenderTest extends OrderApplicationTests {
    12. @Autowired
    13. private AmqpTemplate amqpTemplate;
    14. @Test
    15. public void send() {
    16. amqpTemplate.convertAndSend("myQueue","你是说什么啊,发送时间:"+new Date());
    17. }
    18. }
    1. package com.hx.order;
    2. import org.junit.Test;
    3. import org.junit.runner.RunWith;
    4. import org.springframework.boot.test.context.SpringBootTest;
    5. import org.springframework.test.context.junit4.SpringRunner;
    6. @RunWith(SpringRunner.class)
    7. @SpringBootTest
    8. public class OrderApplicationTests {
    9. @Test
    10. public void contextLoads() {
    11. }
    12. }

     

  6. 先启动服务,再启动测试方法,测试消息能否发送



    测试成功。。。。。。
  7. 自动创建队列  @RabbitListener(queuesToDeclare = @Queue("myQueue"))
    1. package com.hx.order.message;
    2. import lombok.extern.slf4j.Slf4j;
    3. import org.springframework.amqp.rabbit.annotation.Queue;
    4. import org.springframework.amqp.rabbit.annotation.RabbitListener;
    5. import org.springframework.stereotype.Component;
    6. /**
    7. * 接收mq消息
    8. */
    9. @Slf4j
    10. @Component
    11. public class MqReceiver {
    12. // 自动创建队列
    13. @RabbitListener(queuesToDeclare = @Queue("myQueue"))
    14. public void process(String message) {
    15. log.info("MqReceiver:{}"+message);
    16. }
    17. }

     

  8. 自动创建 Exchange和Queue
    
    
    1. package com.hx.order.message;
    2. import lombok.extern.slf4j.Slf4j;
    3. import org.springframework.amqp.rabbit.annotation.Exchange;
    4. import org.springframework.amqp.rabbit.annotation.Queue;
    5. import org.springframework.amqp.rabbit.annotation.QueueBinding;
    6. import org.springframework.amqp.rabbit.annotation.RabbitListener;
    7. import org.springframework.stereotype.Component;
    8. /**
    9. * 接收mq消息
    10. */
    11. @Slf4j
    12. @Component
    13. public class MqReceiver {
    14. // 自动创建 Exchange和Queue
    15. @RabbitListener(bindings =@QueueBinding(
    16. value = @Queue("myQueue"),
    17. exchange = @Exchange("myExchange")
    18. ))
    19. public void process(String message) {
    20. log.info("MqReceiver:{}"+message);
    21. }
    22. }

    不仅自动创建这俩个,还把他俩绑定在一起。

  9. 测试Exchange 交换器  key  路由   value 队列
    1. package com.hx.order.message;
    2. import lombok.extern.slf4j.Slf4j;
    3. import org.springframework.amqp.rabbit.annotation.Exchange;
    4. import org.springframework.amqp.rabbit.annotation.Queue;
    5. import org.springframework.amqp.rabbit.annotation.QueueBinding;
    6. import org.springframework.amqp.rabbit.annotation.RabbitListener;
    7. import org.springframework.stereotype.Component;
    8. /**
    9. * 接收mq消息
    10. */
    11. @Slf4j
    12. @Component
    13. public class MqReceiver {
    14. // 自动创建 Exchange和Queue
    15. @RabbitListener(bindings =@QueueBinding(
    16. value = @Queue("myQueue"),
    17. exchange = @Exchange("myExchange")
    18. ))
    19. public void process(String message) {
    20. log.info("MqReceiver:{}"+message);
    21. }
    22. /**书本
    23. * exchange 组(交换器) key 路由(钥匙) value 队列
    24. * @param message
    25. */
    26. @RabbitListener(bindings =@QueueBinding(
    27. exchange = @Exchange("myOrder"),
    28. key = "book",
    29. value = @Queue("bookOrder")
    30. ))
    31. public void processBook(String message) {
    32. log.info("bookMqReceiver:{}"+message);
    33. }
    34. /**
    35. *
    36. * @param message
    37. */
    38. @RabbitListener(bindings =@QueueBinding(
    39. exchange = @Exchange("myOrder"),
    40. key = "fruit",
    41. value = @Queue("fruitOrder")
    42. ))
    43. public void processFruit(String message) {
    44. log.info("fruitMqReceiver:{}"+message);
    45. }
    46. }
    1. package com.hx.order;
    2. import org.junit.Test;
    3. import org.springframework.amqp.core.AmqpTemplate;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.stereotype.Component;
    6. import java.util.Date;
    7. /**
    8. * 发送mq消息测试
    9. */
    10. @Component
    11. public class MqSenderTest extends OrderApplicationTests {
    12. @Autowired
    13. private AmqpTemplate amqpTemplate;
    14. @Test
    15. public void send() {
    16. amqpTemplate.convertAndSend("myQueue","你是说什么啊,发送时间:"+new Date());
    17. }
    18. @Test
    19. public void sendOrder() {
    20. // 第一的参数是交换器 第二个是路由, 第三个是消息
    21. amqpTemplate.convertAndSend("myOrder","fruit","你是说什么啊,发送时间:"+new Date());
    22. }
    23. }

     

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

闽ICP备14008679号