zoukankan      html  css  js  c++  java
  • RabbitMQ和SpringBoot的简单整合列子

    一 思路总结

     1 主要用spring-boot-starter-amqp来整合RabbitMQ和SpringBoot

     2 使用spring-boot-starter-test来进行单元测试

     3编写配置文件application.yml

    spring:
    application:
    name: rabbitmq-hello
    rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
    4 编写主类Application.class
    5 编写Sender并自动扫面,以备自动注入
    @Component
    public class Sender {
    @Autowired
    private AmqpTemplate amqpTemplate;

    public void send() throws Exception {
    String context = "hello" + new Date();
    System.out.println("Sender:"+context);
    this.amqpTemplate.convertAndSend("hello",context);
    }
    }
    6 编写Reveiver并自动扫面同时监听队列hello,并用RabbitHandler来处理请求。
    @Component
    @RabbitListener(queues = "hello")
    public class Receiver {

    @RabbitHandler
    public void process(String hello){
    System.out.println("Receiver:"+hello);
    }
    }
    7 编写刚刚用到的hello队列的配置类
    @Configuration
    public class RabbitConfig {
    @Bean
    public Queue helloQueue() {
    return new Queue("hello");
    }
    }
    8 编写单元测试类Test,调用Sender的方法发送message,这样Receiver就能自动监听并在主类哪里输出了
    @SpringBootTest(classes = Application.class)
    @RunWith(SpringJUnit4ClassRunner.class)
    public class Test {

    @Autowired
    private Sender sender;

    @org.junit.Test
    public void hello() throws Exception {
    sender.send();
    }
    }
    9 结果

    
    
  • 相关阅读:
    37.1 net-- udp传输
    37 net 网络编程
    review
    java day02 记录
    36.2 线程生命周期
    36.1 线程锁
    36 Thread 多线程
    35 编码 ASCII Unicode UTF-8 ,字符串的编码、io流的编码
    34.6 Properties(k,v存储) 和io流结合使用
    今日学习总结
  • 原文地址:https://www.cnblogs.com/xd03122049/p/6026765.html
Copyright © 2011-2022 走看看