加线程授权许可

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