首页 > 编程语言 > 详细

SpringBoot整合RabbitMQ

时间:2021-04-14 01:48:05      阅读:22      评论:0      收藏:0      [点我收藏+]

一、引入外部依赖 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";
    }
}

 

SpringBoot整合RabbitMQ

原文:https://www.cnblogs.com/mingforyou/p/14655480.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!