zoukankan      html  css  js  c++  java
  • SpringBoot+RabbitMQ学习笔记(一)

    一丶添加依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>

    配置文件:application.properties

    server.port=8883
    
    spring.application.name=hello-world
    spring.rabbitmq.host=localhost
    spring.rabbitmq.port=5672
    spring.rabbitmq.username=guest
    spring.rabbitmq.password=guest

    二丶创建消息队列

    package com.example.ampq;
    
    import org.springframework.amqp.core.Queue;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * Author:aijiaxiang
     * Date:2020/4/26
     * Description:
     */
    @Configuration
    public class QueueConfig {
        /**
         * 创建队列
         * @return
         */
        @Bean
        public Queue createQueue(){
            return new Queue("hello-queue");
        }
    }

    三丶创建生产者

    package com.example.ampq;
    
    import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    /**
     * Author:aijiaxiang
     * Date:2020/4/26
     * Description:发送消息
     */
    @Component
    public class Sender {
    
        @Autowired
        private AmqpTemplate amqpTemplate;
    
        /**
         * 发送消息的方法
         * @param msg
         */
        public void send(String msg){
            //向消息队列发送消息
            //参数1:队列名称
            //参数2:消息
            this.amqpTemplate.convertAndSend("hello-queue",msg);
     
        }
    }

    四丶创建消费者

    package com.example.ampq;
    
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    /**
     * Author:aijiaxiang
     * Date:2020/4/26
     * Description:消息接收者
     */
    @Component
    public class Receiver {
    
        /**
         * 接收消息的方法,采用消息队列监听机制
         *
         * 使用 @RabbitListener 注解标记方法,当监听到队列 hello-queue 中有消息时则会进行接收并处理
         *
         * @param msg
         */
        @RabbitListener(queues = "hello-queue")
        public void process(String msg){
            System.out.println("receiver:"+msg);
        }
    }

    五丶测试一波

    package com.example.amqp;
    
    import com.example.ampq.Sender;
    import com.example.helloworld.HelloworldApplication;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    /**
     * Author:aijiaxiang
     * Date:2020/4/26
     * Description:
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = HelloworldApplication.class)
    public class QueueTest {
        @Autowired
        private Sender sender;
    
        /**
         * 测试消息队列
         */
        @Test
        public void test1() throws InterruptedException {
            while (true){
                Thread.sleep(1000);
                sender.send("hello");
            }
    
        }
    
      
    }
  • 相关阅读:
    将SqlServer的数据导出到Excel/csv中的各种方法 .
    SqlServer: 单用户模式下查杀相关进程实现单/多用户转换 .
    SQL Server游标的使用【转】
    由几行代码浅析C#的方法参数传递
    脑力风暴之小毛驴历险记(1)好多胡萝卜(下)
    关于sql_mode=only_full_group_by问题的解决方法
    如何同一个controller下添加新页面
    UNIAPP全局变量的实现方法
    Ztree点击节点选中复选框的相关操作
    一沙框架(yishaadmin) 前端引入VUE的实现方法
  • 原文地址:https://www.cnblogs.com/aijiaxiang/p/12795920.html
Copyright © 2011-2022 走看看