许鹏程
2023-06-30 3bbfaa3d7d416afbd154576453c8ee9e7e2f8899
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
package com.product.kt.test.service2;
 
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
 
import com.product.core.spring.context.SpringMVCContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
 
import com.product.core.entity.FieldSetEntity;
import com.product.kt.test.config.SyncConfig;
import com.product.kt.test.service.ReadToolsService;
import com.product.kt.test.service.WriteToolService;
 
 
@Component
public class SyncToolsHandler2 {
 
 
    private static final Logger LOG = LoggerFactory.getLogger(SyncToolsHandler2.class);
 
    @Autowired
    ReadToolsService readToolsService;
 
    @Autowired
    WriteToolService writeToolService;
 
    //每次读取数据量大小
    public static int pageSize = SyncConfig.PAGE_SIZE;
 
    @Deprecated
    public void clearExecutor() {
 
    }
 
    /**
     * 获取数据
     *
     * @param tableName
     * @param orderField
     * @param timeField
     */
    @Async("ToolTask")
    public void syncToolData(String sourceLinkUUID, String tableName, String orderField, String timeField) {
        //获取数据总条数
        int countSize = readToolsService.getToolCount(sourceLinkUUID, tableName);
 
        //计算总页数
        int pageCount = (int) Math.ceil((double) countSize / (double) pageSize);
 
        LOG.info("数据操作表:" + tableName + ",  数据总条数:" + countSize + ", 总页数:" + pageCount);
 
        for (int i = 1; i <= pageCount; i++) {
            final int num = i;
            //获取数据
            List<FieldSetEntity> readDatas = readToolsService.getToolListByAutoId(sourceLinkUUID, num, pageSize, tableName,timeField);
            LOG.info("读取表" + tableName + "数据第" + (num) + "页共" + readDatas.size() + "条数据");
            if (readDatas != null) {
                // 接收集合各段的 执行的返回结果
                syncTools(sourceLinkUUID, readDatas, (num), tableName, timeField);
            }
        }
        SpringMVCContextHolder.getSystemLogger().info("数据插入完成.");
    }
 
    /**
     * 异步执行
     *
     * @param readToolList
     * @param pageIndex
     * @return
     */
    public Future<String> syncTools(String sourceLinkUUID, List<FieldSetEntity> readToolList, int pageIndex, String tableName, String timeField) {
 
        System.out.println("thread name " + Thread.currentThread().getName());
 
        LOG.info(String.format("此批数据的段数为:%s 此段数据的数据条数为:%s", pageIndex, readToolList.size()));
 
        //声明future对象
        Future<String> result = new AsyncResult<String>("");
 
        //循环遍历
        if (null != readToolList && readToolList.size() > 0) {
            try {
                writeToolService.writeData(sourceLinkUUID, readToolList, tableName, timeField);
            } catch (Exception e) {
                //记录出现异常的时间,线程name
                result = new AsyncResult<String>("fail,time=" + System.currentTimeMillis() + ",thread id=" + Thread.currentThread().getName() + ",pageIndex=" + pageIndex);
            }
        }
        return result;
    }
}