parent
eb617c938f
commit
cf3f5d300b
@ -0,0 +1,56 @@
|
||||
package cn.teammodel.callAble;
|
||||
|
||||
import cn.teammodel.model.entity.school.LessonRecord;
|
||||
import cn.teammodel.repository.LessonRecordRepository;
|
||||
import com.azure.spring.data.cosmos.core.query.CosmosPageRequest;
|
||||
import org.springframework.data.domain.Slice;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class LessonRecordQueryTask implements Callable<List<LessonRecord>> {
|
||||
private final LessonRecordRepository repository;
|
||||
private final String lessonRecordKey;
|
||||
private final Long startTime;
|
||||
private final Long endTime;
|
||||
private final String subjectId;
|
||||
private final String tmdId;
|
||||
private final String grade;
|
||||
private final String periodId;
|
||||
private static final int SLICE_SIZE = 1000;
|
||||
|
||||
public LessonRecordQueryTask(LessonRecordRepository repository, String lessonRecordKey,
|
||||
long startTime, long endTime,
|
||||
String subjectId, String tmdId, String grade,
|
||||
String periodId) {
|
||||
this.repository = repository;
|
||||
this.lessonRecordKey = lessonRecordKey;
|
||||
this.startTime = startTime;
|
||||
this.endTime = endTime;
|
||||
this.subjectId = subjectId;
|
||||
this.tmdId = tmdId;
|
||||
this.grade = grade;
|
||||
this.periodId = periodId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LessonRecord> call() throws Exception {
|
||||
List<LessonRecord> result = new ArrayList<>();
|
||||
CosmosPageRequest pageRequest = new CosmosPageRequest(0, SLICE_SIZE, null);
|
||||
|
||||
while (true) {
|
||||
Slice<LessonRecord> slice = repository.getLessonsByConditions(
|
||||
lessonRecordKey, startTime, endTime, subjectId, tmdId, grade, periodId, pageRequest
|
||||
);
|
||||
result.addAll(slice.getContent());
|
||||
|
||||
if (!slice.hasNext()) {
|
||||
break;
|
||||
}
|
||||
pageRequest = (CosmosPageRequest) slice.nextPageable();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package cn.teammodel.test;
|
||||
|
||||
import cn.teammodel.callAble.LessonRecordQueryTask;
|
||||
import cn.teammodel.model.entity.school.LessonRecord;
|
||||
import cn.teammodel.repository.LessonRecordRepository;
|
||||
import com.azure.spring.data.cosmos.core.query.CosmosPageRequest;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class LessonRecordQueryService {
|
||||
//private static final int SLICE_SIZE = 1000; // 分页大小
|
||||
private static final int THREAD_POOL_SIZE = 4; // 线程池大小
|
||||
|
||||
private final LessonRecordRepository repository;
|
||||
private final ExecutorService executor;
|
||||
|
||||
public LessonRecordQueryService(LessonRecordRepository repository) {
|
||||
this.repository = repository;
|
||||
this.executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
|
||||
}
|
||||
|
||||
public List<LessonRecord> queryLessonsInParallel(String lessonRecordKey,
|
||||
Long startTime,
|
||||
Long endTime,
|
||||
String subjectId,
|
||||
String tmdId,
|
||||
String grade,
|
||||
String periodId) throws InterruptedException, ExecutionException {
|
||||
List<Future<List<LessonRecord>>> futures = new ArrayList<>();
|
||||
|
||||
// 按时间范围拆分任务
|
||||
long timeRange = endTime - startTime;
|
||||
int numTasks = THREAD_POOL_SIZE; // 拆分为与线程池大小相同的任务数
|
||||
|
||||
for (int i = 0; i < numTasks; i++) {
|
||||
long chunkStart = startTime + (i * timeRange / numTasks);
|
||||
long chunkEnd = (i == numTasks - 1) ? endTime : startTime + ((i + 1) * timeRange / numTasks);
|
||||
|
||||
//CosmosPageRequest pageRequest = new CosmosPageRequest(0, SLICE_SIZE, null);
|
||||
LessonRecordQueryTask task = new LessonRecordQueryTask(
|
||||
repository, lessonRecordKey, chunkStart, chunkEnd,
|
||||
subjectId, tmdId, grade, periodId
|
||||
);
|
||||
futures.add(executor.submit(task));
|
||||
}
|
||||
|
||||
// 合并结果
|
||||
List<LessonRecord> allRecords = new ArrayList<>();
|
||||
for (Future<List<LessonRecord>> future : futures) {
|
||||
allRecords.addAll(future.get());
|
||||
}
|
||||
|
||||
return allRecords;
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
executor.shutdown();
|
||||
try {
|
||||
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
|
||||
executor.shutdownNow(); // 强制关闭未完成的任务
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
executor.shutdownNow();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue