优化同步成绩
This commit is contained in:
@@ -9,6 +9,7 @@ import com.srs.comprehensive.domain.CphStuScoreMiddle;
|
||||
import com.srs.comprehensive.domain.Dto.CphStuScoreMiddleDto;
|
||||
import com.srs.comprehensive.domain.Vo.CphStuScoreMiddleDtoVo;
|
||||
import com.srs.comprehensive.service.ICphStuScoreMiddlesService;
|
||||
import com.srs.comprehensive.config.SyncConfig;
|
||||
import com.sun.net.httpserver.Authenticator;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
@@ -21,6 +22,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;
|
||||
|
||||
import static com.srs.common.core.domain.AjaxResult.success;
|
||||
|
||||
@@ -34,6 +38,9 @@ public class CphStuScoreMiddleController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
public ICphStuScoreMiddlesService cphStuScoreMiddlesService;
|
||||
|
||||
@Autowired
|
||||
private SyncConfig syncConfig;
|
||||
|
||||
//同步学生成绩
|
||||
@GetMapping("/selectAllw")
|
||||
@@ -57,28 +64,85 @@ public class CphStuScoreMiddleController extends BaseController {
|
||||
}
|
||||
|
||||
//同步学生成绩,清空在添加
|
||||
@GetMapping("/selectAll")//16:03
|
||||
@GetMapping("/selectAll")
|
||||
public AjaxResult selectCphStuScoreMiddleEmptyAndAdd(){
|
||||
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 {
|
||||
logger.info("开始同步学生成绩数据...");
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
// 清空现有数据
|
||||
cphStuScoreMiddlesService.emptyTableDate();
|
||||
logger.info("已清空现有数据");
|
||||
|
||||
// 配置参数
|
||||
int pageSize = syncConfig.getPageSize();
|
||||
int threadPoolSize = syncConfig.getWebThreadPoolSize();
|
||||
|
||||
// 获取总数
|
||||
int studentInfoNumber = cphStuScoreMiddlesService.serectCphStuScoreMiddleCount(null);
|
||||
int totalPages = (int) Math.ceil((double) studentInfoNumber / pageSize);
|
||||
|
||||
logger.info("总记录数: {}, 总页数: {}, 每页大小: {}", studentInfoNumber, totalPages, pageSize);
|
||||
|
||||
// 创建线程池
|
||||
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();
|
||||
logger.debug("第{}页处理完成,记录数: {}", currentPage, cphStuScoreMiddles.size());
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
errorCount.incrementAndGet();
|
||||
logger.error("处理第{}页时发生错误: {}", currentPage, e.getMessage(), e);
|
||||
} finally {
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 等待所有任务完成
|
||||
boolean completed = latch.await(syncConfig.getTimeoutMinutes(), TimeUnit.MINUTES);
|
||||
|
||||
if (!completed) {
|
||||
logger.warn("同步任务未在指定时间内完成");
|
||||
return AjaxResult.error("同步任务超时");
|
||||
}
|
||||
|
||||
// 关闭线程池
|
||||
executor.shutdown();
|
||||
|
||||
long endTime = System.currentTimeMillis();
|
||||
long duration = endTime - startTime;
|
||||
|
||||
logger.info("学生成绩同步完成 - 总耗时: {}ms, 成功页数: {}, 失败页数: {}",
|
||||
duration, successCount.get(), errorCount.get());
|
||||
|
||||
if (errorCount.get() > 0) {
|
||||
return AjaxResult.warn("同步完成,但有部分数据失败。成功: " + successCount.get() +
|
||||
",失败: " + errorCount.get());
|
||||
}
|
||||
|
||||
return AjaxResult.success("同步完成,共处理 " + totalPages + " 页数据,耗时 " + duration + "ms");
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("同步学生成绩数据时发生严重错误", e);
|
||||
return AjaxResult.error("同步失败: " + e.getMessage());
|
||||
}
|
||||
executor.shutdown();
|
||||
return success();
|
||||
}
|
||||
|
||||
//查询sqlserver学生成绩
|
||||
|
||||
13
srs-admin/src/main/resources/application-sync.yml
Normal file
13
srs-admin/src/main/resources/application-sync.yml
Normal file
@@ -0,0 +1,13 @@
|
||||
# 数据同步配置
|
||||
sync:
|
||||
student-score:
|
||||
# 分页大小,建议根据内存和数据库性能调整
|
||||
page-size: 3000
|
||||
# Web接口线程池大小,建议不超过CPU核心数*2
|
||||
web-thread-pool-size: 10
|
||||
# 定时任务线程池大小,建议不超过CPU核心数
|
||||
scheduled-thread-pool-size: 8
|
||||
# 同步超时时间(分钟)
|
||||
timeout-minutes: 300
|
||||
# 批量插入大小,建议根据数据库性能调整(MySQL建议1000-2000)
|
||||
batch-insert-size: 1000
|
||||
Reference in New Issue
Block a user