kimi
2020-04-15 5d15287b2a06f978485ac6af71e33e1a82b43a65
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
45
46
47
48
49
50
51
52
53
54
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
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.highdatas.mdm.util.pool;
 
import com.alibaba.fastjson.JSONObject;
import com.highdatas.mdm.pojo.CodeMsg;
import com.highdatas.mdm.pojo.Result;
import com.highdatas.mdm.util.Constant;
import com.highdatas.mdm.util.DbUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
 
/**
 * @author kimi
 * @description
 * @date 2020-04-14 11:42
 */
 
 
public abstract class PriorityTask  implements Runnable, Comparable<PriorityTask>{
    @Value("${pool.coefficient}")
    Integer cntCoefficient;
    @Value("${pool.timeout}")
    Integer timeout;
    protected Long prority;
    protected String code;
    protected int cnt;
    protected Date time;
    protected Date endTime;
    protected HttpServletResponse response;
 
    public PriorityTask(String code, HttpServletResponse response, int cnt, Date time) {
        this.code = code;
        this.response = response;
        calPrority();
        calEndTime();
    }
 
    public PriorityTask(String code, HttpServletResponse response) {
        this.code = code;
        this.response = response;
    }
 
    public void calEndTime() {
        long time = this.time.getTime();
        if (timeout == null) {
            timeout = 3000000;
        }
        long l = time + timeout;
        this.endTime = new Date(l);
    }
 
    public void calPrority() {
        long basePrority = DbUtils.getDistanceTime2Long(DbUtils.getDateByEnd(), this.time) / 1000;
        if (cntCoefficient == null) {
            //默认系数50
            cntCoefficient = Constant.cntCoefficient;
        }
        int val = cnt * cntCoefficient;
        basePrority += val;
        this.prority = basePrority;
    }
 
    public String getCode() {
        return code;
    }
 
    public int getCnt() {
        return cnt;
    }
 
    public Date getEndTime() {
        return endTime;
    }
 
    public Date getTime() {
        return time;
    }
 
    public HttpServletResponse getResponse() {
        return response;
    }
 
    public PriorityTask(String code, long prority, HttpServletResponse response) {
        this.prority = prority;
        this.code = code;
        this.response = response;
    }
 
    public void setCnt(int cnt) {
        this.cnt = cnt;
        calPrority();
    }
 
    public void setTime(Date time) {
        this.time = time;
        calPrority();
        calEndTime();
    }
 
    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }
 
    public long getPrority() {
        return prority;
    }
 
    @Override
    public abstract void run();
 
 
    @Override
    public int compareTo(PriorityTask o) {
        return prority.compareTo(o.getPrority());
    }
 
    public void printTimeOut() throws IOException {
        PrintWriter writer = response.getWriter();
        response.setCharacterEncoding("UTF-8");
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        writer.write(JSONObject.toJSONString(Result.error(CodeMsg.TIMOUT_ERROR)));
    }
 
    public void printRepeat() throws IOException {
        PrintWriter writer = response.getWriter();
        response.setCharacterEncoding("UTF-8");
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        writer.write(JSONObject.toJSONString(Result.error(CodeMsg.REPEAT_ERROR)));
    }
}