1. 程式人生 > >訊息佇列rabbitmq在java中的使用

訊息佇列rabbitmq在java中的使用

        前言:RabbitMQ是實現AMQP(高階訊息佇列協議)的訊息中介軟體的一種,最初用於在分散式系統中儲存轉發訊息,在易用性、擴充套件性、高可用性等方面表現不俗。RabbitMQ主要是為了實現系統之間的雙向解耦而實現的。當生產者大量產生資料時,消費者無法快速消費,那麼需要一箇中間層。儲存這個資料。

        在開始本次小demo之前,本地必須裝有rabbitmq,本人是在mac系統安裝的rabbitmq 3.7.9版本。在idea上使用spring boot框架進行編碼。

        1.首先建立一個spring boot專案,可以在idea中手動建立。也可以在spring的官方(

https://start.spring.io/)自動建立,再匯入到idea中。

        在spring官方網站中輸入Group和Artifact,然後點選Generate Project,會生成一個spring-boot專案的壓縮包,解壓後可以在idea中進行匯入。

        2.在建立好的spring-boot專案中引入rabbitmq的依賴

<!-- rabbitMq依賴 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

        3.開啟spring-boot專案resources資料夾下面的application檔案,預設建立時是properties格式,本人更喜歡yml,於是手動改為了application.yml,在此配置檔案中新增配置:

        yml語法格式配置如下,需要注意的yml的語法嚴格要求空格的縮排:

server:
  port: 8080

spring:
  application:
    name: spirng-boot-demo
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: admin
    password: 123456

        對應的properties配置如下:

server.port = 8080
spring.application.name = spirng-boot-demo
spring.rabbitmq.host = 127.0.0.1
spring.rabbitmq.port = 5672
spring.rabbitmq.username = admin
spring.rabbitmq.password = 123456

        4.建立一個佇列

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: yuyang
 * @description: 佇列配置
 * @date: 2018/11/24 15:23
 */
@Configuration
public class RabbitConfig {

    @Bean
    public Queue getQueue() {

        return new Queue("hello");
    }
}

        使用@Configuration註解和@Bean註解使得在spring-boot專案啟動的時候載入該RabbitConfig,建立一個名為“hello”的佇列。

        5.建立一個生產者類(HelloProducer),用來生產訊息

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author: yuyang
 * @description:
 * @date: 2018/11/24 15:24
 */
@Component
public class HelloProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send() {

        // 訊息
        String message = "hello " + new Date();
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>producer message:" + message);
        this.rabbitTemplate.convertAndSend("hello", message);
    }
}

        此處將HelloProducer生產者註冊到spring容器中,然後注入RabbitTemplate訊息佇列工具類,呼叫此工具類傳送訊息的時候,第一個引數為傳送訊息到目標佇列的佇列名稱,第二個引數為訊息內容。

        6.建立一個消費者類(HelloRecieve),用來消費佇列中的訊息

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author: yuyang
 * @description:
 * @date: 2018/11/24 15:25
 */
@Component
public class HelloRecieve {

    @RabbitHandler
    @RabbitListener(queues = "hello")
    public void process(String hello) {

        // 在控制檯列印訊息
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>recieve message:" + hello);
    }
}

        使用了@RabbitHandler註解和@RabbitListener註解來監聽佇列中的訊息,queues = "hello"指明瞭此消費者用來消費佇列名稱為“hello”的佇列訊息內容。

        7.在test包下建一個RabbitMqTest類來進行測試

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author: yuyang
 * @description:
 * @date: 2018/11/24 15:26
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMqTest {

    @Autowired
    private HelloProducer helloProducer;

    @Test
    public void hello() {

        this.helloProducer.send();
    }
}

        注入HelloProducer生產者類,呼叫它的send()方法傳送一條訊息。開啟測試:

        可以看到生產者生產的訊息被消費者給消費了。

        我們再來測試下,將消費者程式碼給註釋掉,重新執行測試類

        可以看到只有生產者生產訊息,而此條訊息並沒有被消費。

        我們看到在Queues下面有一個名為“hello”的佇列,有一條訊息在Ready狀態,正是我們剛剛那條未被消費者消費的訊息,點選佇列名“hello”,可以跳轉到此條佇列的資料頁面。

        此時我們把消費者註釋掉的程式碼再開啟,讓消費者可以正常消費,然後再執行一次測試類

        可以看到有兩條消費記錄,一條是之前那條未被消費的訊息,一條是剛剛執行測試類新產生的訊息,在消費者程式碼被開啟之後,一起被消費了。