Message queue (1) - Entity class

foreword

In the previous blog, we described the relevant knowledge of message queues. In this blog, we mainly explain the implementation of entity classes in message queues.

create project

Because there have been blogs written about the creation of SpringBoot before, I won’t describe too much here
1: First create a SpringBoot project,
2: Depend on at least two dependencies, SpringWeb and MyBatis (I use Lombok, it doesn’t matter if you don’t)
3 : Create related exchanges, queues, bindings, messages, and other entity classes

The creation of the switch entity class

For the switch, we need the name of the switch, the type of the switch, the persistence of the switch, whether to delete it automatically, and whether to configure additional parameters. These fields

package com.example.demo.mqServer.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

// 代表交换机的类

public class Exchange {
    
    
    // 交换机名字, 唯一标识
    private String name;
    // 交换机的类型
    private ExchangeType type = ExchangeType.direct;
    //判断是否要持久化
    private boolean durable = false;
    //判断是否自动删除
    private boolean autoDelete =false;
    //交换机指定的额外参数
    // 为了把argument存到数据库中, 要使用JSON转成字符串
    private Map<String,Object> arguments  = new HashMap<>();

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public ExchangeType getType() {
    
    
        return type;
    }

    public void setType(ExchangeType type) {
    
    
        this.type = type;
    }

    public boolean isDurable() {
    
    
        return durable;
    }

    public void setDurable(boolean durable) {
    
    
        this.durable = durable;
    }

    public boolean isAutoDelete() {
    
    
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
    
    
        this.autoDelete = autoDelete;
    }

    public String getArguments() {
    
    
        // 将当前的getArguments从map, 转换成String
        // SpringBoot 集成了JSON
        ObjectMapper objectMapper = new ObjectMapper();
        try {
    
    
           return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
    
    
            e.printStackTrace();
        }
        return "{}";
    }

    public void setArguments(String argumentsJson) {
    
    
        ObjectMapper objectMapper = new ObjectMapper();
        try {
    
    
            this.arguments = objectMapper.readValue(argumentsJson, new TypeReference<HashMap<String, Object>>() {
    
    });
        } catch (JsonProcessingException e) {
    
    
            e.printStackTrace();
        }

    }
}

package com.example.demo.mqServer.core;

public enum ExchangeType {
    
    
    direct(0),
    fanout(1),
    topic(2);

    private final int type;

    ExchangeType(int type) {
    
    
        this.type = type;
    }
}

Creation of queue entity class

For queues, its attribute values ​​are very similar to switches

package com.example.demo.mqServer.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

// 代表队列

public class MSGQueue {
    
    
    // 队列名字, 唯一标识
    private String name;
    //判断是否要持久化
    private boolean durable = false;
    // 判断该队列是否被独占
    private boolean exclusive = false;
    //判断是否自动删除
    private boolean autoDelete =false;
    //队列指定的额外参数
    private Map<String,Object> arguments  = new HashMap<>();

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public boolean isDurable() {
    
    
        return durable;
    }

    public void setDurable(boolean durable) {
    
    
        this.durable = durable;
    }

    public boolean isExclusive() {
    
    
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
    
    
        this.exclusive = exclusive;
    }

    public boolean isAutoDelete() {
    
    
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
    
    
        this.autoDelete = autoDelete;
    }

    public String getArguments() {
    
    
        // 将当前的getArguments从map, 转换成String
        // SpringBoot 集成了JSON
        ObjectMapper objectMapper = new ObjectMapper();
        try {
    
    
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
    
    
            e.printStackTrace();
        }
        return "{}";
    }

    public void setArguments(String argumentsJson) {
    
    
        ObjectMapper objectMapper = new ObjectMapper();
        try {
    
    
            this.arguments = objectMapper.readValue(argumentsJson, new TypeReference<HashMap<String, Object>>() {
    
    });
        } catch (JsonProcessingException e) {
    
    
            e.printStackTrace();
        }

    }
}

Creation of bound entity classes

