天天看點

springboot整合mq發送消息隊列

寫在前面,mq簡稱消息隊列,本文介紹的是activemq.那mq主要用在什麼場景,他的作用又是什麼呢?

介紹:mq稱為消息中間件,語言表達不如看圖.

image.png

顧名思義,mq主要還是為了提高伺服器響應速度,提高客戶體驗.舉個例子大家就應該明白了。

比如當使用者登入某商城後,點選某商品要求發送郵件或者發送短信,此時郵件和短信是有可能失敗的,如果同步的話必然會引起客戶長時間等待,不利于客戶體驗(mq是異步).是以mq可以設定一個定時器,每隔一段時間對于發送失敗的郵件和短信重新發送。

第一步:建立marven項目。

第二步:在resource當中建立application.yml檔案

Image.png

然後在裡面輸入:

spring:
  activemq:


    broker-url: tcp://127.0.0.1:61616
    user: admin
    password: admin
queue: sunjian
server:
  port: 8081
           

第三步:在pom當中引入springboot

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sunjian.activemq.producer</groupId>
    <artifactId>sunjian-producer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
         <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
         <version>1.5.4.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <java.version>1.8</java.version>
    </properties>
    <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter</artifactId>
         </dependency>
         <!-- spring boot web支援:mvc,aop... -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-activemq</artifactId>
         </dependency>
         <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>fastjson</artifactId>
             <version>1.2.38</version>
         </dependency>
    </dependencies>
    <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
         </plugins>
    </build>
</project>
           

第四步: 建立entity實體類

package com.sunjian.entity;
public class UserEntity {
    private Long id;
    private String name;
    private Integer age;
    public UserEntity(Long id, String name, Integer age) {
         super();
         this.id = id;
         this.name = name;
         this.age = age;
    }
    public Long getId() {
         return id;
    }
    public void setId(Long id) {
         this.id = id;
    }
    public String getName() {
         return name;
    }
    public void setName(String name) {
         this.name = name;
    }
    public Integer getAge() {
         return age;
    }
    public void setAge(Integer age) {
         this.age = age;
    }
}
           

第五步:建立QueueConfig,一個消息隊列,主要作用是把消息隊列的名字傳進去

package com.sunjian.confg;

import javax.jms.Queue;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @classDesc: 功能描述:(建立一個隊列)
 */
@Configuration
public class QueueConfig {
    @Value("${queue}")//值就是sunjian
    private String queueName;

    @Bean
    public Queue queue() {//消息隊列的名字就是sunjian
        return new ActiveMQQueue(queueName);
    }

}
           

第六步:建立Producer,生産者,往消息隊列中發送消息,為了示範明顯,加入了定時任務

package com.sunjian;
import java.util.UUID;
import javax.jms.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.sunjian.entity.UserEntity;
/**
 * @classDesc: 功能描述:(生産者代碼)
 */
@Component//将Producer注入到容器
@EnableScheduling//定時任務的注解
public class Producer {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired//注入
    private Queue queue;
    private int age = 18;
    @Scheduled(fixedDelay = 5000)//每隔5秒鐘執行這個方法
    public void send() {
         age++;
         UserEntity userEntity = new UserEntity(System.currentTimeMillis(), UUID.randomUUID().toString(), age);
         String json = new JSONObject().toJSONString(userEntity);//将實體類轉換成json字元串
         System.out.println("json:" + json);
         jmsMessagingTemplate.convertAndSend(queue, json);//向指定隊列中發送消息
    }
}
           

第七部:建立啟動類App

package com.sunjian;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

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

}
           

代碼運作完畢:接下來,先安裝mq

第八步:下載下傳mq

mq下載下傳位址

為示範友善,我們下載下傳windows版本的mq

解壓之後,需要先安裝InstallService.bat,然後在運作activemq.bat

在網頁中運作mq網址

http://127.0.0.1:8161/admin/

賬号是admin,密碼是admin

第九步:mq已經裝完,代碼也已經寫完,測試一下看看,右鍵執行App

定時任務執行了7條資料,在sunjian當中也執行了7條.mq發送消息成功

activemq接收消息請看

關注我的公衆号,都是滿滿的幹貨!

孫堅.gif