Callable Future 实现多线程按照顺序上传文件

/**

* 异步上传 BiopsyFlow 中的所有视频和音频文件到暂存服务器

*/

private List<String> uploadBiopsyFlowFiles(BiopsyFlow biopsyFlow, SyncFile SyncFile) {

List<String> fileBase64List = syncFile.getFileBase64List();

if (CollectionUtils.isEmpty(syncFile.getFileBase64List())) {

log.info("没有文件需要上传");

return Collections.emptyList();

}

log.info("上传文件数量:{}", fileBase64List.size());

List<Callable<String>> tasks = new ArrayList<>();

for (String file : fileBase64List) {

Callable<String> task = () -> {

log.debug("上传线程:{}", Thread.currentThread().getName());

try {

return fileDealUtils.uploadTxFile(file, biopsyFlow.getTransId());

} catch (Exception e) {

log.error("文件上传失败, 异常={}", e.getMessage());

return null;

}

};

tasks.add(task);

}

// 使用公共线程池提交任务

List<Future<String>> futures = new ArrayList<>();

try {

for (Callable<String> task : tasks) {

Future<String> future = pullVideoTaskExecutor.submit(task);

futures.add(future);

}

// 按顺序获取结果

List<String> fileIds = new ArrayList<>();

for (Future<String> future : futures) {

try {

String fileId = future.get();

if (fileId != null) {

fileIds.add(fileId);

} else {

log.error("文件上传失败: 返回的文件ID为空");

return Collections.emptyList();

}

} catch (Exception e) {

log.error("文件上传失败: 异常={}", e.getMessage());

return Collections.emptyList();

}

}

// // TODO 测试下载,上线之前删除

// for (String fileId : fileIds) {

// String base64 = fileDealUtils.testDownMp4(fileId);

// log.info("下载的文件id:{}, 对应下载的视频base64:{}", fileId,base64.substring(0,100));

// }

return fileIds;

} catch (Exception e) {

log.error("任务执行过程中发生异常", e);

return Collections.emptyList();

}

}

相关推荐
怒放吧德德6 小时前
Netty 4.2 入门指南:从概念到第一个程序
java·后端·netty
雨中飘荡的记忆7 小时前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案
java·redis·后端
心之语歌10 小时前
基于注解+拦截器的API动态路由实现方案
java·后端
华仔啊11 小时前
Stream 代码越写越难看?JDFrame 让 Java 逻辑回归优雅
java·后端
ray_liang11 小时前
用六边形架构与整洁架构对比是伪命题?
java·架构
Ray Liang12 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Java水解12 小时前
Java 中间件:Dubbo 服务降级(Mock 机制)
java·后端
SimonKing17 小时前
OpenCode AI辅助编程,不一样的编程思路,不写一行代码
java·后端·程序员
FastBean17 小时前
Jackson View Extension Spring Boot Starter
java·后端
Seven9718 小时前
剑指offer-79、最⻓不含重复字符的⼦字符串
java