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;
|
}
|
}
|