feat(repository): 优化AI相机照片处理性能

- 引入CompletableFuture实现照片处理并发执行
- 创建专用线程池IMAGE_PROCESS_EXECUTOR管理异步任务
- 将原有串行处理逻辑改为并行处理
- 更新默认存储适配器从assets到assets-ext
This commit is contained in:
2025-12-06 21:14:29 +08:00
parent 554f55a7c1
commit df33e7929f

View File

@@ -28,11 +28,24 @@ import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
@Slf4j
@Component
public class SourceRepository {
private static final ExecutorService IMAGE_PROCESS_EXECUTOR = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors(),
runnable -> {
Thread thread = new Thread(runnable);
thread.setName("ai-cam-image-processor-" + thread.getId());
thread.setDaemon(true);
return thread;
}
);
@Autowired
private SourceMapper sourceMapper;
@Autowired
@@ -97,15 +110,20 @@ public class SourceRepository {
// 2. 构建图像处理配置
BceEnhancerConfig config = buildEnhancerConfig();
// 3. 为每张照片创建处理管线并执行
for (SourceEntity source : aiCamImages) {
try {
processSingleAiCamImage(source, config);
} catch (Exception e) {
log.error("处理AI相机照片失败, sourceId: {}, error: {}", source.getId(), e.getMessage(), e);
// 继续处理下一张照片,不中断整个流程
}
}
// 3. 并发处理所有照片
List<CompletableFuture<Void>> futures = aiCamImages.stream()
.map(source -> CompletableFuture.runAsync(() -> {
try {
processSingleAiCamImage(source, config);
} catch (Exception e) {
log.error("处理AI相机照片失败, sourceId: {}, error: {}", source.getId(), e.getMessage(), e);
// 继续处理下一张照片,不中断整个流程
}
}, IMAGE_PROCESS_EXECUTOR))
.collect(Collectors.toList());
// 4. 等待所有任务完成
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
log.info("AI相机照片处理完成, faceId: {}", faceId);
@@ -139,7 +157,7 @@ public class SourceRepository {
adapter = StorageFactory.get(configManager.getString("store_type"));
adapter.loadConfig(configManager.getObject("store_config_json", Map.class));
} catch (StorageUnsupportedException ignored) {
adapter = StorageFactory.use("assets");
adapter = StorageFactory.use("assets-ext");
}
context.setStorageAdapter(adapter);