• 【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--

  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/heyang78/p/15243000.html
Copyright © 2020-2023  润新知