优化同步成绩
This commit is contained in:
@@ -12,6 +12,9 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Component("CphTask")
|
||||
public class CphTask {
|
||||
@@ -56,26 +59,73 @@ public class CphTask {
|
||||
* 同步学生成绩
|
||||
*/
|
||||
public void stuScoreMiddleSync(){
|
||||
cphStuScoreMiddlesService.emptyTableDate();
|
||||
int pageNum=1;
|
||||
int pageSize=3000;
|
||||
int studentInfoNumber = cphStuScoreMiddlesService.serectCphStuScoreMiddleCount(null);//总数
|
||||
int sum=studentInfoNumber/ pageSize;//需要循环的次数
|
||||
int sumS=studentInfoNumber%pageSize>0? sum + 1: sum;
|
||||
QueryWrapper<CphStuScoreMiddleDto> objectQueryWrapper = new QueryWrapper<>();
|
||||
objectQueryWrapper.orderByDesc("stu_no", "kcdm");
|
||||
ExecutorService executor = Executors.newFixedThreadPool(15);
|
||||
for (pageNum=1; pageNum <= sumS; pageNum++){
|
||||
final int currentPage = pageNum;
|
||||
executor.execute(() -> {
|
||||
List<Map<String, Object>> cphStuScoreMiddles = cphStuScoreMiddlesService
|
||||
.serectCphStuScoreMiddlesXh(currentPage, pageSize, null);
|
||||
cphStuScoreMiddlesService.selectCphStuScoreMiddleEmptyAndAdd(cphStuScoreMiddles);
|
||||
cphStuScoreMiddles.clear();
|
||||
});
|
||||
try {
|
||||
System.out.println("开始同步学生成绩数据...");
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
// 清空现有数据
|
||||
cphStuScoreMiddlesService.emptyTableDate();
|
||||
System.out.println("已清空现有数据");
|
||||
|
||||
// 配置参数
|
||||
int pageSize = 3000;
|
||||
int threadPoolSize = 8; // 定时任务使用较少线程,避免影响其他任务
|
||||
|
||||
// 获取总数
|
||||
int studentInfoNumber = cphStuScoreMiddlesService.serectCphStuScoreMiddleCount(null);
|
||||
int totalPages = (int) Math.ceil((double) studentInfoNumber / pageSize);
|
||||
|
||||
System.out.println("总记录数: " + studentInfoNumber + ", 总页数: " + totalPages);
|
||||
|
||||
// 创建线程池
|
||||
ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
|
||||
CountDownLatch latch = new CountDownLatch(totalPages);
|
||||
AtomicInteger successCount = new AtomicInteger(0);
|
||||
AtomicInteger errorCount = new AtomicInteger(0);
|
||||
|
||||
// 提交任务
|
||||
for (int pageNum = 1; pageNum <= totalPages; pageNum++) {
|
||||
final int currentPage = pageNum;
|
||||
executor.submit(() -> {
|
||||
try {
|
||||
List<Map<String, Object>> cphStuScoreMiddles = cphStuScoreMiddlesService
|
||||
.serectCphStuScoreMiddlesXh(currentPage, pageSize, null);
|
||||
|
||||
if (cphStuScoreMiddles != null && !cphStuScoreMiddles.isEmpty()) {
|
||||
cphStuScoreMiddlesService.selectCphStuScoreMiddleEmptyAndAdd(cphStuScoreMiddles);
|
||||
successCount.incrementAndGet();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
errorCount.incrementAndGet();
|
||||
System.err.println("处理第" + currentPage + "页时发生错误: " + e.getMessage());
|
||||
} finally {
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 等待所有任务完成
|
||||
boolean completed = latch.await(30, TimeUnit.MINUTES);
|
||||
|
||||
if (!completed) {
|
||||
System.err.println("同步任务未在指定时间内完成");
|
||||
return;
|
||||
}
|
||||
|
||||
// 关闭线程池
|
||||
executor.shutdown();
|
||||
|
||||
long endTime = System.currentTimeMillis();
|
||||
long duration = endTime - startTime;
|
||||
|
||||
System.out.println("学生成绩同步完成 - 总耗时: " + duration + "ms, 成功页数: " +
|
||||
successCount.get() + ", 失败页数: " + errorCount.get());
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("同步学生成绩数据时发生严重错误: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
executor.shutdown();
|
||||
//cphStuScoreMiddlesService.syncCphStuScoreMiddles();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user