CreativeVideoUnderstandJob.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package com.tzld.piaoquan.featurestools.job;
  2. import com.tzld.piaoquan.featurestools.dao.mapper.CreativeVideoUnderstanderMapper;
  3. import com.tzld.piaoquan.featurestools.model.po.CreativeVideoUnderstander;
  4. import com.tzld.piaoquan.featurestools.model.po.CreativeVideoUnderstanderExample;
  5. import com.tzld.piaoquan.featurestools.model.vo.VideoUnderstandParam;
  6. import com.tzld.piaoquan.featurestools.model.vo.VideoUnderstandResult;
  7. import com.tzld.piaoquan.featurestools.service.VideoUnderstandService;
  8. import com.tzld.piaoquan.featurestools.util.DateUtil;
  9. import com.tzld.piaoquan.featurestools.util.page.Page;
  10. import com.xxl.job.core.biz.model.ReturnT;
  11. import com.xxl.job.core.handler.annotation.XxlJob;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.apache.commons.lang3.StringUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Component;
  16. import org.springframework.util.CollectionUtils;
  17. import java.util.Date;
  18. import java.util.List;
  19. import java.util.concurrent.*;
  20. @Slf4j
  21. @Component
  22. public class CreativeVideoUnderstandJob {
  23. private static final ThreadPoolExecutor videoPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);
  24. private static final ArrayBlockingQueue<CreativeVideoUnderstander> videoQueue = new ArrayBlockingQueue<>(100000);
  25. @Autowired
  26. private CreativeVideoUnderstanderMapper creativeVideoUnderstanderMapper;
  27. @Autowired
  28. private VideoUnderstandService videoUnderstandService;
  29. @XxlJob("videoUnderstanderJob")
  30. public ReturnT<String> videoUnderstander(String param) throws InterruptedException {
  31. if (videoPoolExecutor.getCorePoolSize() - videoPoolExecutor.getActiveCount() > 0) {
  32. int threadSize = videoPoolExecutor.getCorePoolSize() - videoPoolExecutor.getActiveCount();
  33. log.info("threadNum={}", threadSize);
  34. CountDownLatch countDownLatch = new CountDownLatch(threadSize);
  35. // 启动消费者线程
  36. for (int i = 0; i < threadSize; i++) {
  37. videoPoolExecutor.execute(new Thread(() -> {
  38. log.info("启动视频理解线程");
  39. while (true) {
  40. try {
  41. // 超过 5 分钟没有数据,销毁当前线程
  42. CreativeVideoUnderstander creativeVideoUnderstander = videoQueue.poll(5, TimeUnit.MINUTES); // 等待最多 5 分钟
  43. log.info("videoQueue size={}", videoQueue.size());
  44. if (creativeVideoUnderstander == null) {
  45. break; // 退出当前线程
  46. }
  47. System.out.println("creativeVideoUnderstander=" + creativeVideoUnderstander);
  48. processCreativeVideoUnderstander(creativeVideoUnderstander);
  49. } catch (Exception e) {
  50. log.error("视频理解线程异常", e);
  51. }
  52. }
  53. log.info("视频理解线程结束");
  54. countDownLatch.countDown();
  55. }));
  56. }
  57. CreativeVideoUnderstanderExample example = new CreativeVideoUnderstanderExample();
  58. example.createCriteria().andStatusEqualTo(0);
  59. long count = creativeVideoUnderstanderMapper.countByExample(example);
  60. int pageSize = 1000;
  61. int pageNum = (int) (count / pageSize) + 1;
  62. for (int i = 1; i <= pageNum; i++) {
  63. Page page = new Page<>(i, pageSize);
  64. example.setPage(page);
  65. List<CreativeVideoUnderstander> creativeVideoUnderstanderList = creativeVideoUnderstanderMapper.selectByExample(example);
  66. if (!CollectionUtils.isEmpty(creativeVideoUnderstanderList)) {
  67. videoQueue.addAll(creativeVideoUnderstanderList);
  68. }
  69. }
  70. countDownLatch.await();
  71. }
  72. return ReturnT.SUCCESS;
  73. }
  74. private void processCreativeVideoUnderstander(CreativeVideoUnderstander creativeVideoUnderstander) {
  75. VideoUnderstandParam videoUnderstandParam = new VideoUnderstandParam();
  76. videoUnderstandParam.setUrl(creativeVideoUnderstander.getVideoUrl());
  77. // videoUnderstandParam.setFileName(creativeVideoUnderstander.getVideoFileName());
  78. VideoUnderstandResult videoUnderstandResult = videoUnderstandService.getVideoUnderstandResult(videoUnderstandParam);
  79. System.out.println("videoUnderstandResult=" + videoUnderstandResult);
  80. if (videoUnderstandResult == null) {
  81. addRetryCount(creativeVideoUnderstander);
  82. return;
  83. }
  84. if (StringUtils.isNotEmpty(videoUnderstandResult.getError())) {
  85. log.error("videoUnderstandResult id={} error={}", creativeVideoUnderstander.getId(), videoUnderstandResult.getError());
  86. addRetryCount(creativeVideoUnderstander);
  87. return;
  88. }
  89. if (StringUtils.isNotEmpty(videoUnderstandResult.getFileName())) {
  90. creativeVideoUnderstander.setVideoFileName(videoUnderstandResult.getFileName());
  91. }
  92. if (StringUtils.isNotEmpty(videoUnderstandResult.getFileStatus())) {
  93. creativeVideoUnderstander.setVideoFileStatus(videoUnderstandResult.getFileStatus());
  94. }
  95. if (StringUtils.isNotEmpty(videoUnderstandResult.getExpireTime())) {
  96. Date date = DateUtil.parseDate(videoUnderstandResult.getExpireTime());
  97. if (date != null) {
  98. creativeVideoUnderstander.setVideoFileExpireTime(date);
  99. }
  100. }
  101. if (StringUtils.isNotEmpty(videoUnderstandResult.getUnderstanderText())) {
  102. creativeVideoUnderstander.setUnderstanderText(videoUnderstandResult.getUnderstanderText());
  103. creativeVideoUnderstander.setStatus(1);
  104. creativeVideoUnderstanderMapper.updateByPrimaryKeyWithBLOBs(creativeVideoUnderstander);
  105. return;
  106. }
  107. addRetryCount(creativeVideoUnderstander);
  108. }
  109. private void addRetryCount(CreativeVideoUnderstander creativeVideoUnderstander) {
  110. if (creativeVideoUnderstander.getRetryCount() >= 3) {
  111. creativeVideoUnderstander.setStatus(2);
  112. } else {
  113. creativeVideoUnderstander.setRetryCount(creativeVideoUnderstander.getRetryCount() + 1);
  114. }
  115. creativeVideoUnderstanderMapper.updateByPrimaryKeySelective(creativeVideoUnderstander);
  116. }
  117. }