1821349743@qq.com
2023-02-20 a387ff8978bd0e738e83e0277cfa38d7c3ab3080
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.product.data.sync.util;
 
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.util.CallBack;
import com.product.util.CallBackValue;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
class ThreadSelectManager {
 
    private Map<Integer, CustomResultSet> pageResultSet = new HashMap<>();
    /**
     * 开始查询页数
     */
    private int startPage;
 
    private ExecutorService executorService;
 
    private Connection sourceConnection;
 
    private CallBackValue<String> getQuerySql;
 
    private int totalPage = 0;
 
    private CallBack<Object> setColumnNames;
 
    private CallBackValue<String[]> getColumnNames;
 
 
    public ThreadSelectManager(BatchData batchData) {
        this.getQuerySql = (o) -> batchData.getQuerySql((int) o);
        this.setColumnNames = (o) -> {
            batchData.setColumnNames((String[]) o);
        };
        this.getColumnNames = (o) -> batchData.getColumnNames();
        this.sourceConnection = ConnectionManager.getConnection();
        this.totalPage = batchData.getTotalPage();
        this.currentResult = 1;
    }
 
    /**
     * 当前以获取第几页
     */
    private int currentResult = -1;
 
    private boolean openLog = true;
 
    /**
     * 开始查询
     *
     * @return
     */
    public boolean startQuery() {
        //不能重复开始查询
        if (this.pageResultSet.size() <= 0 && this.executorService == null) {
            this.executorService = Executors.newFixedThreadPool(1);
            this.queryData();
            return true;
        }
        return false;
    }
 
    /**
     * 获取下一页数据
     *
     * @return
     */
    public CustomResultSet getNext() {
        if (this.currentResult == -1) {
            return null;
        }
        int page = this.currentResult;
        if (this.pageResultSet.get(page) != null) {
            this.currentResult++;
            return this.pageResultSet.get(page);
        } else {
            if (!this.executorService.isTerminated()) {
                try {
                    outPutLog("等待线程中查询数据....", 1);
                    Thread.currentThread().sleep(1500);
                    return this.getNext();
                } catch (Exception e) {
                    outPutLog("等待线程中查询数据,出错....", 2);
                    SpringMVCContextHolder.getSystemLogger().error(e);
                    return null;
                }
            } else {
                this.currentResult++;
                outPutLog("查询数据线程已结束,没有 " + page + " 数据", 1);
                return null;
            }
        }
    }
 
    public static void main(String[] args) {
        int total = 3313;
        int i1 = total / 3;
        String[] ints = new String[3];
        for (int i = 1; i <= 3; i++) {
        }
    }
 
    /**
     * 查询数据
     */
    private void queryData() {
        outPutLog("开始线程查询数据,需要查询:" + totalPage + " 页数据", 1);
//        for (int i = 0; i < 3; i++) {
        this.executorService.execute(() -> {
            try {
                int currentPage = 1;
                while (totalPage > 0) {
                    outPutLog("开始查询页数:" + currentPage, 1);
                    String querySql = getQuerySql.method(currentPage);
                    outPutLog("查询sql:\n\t" + querySql, 1);
                    PreparedStatement pst = sourceConnection.prepareStatement(querySql);
                    ResultSet resultSet = pst.executeQuery();
                    if (resultSet != null) {
                        String[] columnNames = this.getColumnNames.method(null);
                        CustomResultSet customResultSet = new CustomResultSet(resultSet, columnNames);
                        if (columnNames == null || columnNames.length <= 0) {
                            this.setColumnNames.method(customResultSet.getColumnNames());
                        }
                        this.pageResultSet.put(currentPage, customResultSet);
                    }
                    currentPage++;
                    this.totalPage--;
                }
            } catch (Exception e) {
                e.printStackTrace();
                outPutLog("线程线程查询数据,出错....", 2);
                SpringMVCContextHolder.getSystemLogger().error(e);
            }
        });
//        }
        this.executorService.shutdown();
    }
 
    /**
     * 输出日志
     *
     * @param msg  消息
     * @param type 类型  1 info 2 error
     */
    private void outPutLog(String msg, int type) {
        if (this.openLog) {
            if (type == 1) {
                SpringMVCContextHolder.getSystemLogger().info(msg);
            } else if (type == 2) {
                SpringMVCContextHolder.getSystemLogger().error(msg);
            }
        }
    }
 
 
}