|
|
@@ -140,7 +140,8 @@ import java.time.ZoneId;
|
|
|
import java.time.ZoneOffset;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.concurrent.*;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.fs.course.utils.LinkUtil.generateRandomStringWithLock;
|
|
|
@@ -3241,26 +3242,54 @@ public class Task {
|
|
|
// return;
|
|
|
// }
|
|
|
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+
|
|
|
+ // 10个线程的多线程执行
|
|
|
+ ExecutorService executor = new ThreadPoolExecutor(
|
|
|
+ 10, 10, 60L, TimeUnit.SECONDS,
|
|
|
+ new LinkedBlockingQueue<>(),
|
|
|
+ new ThreadPoolExecutor.CallerRunsPolicy());
|
|
|
+ AtomicInteger cacheCount = new AtomicInteger(0);
|
|
|
+ int totalTasks = contacts.size() * fsUserCourseVideos.size();
|
|
|
+ CountDownLatch latch = new CountDownLatch(totalTasks);
|
|
|
+
|
|
|
for (QwExternalContact contact : contacts) {
|
|
|
for (FsUserCourseVideo video : fsUserCourseVideos) {
|
|
|
- try {
|
|
|
- String key = "fei_shu_doc_id:" + contact.getId() + "_" + video.getVideoId();
|
|
|
- String feiShuDocId = redisCache.getCacheObject(key);
|
|
|
- if (StringUtils.isNotBlank(feiShuDocId)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- feiShuDocId = getFeiShuDocId(video, contact.getCompanyId(), contact.getCompanyUserId());
|
|
|
- if (StringUtils.isNotBlank(feiShuDocId)) {
|
|
|
- redisCache.setCacheObject("fei_shu_doc_id:" + contact.getId() + "_" + video.getVideoId(), feiShuDocId);
|
|
|
+ executor.execute(() -> {
|
|
|
+ try {
|
|
|
+ String key = "fei_shu_doc_id:" + contact.getId() + "_" + video.getVideoId();
|
|
|
+ String feiShuDocId = redisCache.getCacheObject(key);
|
|
|
+ if (StringUtils.isBlank(feiShuDocId)) {
|
|
|
+ feiShuDocId = getFeiShuDocId(video, contact.getCompanyId(), contact.getCompanyUserId());
|
|
|
+ if (StringUtils.isNotBlank(feiShuDocId)) {
|
|
|
+ redisCache.setCacheObject("fei_shu_doc_id:" + contact.getId() + "_" + video.getVideoId(), feiShuDocId);
|
|
|
+ int count = cacheCount.incrementAndGet();
|
|
|
+ logger.info("生成飞书缓存成功, 第{}个, contactId: {}, videoId: {}", count, contact.getId(), video.getVideoId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("生成飞书文档失败, 课程id: {}, 外部联系人id: {}, 错误信息: {}", video.getCourseId(), contact.getExternalUserId(), e.getMessage());
|
|
|
+ } finally {
|
|
|
+ latch.countDown();
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- logger.error("生成飞书文档失败, 课程id: {}, 外部联系人id: {}, 错误信息: {}", video.getCourseId(), contact.getExternalUserId(), e.getMessage());
|
|
|
- }
|
|
|
+ });
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ try {
|
|
|
+ latch.await();
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ logger.error("多线程执行被中断", e);
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ }
|
|
|
+ executor.shutdown();
|
|
|
+
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ logger.info("generateFeiShuRedisCache 执行完成, 总生成缓存数: {}, 总耗时: {} ms", cacheCount.get(), (end - start));
|
|
|
}
|
|
|
|
|
|
public void generateFeiShuRedisCacheFixed(){
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
List<FsUserCourseVideo> list = new ArrayList<>();
|
|
|
FsUserCourseVideo courseVideo = fsUserCourseVideoMapper.selectFsUserCourseVideoByVideoId(1672L);
|
|
|
list.add(courseVideo);
|
|
|
@@ -3284,23 +3313,49 @@ public class Task {
|
|
|
logger.error("未查询到外部联系人数据");
|
|
|
return;
|
|
|
}
|
|
|
+
|
|
|
+ // 10个线程的多线程执行
|
|
|
+ ExecutorService executor = new ThreadPoolExecutor(
|
|
|
+ 10, 10, 60L, TimeUnit.SECONDS,
|
|
|
+ new LinkedBlockingQueue<>(),
|
|
|
+ new ThreadPoolExecutor.CallerRunsPolicy());
|
|
|
+ AtomicInteger cacheCount = new AtomicInteger(0);
|
|
|
+ int totalTasks = contacts.size() * list.size();
|
|
|
+ CountDownLatch latch = new CountDownLatch(totalTasks);
|
|
|
+
|
|
|
for (QwExternalContact contact : contacts) {
|
|
|
for (FsUserCourseVideo video : list) {
|
|
|
- try {
|
|
|
- String key = "fei_shu_doc_id:" + contact.getId() + "_" + video.getVideoId();
|
|
|
- String feiShuDocId = redisCache.getCacheObject(key);
|
|
|
- if (StringUtils.isNotBlank(feiShuDocId)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- feiShuDocId = getFeiShuDocId(video, contact.getCompanyId(), contact.getCompanyUserId());
|
|
|
- if (StringUtils.isNotBlank(feiShuDocId)) {
|
|
|
- redisCache.setCacheObject("fei_shu_doc_id:" + contact.getId() + "_" + video.getVideoId(), feiShuDocId);
|
|
|
+ executor.execute(() -> {
|
|
|
+ try {
|
|
|
+ String key = "fei_shu_doc_id:" + contact.getId() + "_" + video.getVideoId();
|
|
|
+ String feiShuDocId = redisCache.getCacheObject(key);
|
|
|
+ if (StringUtils.isBlank(feiShuDocId)) {
|
|
|
+ feiShuDocId = getFeiShuDocId(video, contact.getCompanyId(), contact.getCompanyUserId());
|
|
|
+ if (StringUtils.isNotBlank(feiShuDocId)) {
|
|
|
+ redisCache.setCacheObject("fei_shu_doc_id:" + contact.getId() + "_" + video.getVideoId(), feiShuDocId);
|
|
|
+ int count = cacheCount.incrementAndGet();
|
|
|
+ logger.info("生成飞书缓存成功, 第{}个, contactId: {}, videoId: {}", count, contact.getId(), video.getVideoId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("生成飞书文档失败, 课程id: {}, 外部联系人id: {}, 错误信息: {}", video.getCourseId(), contact.getExternalUserId(), e.getMessage());
|
|
|
+ } finally {
|
|
|
+ latch.countDown();
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- logger.error("生成飞书文档失败, 课程id: {}, 外部联系人id: {}, 错误信息: {}", video.getCourseId(), contact.getExternalUserId(), e.getMessage());
|
|
|
- }
|
|
|
+ });
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ try {
|
|
|
+ latch.await();
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ logger.error("多线程执行被中断", e);
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ }
|
|
|
+ executor.shutdown();
|
|
|
+
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ logger.info("generateFeiShuRedisCacheFixed 执行完成, 总生成缓存数: {}, 总耗时: {} ms", cacheCount.get(), (end - start));
|
|
|
}
|
|
|
public List<FeishuAccount> getFeiShuAccountCompanyUser(Long companyId, Long companyUserId) {
|
|
|
String cacheKey = FEISHU_COMPANY_USER_KEY + companyId + ":" + companyUserId;
|