SpringBoot對消息隊列(MQ)的支持


1.異步消息的定義
  異步消息的主要目的是為了系統與系統之間的通信,所謂異步消息即消息發送者無需等待消息接收者的處理以及返回,甚至無需關心消息是否發送成功
  在異步消息中有兩個很重要的概念,即消息代理和目的地,當消息發送者發送消息之后,消息將由消息代理接管,消息代理保證消息傳遞到指定目的地。
  異步消息主要有兩種目的地形式,隊列(queue)和主題(topic),隊列用於點對點形式的消息通信,主題用於發布訂閱式的消息通信。

1.1目的地形式分類
1.1.1點對點式
  當消息發送者發送消息,消息代理將消息后將消息放進一個隊列里,當有消息接收者來接收消息的時候,消息將從隊列中取出傳遞給消息接收者,這時候隊列里就沒有了這條消息。點對點式確保每一條消息只有唯一的發送者和接收者,但這並不能說明只有一個接收者能夠從隊列中接收消息,因為隊列中有多個消息,點對點式只保證每一條消息只有唯一的發送者和接收者

1.1.2發布/訂閱式
  發布訂閱式是消息發送者發送消息到主題,而多個消息接收者監聽這個主題,此時的消息發送者和接收者分別叫做發布者和訂閱者

1.2 企業級消息代理
  JMS即JAVA消息服務,是基於JVM的消息代理規范,ActiveMQ是一個JMS的實現
AMQP也是一個消息代理的規范,他不僅兼容JMS,還支持跨語言和平台,AMQP的主要實現是RabbitMQ

1.3 Spring以及SpringBoot的支持
  Spring針對JMS和RabbitMQ分別提供了JmsTemplete和RabbitTemplete來發送消息。為我們提供了@JmsListener,@RabbitListener注解來監聽消息代理發送的消息。我們分別需要通過@EnableJms和@EnableRabbit來開啟支持
  SpringBoot自動配置了上述@EnableJms,@EnableRabbit,JmsTemplete,RabbitTemplete的支持,同時我們可以在application.properties文件中分別以spring.activemq和spring.rabbitmq來分別配置所需的屬性。

2.SpringBoot對JMS(ActiveMQ)的支持
下載安裝
ActiveMQ的官方下載地址:http://activemq.apache.org/download.html,下載安裝完成后
進入bin目錄,發現有win32和win64兩個文件夾,這2個文件夾分別對應windows32位和windows64位操作系統的啟動腳本。進入對應的文件夾中雙擊activemq.bat。即可正常啟動
訪問http://localhost:8161/admin。輸入默認的用戶名和密碼:admin/admin即可進入ActiveMQ的控制台

2.2 配置
SpringBoot提供了針對ActiveMQ的支持,只需要在pom.xml文件中引入即可:

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


在application.properties配置ActiveMQ的消息代理地址:

spring.activemq.broker-url=tcp://localhost:61616


注意,此處配置的消息代理必須讓ActiveMQ啟動時才有作用,否則無效

在實際情況下,消息的發布者和接受者一般都是分開的,而這里,我們僅作測試,將消息發送者和接收者放在一個程序中

2.3代碼文件
2.3.1消息定義

public class Msg implements MessageCreator {
    @Override
    public Message createMessage(Session session) throws JMSException {
        return session.createTextMessage("測試消息");
    }
}


2.3.2消息發送及目的地定義

@SpringBootApplication
public class SpringBootMqApplication implements CommandLineRunner{

    @Autowired
    JmsTemplate  jmsTemplate;
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMqApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
            jmsTemplate.send("my-destination",new Msg());
    }
}

CommandLineRunner接口中的run方法,是在程序啟動后就會執行的代碼。JmsTemplate 是用來操作JMS消息的操作類。

2.3.3消息監聽

@Component
public class Receiver {
    @JmsListener(destination = "my-destination")
    public void  receivedMessage(String message){
        System.out.println("接受到"+message);
    }
}

@JmsListener顯示的定義了指定要監聽的目的地。

 

2.3.4運行結果
運行結果顯示監聽收到了消息

 

 

 

 

ActiveMQ的控制台中顯示我們發送的消息

 

 

 

3.SpringBoot對AMQP(RabbitMQ)的支持
3.1RabbitMQ的安裝配置
  RabbitMQ是基於Erlang語言開發的。所以安裝RabbitMQ之前需要先下載安裝配置Erlang,下載地址:http://www.erlang.org/downloads
並將安裝后的D:\Program Files\erl9.0\bin的bin目錄配置到path環境變量中。然后下載安裝RabbitMQ。下載地址:http://www.rabbitmq.com/download.html
安裝完成之后在開始菜單中找到RabbitMQ Command Promt,打開控制台,輸入命令

rabbitmq-plugins enable rabbitmq_management


控制台無錯誤之后,訪問http://localhost:15672。使用默認的用戶名/密碼:guest/guest進行登錄
即可見到如圖所示界面:

 

 

3.2 RabbitMQ測試代碼文件
SpringBoot默認Rabbit的主機為localhost,端口號為5672,所以我們無需為RabbitMQ配置其他信息。
入口文件

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.lang.annotation.Annotation;

@SpringBootApplication
public class SpringBootAmqpApplication implements CommandLineRunner {
    @Autowired
     RabbitTemplate rabbitTemplate;
    public static void main(String[] args) {
        SpringApplication.run(SpringBootAmqpApplication.class, args);
    }

    @Bean //2
    public Queue wiselyQueue(){
        return new Queue("my-queue");
    }

    @Override
    public void run(String... strings) throws Exception {
        rabbitTemplate.convertAndSend("my-queue","來自RabbitMQ的問候");
    }
}

 

接收類

@Component
public class Receiver {
    @RabbitListener(queues = "my-queue")
    public  void  ReceiveMesaage(String  message){
        System.out.println("接受到"+message);
    }
}

 


3.3 測試結果

 

在RabbitMQ控制台中額可以看到

 

 

4 小結
  這里主要是對ActiveMQ和RabbitMQ進行了簡單的嘗試,了解了異步消息的通信。有興趣的同學可以進行深入研究。

 

 

 

 

 

 

 

 


————————————————
原文鏈接:https://blog.csdn.net/u011342403/article/details/77940765


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM