• SpringBoot整合RabbitMQ


    一、引入外部依赖 https://mvnrepository.com/ 

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

    二、代码

    spring.application.name=demo
    spring.rabbitmq.addresses=192.168.1.xxx:8088
    spring.rabbitmq.username=admin
    spring.rabbitmq.password=admin
    spring.rabbitmq.host: localhost
    spring.rabbitmq.port: 5672
    spring.rabbitmq.username: guest
    spring.rabbitmq.password: guest
    package com.example.mq;
    
    import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MySend {
        @Autowired
        private AmqpTemplate amqpTemplate;
    
        public void send(){
            amqpTemplate.convertAndSend("testQue","testMsg");
        }
    
    }
    package com.example.mq;
    
    import org.springframework.amqp.rabbit.annotation.RabbitHandler;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    
    @Component
    public class MyReceiver {
        @RabbitHandler
        @RabbitListener(queues = "testQue")
        public void receive(String text){
            System.out.println(text+".....");
        }
    
    }
    package com.example.controller;
    
    import com.example.mq.MySend;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jms.core.JmsTemplate;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class MQController {
        @Autowired
        private MySend mySend;
    
        @Autowired
        private JmsTemplate jmsTemplate;
    
        @RequestMapping("/activeMQ")
        @ResponseBody
        public String tests(){
            jmsTemplate.convertAndSend("testname","testMsg");
            return "ok";
        }
    
        @RequestMapping("/rabbitMQ")
        @ResponseBody
        public String test2(){
            mySend.send();
            return "ok";
        }
    }
  • 相关阅读:
    在mvc中,使用a链接,怎么转到别的html中
    mvc中怎么读取值传到后台中方法之一(表单传值法)
    mvc中怎么带参传递
    sqlserver去掉字符串结尾的全角空格并用半角替换
    Ajax学习笔记
    Ajax级联实例
    [转]js导航栏处于选中状态
    asp.net GridView的使用
    keycode大全
    IsPostBack的使用
  • 原文地址:https://www.cnblogs.com/mingforyou/p/14655480.html
Copyright © 2020-2023  润新知