ServerMessageReqVo.java 4.11 KB
Newer Older
1 2 3
package com.yanzuoguang.mq.vo.req;


4
import com.yanzuoguang.util.helper.StringHelper;
5 6 7 8 9 10 11 12 13
import io.swagger.annotations.ApiModelProperty;

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

/**
 * 发送给指定服务器消息
 *
 * @param <T>
14
 * @author 颜佐光
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 */
public class ServerMessageReqVo<T> extends ServerQueueReqVo {

    /**
     * 消息标记,在token为空时,发送给所有服务器队列,不为空时,发给token所在服务器的队列
     */
    @ApiModelProperty(notes = "消息标记,在token为空时,发送给所有服务器队列,不为空时,发给token所在服务器的队列")
    private String token;

    /**
     * 数据内容
     */
    @ApiModelProperty(notes = "数据内容")
    private T data;

    /**
     * 当前次数
     */
    @ApiModelProperty(notes = "当前次数")
    private int pos = 0;

    /**
     * 重试时间,默认为60*1000毫秒,达到指定次数的延迟时间
     */
    @ApiModelProperty(notes = "重试时间,默认为60*1000毫秒,达到指定次数的延迟时间")
    private Map<Integer, Long> repairTime = new HashMap<>();

    /**
     * 最大次数,达到最大次数时,会停止重发
     */
45
    @ApiModelProperty(notes = "最大次数,达到最大次数时,会停止重发。为0时,则默认为100")
46 47
    private int maxPos = 0;

48 49 50 51 52 53 54
    /**
     * 构造函数
     */
    public ServerMessageReqVo() {
        super(StringHelper.EMPTY);
    }

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    /**
     * 构造函数
     *
     * @param queueName
     * @param token
     * @param data
     */
    public ServerMessageReqVo(String queueName, String token, T data) {
        super(queueName);
        this.token = token;
        this.data = data;
    }

    /**
     * 构造函数
     *
     * @param queueName
     * @param token
     * @param data
     * @param maxPos
     */
    public ServerMessageReqVo(String queueName, String token, T data, int maxPos) {
        super(queueName);
        this.token = token;
        this.data = data;
        this.maxPos = maxPos;
    }

    /**
     * 构造函数
     *
     * @param queueName
     * @param token
     * @param data
     * @param repairTime
     */
    public ServerMessageReqVo(String queueName, String token, T data, Map<Integer, Long> repairTime) {
        super(queueName);
        this.token = token;
        this.data = data;
        this.repairTime = repairTime;
    }

    /**
     * 构造函数
     *
     * @param queueName
     * @param maxPos
     */
    public ServerMessageReqVo(String queueName, int maxPos) {
        super(queueName);
        this.maxPos = maxPos;
    }

    /**
     * 添加当前执行位置
     */
    public void addPos() {
        this.pos++;
    }

    /**
     * 是否继续往下执行
     *
     * @return
     */
    public boolean isNext() {
122
        if (this.maxPos == 0) {
123
            this.maxPos = 3;
124 125
        }
        return this.pos < this.maxPos;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    }

    /**
     * 获取下次延迟执行时间
     *
     * @return
     */
    public long getNextDelayTime() {
        Map.Entry<Integer, Long> max = null;
        if (this.repairTime != null) {
            for (Map.Entry<Integer, Long> kvp : this.repairTime.entrySet()) {
                if (kvp.getKey() > this.pos) {
                    continue;
                }
                if (max == null || max.getKey() < kvp.getKey()) {
                    max = kvp;
                }
            }
        }
145
        return max == null ? 600000 : max.getValue();
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getPos() {
        return pos;
    }

    public void setPos(int pos) {
        this.pos = pos;
    }

    public Map<Integer, Long> getRepairTime() {
        return repairTime;
    }

    public void setRepairTime(Map<Integer, Long> repairTime) {
        this.repairTime = repairTime;
    }

    public int getMaxPos() {
        return maxPos;
    }

    public void setMaxPos(int maxPos) {
        this.maxPos = maxPos;
    }

}