17 changed files with 155 additions and 130 deletions
@ -1,58 +0,0 @@ |
|||
package com.project.classicpaper.domain.service; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.concurrent.ConcurrentHashMap; |
|||
import java.util.concurrent.CountDownLatch; |
|||
|
|||
/** |
|||
* 经典套卷生题等待跟踪器 |
|||
* generate() 通过它等待所有异步回调完成后才返回 |
|||
*/ |
|||
@Component |
|||
@Slf4j |
|||
public class PaperGenerationTracker { |
|||
|
|||
private final ConcurrentHashMap<Long, CountDownLatch> latches = new ConcurrentHashMap<>(); |
|||
|
|||
/** |
|||
* 开始跟踪某个 paper 的生成进度 |
|||
* @param paperId 套卷ID |
|||
* @param totalCount 需要生成的题目总数 |
|||
*/ |
|||
public void track(Long paperId, int totalCount) { |
|||
latches.put(paperId, new CountDownLatch(totalCount)); |
|||
log.info(">>> [生题跟踪] 开始跟踪 paperId={}, 总题数={}", paperId, totalCount); |
|||
} |
|||
|
|||
/** |
|||
* 某道题回调完成,countDown |
|||
*/ |
|||
public void onQuestionGenerated(Long paperId) { |
|||
CountDownLatch latch = latches.get(paperId); |
|||
if (latch != null) { |
|||
latch.countDown(); |
|||
log.debug(">>> [生题跟踪] paperId={}, 剩余={}", paperId, latch.getCount()); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 等待所有题目生成完成(一直等待,无超时) |
|||
*/ |
|||
public void waitForCompletion(Long paperId) { |
|||
CountDownLatch latch = latches.get(paperId); |
|||
if (latch == null) { |
|||
return; |
|||
} |
|||
try { |
|||
latch.await(); |
|||
log.info(">>> [生题跟踪] paperId={} 所有题目生成完成", paperId); |
|||
} catch (InterruptedException e) { |
|||
Thread.currentThread().interrupt(); |
|||
log.warn(">>> [生题跟踪] paperId={} 等待被中断", paperId); |
|||
} finally { |
|||
latches.remove(paperId); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue