【RabbitMQ/Thymeleaf/axios/SpringBoot】SpringBoot/Thymeleaf工程如何通过RabbitMq收发消息

本文配套例程下载:https://files.cnblogs.com/files/heyang78/myBank-rabbitmq-sender-receiver-thymeleaf-axios_210908_1532.rar

前提:

一.安装配置好rebbitMq,如果不清楚请参考 https://www.cnblogs.com/heyang78/p/11601941.html

二.安装好后,用guest/guest登录,在Queues页面新建一队列queue01.

三.在pox.xml中书写RabbitMQ需要的依赖。

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

四.在application.properties中配置RabbitMQ的属性。

# Rebbitmq相关设置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

 

以往发消息要么是通过测试函数,要么是在后台运行,这回我打算让它点击页面按钮就发出去,以便更直观感受一下。

好,正文开始:

1.准备页面跳转

复制代码
@Controller
public class ActionController {
    @Autowired
    private StudentMapper studentMapper;
    
    @RequestMapping("/sendMsgPage")
    public String showSendMsgPage() {
        return "sendMsg";
    }
......
}
复制代码

做这个的目的是在浏览器地址栏输入路径时到达我们要发消息的页面。

 

2.撰写sendMsg.html

复制代码
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Send msg to rabbitMq</title>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

</head>
<body>
    <h1>Send msg to rabbitMq.</h1>
    <input type="text" id="msgtxt"/>
    <button id="sendBtn" onclick="send()">Send</button>
</body>
</html>
<script type="text/javascript">
    function send(){
        var msg=document.getElementById("msgtxt").value;
        console.log(msg);
    
        axios({
            method:'get',
            url:'sendMsg?msg='+msg,
            responseType:'json',
        }).then(function(resp){
            console.log(resp);
            if(resp.status==200){
                var status=resp.data;
                if(status=="OK"){
                    alert("Sent msg successfully.");
                }
            }
        }).catch(function(error){
            console.log(error);
        });
    }    

</script>
复制代码

这个页面借助了axios,将文本框的消息通过后台发送出去,后台与之对接的函数自然时路径sendMsg的函数了。

 

3.书写后台处理函数

复制代码
@RestController
public class JsonController {
    @Autowired
    private RabbitMqMsgSender msgSender;
    
    @GetMapping("/sendMsg")
    public String sendMsg(String msg) {        
        msgSender.send(msg);        
        return "OK";
    }
}
复制代码

这个里面借助了注入的RabbitMqMsgSender实例进行消息发送,让我们看看里面究竟是什么样子的。

 

4.撰写RabbitMqMsgSender

复制代码
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitMqMsgSender {
    @Autowired
    private AmqpTemplate mqTlt;
    
    public void send(String msg) {
        this.mqTlt.convertAndSend("queue01",msg);
    }
}
复制代码

这个类里面除了queue01需要我们自己写外,其它都是固定格式。

到此,如果前面的设置和代码都撰写无误,那从页面上点击send,文本框里的消息就会发送出去了,在RabbitMq的queue页面能看到发出来几个,如果没人接收这些消息就会预存着。

 

5.接收消息

复制代码
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues="queue01")
public class RabbitMqMsgReceiver {
    @RabbitHandler
    public void QueueReceive(String receivedMsg) {
        System.out.println("收到消息:"+receivedMsg);
    }
}
复制代码

这个类除了queue01和输出是我们撰写的,其它都是固定代码。把这个类放在工程里,一起动就会把queue01队列里的消息一个个全取出来。

当然,发送方和接收方一般不会在一个工程里,而应该位于两台机器的两个不同工程里,而RabbitMQ应该在一台独立的机器上,即一台机器发,一台机器装RabbitMQ做中介,一台机器收。这个道理大家都明白,就不再赘述了。

--END--

 

posted @   逆火狂飙  阅读(70)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2017-09-08 【Canvas技法】图解绘制圆弧的重要函数 arc(x,y,r,startAngle,endAngle,clockWise)
2013-09-08 已知:A点坐标为(-2,1),B点坐标为(1,4),若线段AB与反比例函数y=k/x存在交点,则k的取值范围是?
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示