For binding, its attribute value is very simple, it only needs the name of the switch, the name of the queue, and the password of both parties

package com.example.demo.mqServer.core;

import lombok.Data;

// 绑定
@Data
public class Binding {
    
    
    // 交换机名称
    private String exchangeName;
    // 队列名称
    private String msgQueueName;
    // 交换机与队列绑定关键字
    private String bindingKey;
}

Creation of message entity class

For the message, we design it like this, we store the message in the form of binary data in the file, so the property values ​​​​of the message are: message body - byte array, message property - article ID, article password (routingKey helps send this message to That queue), and whether the message needs to be persisted, and the beginning and end of the message article, and the deletion of the last message (for the deletion of the message, we adopt the logical deletion method, 0x1 means valid and 0x0 means invalid) For the message
, We also need to let him implement the Serializable interface so that we can instantiate it later

package com.example.demo.mqServer.core;

import lombok.Data;

import java.io.Serializable;
import java.util.UUID;
// 使用标准库中的序列化和反序列化
// 消息
@Data
public class Message implements Serializable {
    
    
    //消息的属性 - 防止拿到空
    private BasicProperties basicProperties = new BasicProperties();
    // 消息的正文
    private byte[] body;
    //偏移量 , beg,表示消息开头到文件开头有多少个字节 , End表示消息结尾到文件开头有多少个字节
//    前闭后开区间
//这俩个属性不需要序列化, 因为不往硬盘中存储
    private transient long offsetBeg;//transient为反序列化
    private transient long offsetEnd;
    // 用字节, 来表示是否逻辑删除 0x1 表示有效, 0x0表示无效
    private byte isValid = 0x1;


    // 创建一个工厂方法, 让工厂方法帮我们封装一下创建 Message 对象的过程.
    // 这个方法中创建的 Message 对象, 会自动生成唯一的 MessageId
    // 万一 routingKey 和 basicProperties 里的 routingKey 冲突, 以外面的为主.
    public static Message createMessageWithId(String RoutingKey ,BasicProperties basicProperties,byte[] body){
    
    
        Message message = new Message();
        if (basicProperties != null) {
    
    
            message.setBasicProperties(basicProperties);
        }
        // 此处生成的 MessageId 以 M- 作为前缀.
       message.basicProperties.setMessageId("M-"+UUID.randomUUID().toString());
       message.basicProperties.setRoutingKey(RoutingKey);
       message.body = body;
        // 此处是把 body 和 basicProperties 先设置出来. 他俩是 Message 的核心内容.
        // 而 offsetBeg, offsetEnd, isValid, 则是消息持久化的时候才会用到. 在把消息写入文件之前再进行设定.
        // 此处只是在内存中创建一个 Message 对象.
       return message;
    }

    public void setMessageId(String messageId){
    
    
        basicProperties.setMessageId(messageId);
    }
    public String  getMessageId(){
    
    
      return basicProperties.getMessageId();
    }
    public void  setRoutingKey(String routingKey){
    
    
        basicProperties.setRoutingKey(routingKey);
    }
    public String getRoutingKey(){
    
    
       return basicProperties.getRoutingKey();
    }
    public void setDeliverMode(int deliverMode){
    
    
        basicProperties.setDeliverMode(deliverMode);
    }
    public int getDeliverMode(){
    
    
        return basicProperties.getDeliverMode();
    }
}

package com.example.demo.mqServer.core;

import lombok.Data;

import java.io.Serializable;

@Data
public class BasicProperties implements Serializable {
    
    
    // 唯一身份表示, 使用UUID
    private String messageId;
    // 与BindingKey 对暗号的
//    如果是 direct 则是转发的队列名
//    如果是 Topic 则是对应的Sting
//    如果是 fanout 则无意义
    private String routingKey;
//    判断是否要持久化, 1代表持久化
    private int deliverMode =1;
}

summary

For the four entity classes, the creation is the most basic. Next, we store these entity classes in databases and files respectively. See the next blog

Guess you like

Origin blog.csdn.net/qq_56454895/article/details/131948315