加线程授权许可

This commit is contained in:
ZLI263
2025-09-19 15:49:51 +08:00
parent c628249428
commit 323747196d

View File

@@ -29,6 +29,7 @@ import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Semaphore;
/**
@@ -76,7 +77,7 @@ public class TmNameplateCorpusServiceImpl extends ServiceImpl<TmNameplateCorpusM
@Autowired
@Resource(name = "threadPoolTaskExecutor")
private ThreadPoolTaskExecutor executor;
private Semaphore semaphore = new Semaphore(6); // 限制并发数
@Override
public void runNameplateCorpusDifyRetry(String paramJson) {
long startTime = System.currentTimeMillis();
@@ -170,43 +171,69 @@ public class TmNameplateCorpusServiceImpl extends ServiceImpl<TmNameplateCorpusM
log.info("铭牌语料第1个业务场景 优先执行。 ");
long startTime = System.currentTimeMillis();
CompletableFuture<Void> summaryTask = CompletableFuture.runAsync(() -> {
//第一个业务场景 开始: 总结和分类业务场景
JSONObject execDifyFlow = diFyService.executeDifyFlow(diFyImageReq, BusinessTypeEnum.SMART_ASSISTANT_NAMEPLATE.getCode(),
JSONObject.toJSONString(corpusReportDTO), null);
log.info("runDify execDifyFlow ,铭牌语料 ,总结和分类业务,返回: {}", execDifyFlow);
try {
log.info("铭牌语料可用许可授权数,总结和分类场景={}", semaphore.availablePermits());
// 获取许可 - 如果没有可用许可会阻塞等待
semaphore.acquire();
//第一个业务场景 开始: 总结和分类业务场景
JSONObject execDifyFlow = diFyService.executeDifyFlow(diFyImageReq, BusinessTypeEnum.SMART_ASSISTANT_NAMEPLATE.getCode(),
JSONObject.toJSONString(corpusReportDTO), null);
log.info("runDify execDifyFlow ,铭牌语料 ,总结和分类业务,返回: {}", execDifyFlow);
}catch (Exception e){
log.error("执行Dify失败: customerFlowId={}, 错误信息: {}",
item.getCustomerFlowId(), e.getMessage(), e);
}finally {
// 释放许可
semaphore.release();
}
}, executor);
long endTime = System.currentTimeMillis();
log.info("第一个业务场景(总结和分类)执行时间: {} ms", (endTime - startTime));
//第一个业务场景, 结束
long startTime2 = System.currentTimeMillis();
CompletableFuture<Void> portraitTask = CompletableFuture.runAsync(() -> {
//"analysisScene": "分析类型", // 1、企微会话 2、AI通话录音 3、AI铭牌(客流) 4、AI铭牌(试驾)
DiFyReq diFyImageReq2 = new DiFyReq();
diFyImageReq2.setUser(ConstantStr.corpus_user);
Map<String, Object> inputMap2 = new HashMap<>();
inputMap2.put("businessId", item.getCustomerFlowId());
inputMap2.put("communicateDate", item.getNameplateEndTime().toString());
inputMap2.put("analysisScene", "3");
inputMap2.put("version", 2);
inputMap2.put("chat", corpusChat);
diFyImageReq2.setInputs(inputMap2);
// 创建新的DiFyReq对象以避免线程安全问题
diFyImageReq2.setFlowId(oneTokenPortrait);
log.info("runDify execDifyFlow ,客户画像场景 token-{} , 对象: {}", oneTokenPortrait, diFyImageReq2);
JSONObject execDifyFlowForPortrait = diFyService.executeDifyFlow(diFyImageReq2, BusinessTypeEnum.CORPUS_PORTRAIT_NAMEPLATE.getCode(),
JSONObject.toJSONString(corpusReportDTO), null);
updateNameplate(execDifyFlowForPortrait.toJSONString());
log.info("runDify execDifyFlow ,铭牌语料 ,客户画像场景,返回: {}", execDifyFlowForPortrait);
try {
log.info("铭牌语料可用许可授权数,画像场景={}", semaphore.availablePermits());
// 获取许可 - 如果没有可用许可会阻塞等待
semaphore.acquire();
CompletableFuture<Void> portraitTask = CompletableFuture.runAsync(() -> {
//"analysisScene": "分析类型", // 1、企微会话 2、AI通话录音 3、AI铭牌(客流) 4、AI铭牌(试驾)
DiFyReq diFyImageReq2 = new DiFyReq();
diFyImageReq2.setUser(ConstantStr.corpus_user);
Map<String, Object> inputMap2 = new HashMap<>();
inputMap2.put("businessId", item.getCustomerFlowId());
inputMap2.put("communicateDate", item.getNameplateEndTime().toString());
inputMap2.put("analysisScene", "3");
inputMap2.put("version", 2);
inputMap2.put("chat", corpusChat);
diFyImageReq2.setInputs(inputMap2);
// 创建新的DiFyReq对象以避免线程安全问题
diFyImageReq2.setFlowId(oneTokenPortrait);
log.info("runDify execDifyFlow ,客户画像场景 token-{} , 对象: {}", oneTokenPortrait, diFyImageReq2);
JSONObject execDifyFlowForPortrait = diFyService.executeDifyFlow(diFyImageReq2, BusinessTypeEnum.CORPUS_PORTRAIT_NAMEPLATE.getCode(),
JSONObject.toJSONString(corpusReportDTO), null);
updateNameplate(execDifyFlowForPortrait.toJSONString());
log.info("runDify execDifyFlow ,铭牌语料 ,客户画像场景,返回: {}", execDifyFlowForPortrait);
}, executor);
} catch (Exception e) {
log.error("执行Dify失败: customerFlowId={}, 错误信息: {}",
item.getCustomerFlowId(), e.getMessage(), e);
} finally {
// 释放许可
semaphore.release();
}
}, executor);
long endTime2 = System.currentTimeMillis();
log.info("第二个业务场景(用户画像)执行时间: {} ms", (endTime2 - startTime2));
log.info("nameplate铭牌业务场景处理完总额和客户画像的总时间,启动线程 {}", (endTime2-startTime));
// 等待两个任务完成
CompletableFuture.allOf(summaryTask, portraitTask).join();
// CompletableFuture.allOf(summaryTask, portraitTask).join();
long endTime3 = System.currentTimeMillis();
log.info("nameplate铭牌业务场景处理完总额和客户画像的总时间,执行完成:{}", (endTime3-startTime));
} catch (Exception e) {