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 结果

    
    
  • 相关阅读:
    C# -- 使用缓冲区进行文件下载操作
    C# -- 使用ODBC连接数据库
    C# -- Quartz.Net入门案例
    C# -- LinkedList的使用
    ASP.NET -- 获取浏览器信息
    Windows -- 从注册表删除IE浏览器加载项
    C# -- FTP上传下载
    C# -- 使用Ping检查网络是否正常
    WinForm -- 为TextBox文本框添加鼠标右键菜单
    C# -- 使用Parallel并行执行任务
  • 原文地址:https://www.cnblogs.com/xd03122049/p/6026765.html
Copyright © 2011-2022 走看看