天天看點

SpringBoot消息服務 —— SpringBoot整合ActiveMQ

SpringBoot整合ActiveMQ

首先了解以下什麼是消息隊列(Message Queue),消息隊列是一種程序中或者線程間的異步通信方式,使用消息隊列,消息生産者在産生消息後,會将消息儲存在消息隊列中,直到消息消費者來取走它,即消息的發送者和接收者不需要同時與消息隊列互動。使用消息隊列可以有效實作服務的解耦,并提高系統的可靠性以及擴充性

首先我們需要先安裝 ActiveMQ ,我安裝的是 Windows 版本的

ActiveMQ 官網下載下傳路徑:http://activemq.apache.org/download.html

ActiveMQ 安裝可參考: https://blog.csdn.net/mr_haixin/article/details/80418204

等安裝好 ActiveMQ 後,打開cmd指令行進入 ActiveMQ 的安裝目錄,執行此指令進行啟動 ActiveMQ,啟動成功後如下圖所示:
bin\activemq start
           
SpringBoot消息服務 —— SpringBoot整合ActiveMQ
AcitveMQ 啟動成功後,不要關閉cmd視窗,然後通路 http://localhost:8161/admin/ 進入 ActiveMQ Console 就表示ActiveMQ啟動成功了,如下所示:

8161是 ActiveMQ的預設端口号,它的賬号和密碼預設都是 admin

SpringBoot消息服務 —— SpringBoot整合ActiveMQ

整合 Spring Boot

1.首先添加 ActiveMQ 的相關依賴,代碼如下:

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

然後在 application.properties 中進行連接配接配置,代碼如下:

spring.activemq.broker-url=tcp://127.0.0.1:61616	//配置broker位址,預設端口是61616
spring.activemq.packages.trust-all=true	//配置所有信任的包
spring.activemq.user=admin	//賬号
spring.activemq.password=admin	//密碼
           

2.接下來在 項目配置類中 提供一個消息隊列的Bean,該 Bean 的執行個體就由 ActiveMQ 提供,代碼如下:

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import javax.jms.Queue;

@SpringBootApplication
public class ActivemqApplication {

    public static void main(String[] args) {
        SpringApplication.run(ActivemqApplication.class, args);
    }

    @Bean
    Queue queue() {
        return new ActiveMQQueue("amq");
    }

}
           

3.在建立一個 JMS 元件來完成消息的發送和接收,代碼如下:

import com.sang.activemq.entity.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.Queue;

@Component
public class JmsComponent {
    @Autowired  //提供一個 JMS 消息發送模闆
    JmsMessagingTemplate messagingTemplate;
    @Autowired
    Queue queue;

    public void send(Message msg) {
        messagingTemplate.convertAndSend(this.queue,msg);
    }

    @JmsListener(destination = "amq")
    public void receive(Message msg) {
        System.out.println("receive:" + msg);
    }
}
           

Message實體類

package com.sang.activemq.entity;

import java.io.Serializable;
import java.util.Date;

public class Message implements Serializable {
    private String content;
    private Date date;

    public Message(){
        super();
    }

    public Message(String content, Date date) {
        this.content = content;
        this.date = date;
    }

    @Override
    public String toString() {
        return "Message{" +
                "content='" + content + '\'' +
                ", date=" + date +
                '}';
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

           

4.最後進行測試

編寫測試類,完成消息發送測試,代碼如下:

在測試類中注入 JmsComponent 元件,然後調用元件的 send 方法發送一個 Message 對象。首先确定 ActiveMQ 啟動,然後啟動 SpringBoot 項目,最後執行單元測試,觀察 Spring Boot 項目日志,如下圖所示

@SpringBootTest
class ActivemqApplicationTests {

    @Autowired
    JmsComponent jmsComponent;

    @Test
    public void contextLoads() {
        Message msg = new Message();
        msg.setContent("hello jms!");
        msg.setDate(new Date());
        jmsComponent.send(msg);
    }
}
           
SpringBoot消息服務 —— SpringBoot整合ActiveMQ

這些就是 SpringBoot 整合ActiveMQ的簡單使用!!!