欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring AMQP(集成了Rabbitmq)---代码版

程序员文章站 2022-07-15 08:12:36
...

Spring AMQP(集成了Rabbitmq)—代码版

项目结构

Spring AMQP(集成了Rabbitmq)---代码版

配置文件

  1. application.yml

    spring:
      rabbitmq:
        host: 192.168.79.128  # 安装rabbitmq的ip
        username: leyou # rabbitmq的账号
        password: 123456 # rabbitmq的密码
        virtual-host: /leyou # 虚拟主机
    

启动类

  1. Application

    package cn.xiaoge.rabbitmq.spring;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * @author: Xiao.Zhang
     * @create: 2018-05-23 18:07
     **/
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

监听者

  1. Listener

    package cn.xiaoge.rabbitmq.spring;
    
    import org.springframework.amqp.core.ExchangeTypes;
    import org.springframework.amqp.rabbit.annotation.Exchange;
    import org.springframework.amqp.rabbit.annotation.Queue;
    import org.springframework.amqp.rabbit.annotation.QueueBinding;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    /**
     * @author: Xiao.Zhang
     * @create: 2018-05-23 18:04
     **/
    @Component
    public class Listener {
        /*
             @RabbitListener(queues = "simple_queue") rabbitmq有该simple_queue队列,
             没有的话就会报错, 推荐使用下面哪种, 没有自动创建
          */
        @RabbitListener(bindings = @QueueBinding(
                /*
                     value = "spring.test.queue" 队列名称
                     durable = "true" 持久化
                 */
                value = @Queue(value = "spring.test.queue", durable = "true"), // 自动生成队列
                /*
                    value = "spring.test.exchange 交换机名称
                    type = ExchangeTypes.TOPIC 类型是topic
                 */
                exchange = @Exchange(  // 自动生成交换机
                        value = "spring.test.exchange",
                        ignoreDeclarationExceptions = "true",
                        type = ExchangeTypes.TOPIC
                ),
                /*
                    key = {"#.#"} 绑定一切, 随便发都能接收
                    这里的key 是routingKey #.#是    任意字符.任意字符  所以生产端的routingKey可以写成任意字符.任意字符
                 */
                key = {"#.#"}// 把他们两个绑定起来
                ))
        public void listen(String msg){
            System.out.println("接收到消息:" + msg);
        }
    }
    

测试

  1. MqDemo

    package cn.xiaoge.rabbitmq.spring;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    /**
     * @author: Xiao.Zhang
     * @create: 2018-05-23 18:08
     **/
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = Application.class)
    public class MqDemo {
    
        @Autowired
        private AmqpTemplate amqpTemplate;
    
        @Test
        public void testSend() throws InterruptedException {
            String msg = "hello, Spring boot amqp";
            // this.amqpTemplate.convertAndSend("spring.test.exchange","a.b", msg);
            this.amqpTemplate.convertAndSend("spring.test.exchange", "spring.test.queue", msg);
            // 等待10秒后再结束
            Thread.sleep(10000);
        }
    }
    
相关标签: Rabbitmq rabbitmq