• Rabbitmq 与springboot 结合


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

    2.yml

    spring:
      application:
        name: spring-boot-rabbitmq-sender
      rabbitmq:
        host: 127.0.0.1
        port: 5672
        username: mm
        password: 123
        virtual-host: /test

    3. 

    AmqpAdmin 创建组件:如交换机,队列,绑定队则
    RabbitTemplate:发送,接受消息
     
    package com.aynu.bootamqp;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.amqp.core.*;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class BootamqpApplicationTests {
    
        @Autowired
        RabbitTemplate rabbitTemplate;
        @Autowired
        AmqpAdmin amqpAdmin;
    
        @Test
        public void create(){
            Exchange exchange = new DirectExchange("test22222",true,false);
            amqpAdmin.declareExchange(exchange);
            System.out.println("创建成功");
    
            Queue queue = new Queue("999",true);
            amqpAdmin.declareQueue(queue);
    
            Binding binding = new Binding("999", Binding.DestinationType.QUEUE,"test22222","goods.add",null);
            amqpAdmin.declareBinding(binding);
        }
    
        @Test
        public void contextLoads(){
            String msg ="hello msg";
            rabbitTemplate.convertAndSend("test22222","goods.add",msg);
        }
        @Test
        public void receive(){
            Object o = rabbitTemplate.receiveAndConvert("999");
            System.out.println(o);
        }
    
    }

    4. 开启队列监控

       @Test
        @RabbitListener(queues = "999")
        public void receive1(){
            System.out.println();
        }

    @EnableRabbit
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class BootamqpApplicationTests {}
     
  • 相关阅读:
    2019南昌网络赛-I(单调栈+线段树)
    poj3250(单调栈模板题)
    poj2528(线段树+离散化)
    poj2828(线段树查找序列第k小的值)
    Seikimatsu Occult Tonneru(网络流,状态数(建不建边)不多时,可考虑直接进行枚举
    A. Coffee Break(思维题,类似于邻接表的head数组用法)
    E. Paint the Tree(树形dp)
    cdq分治学习
    2018SEERC Points and Rectangles (CDQ分治)
    SEERC 2018 Inversion
  • 原文地址:https://www.cnblogs.com/mm163/p/10705939.html
Copyright © 2020-2023  润新知