ScanArchiveFileScheduled.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.gz.scheduled;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.gz.config.MountArchiveFileConfig;
  5. import com.gz.core.exception.BusinessException;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.context.annotation.Profile;
  8. import org.springframework.data.redis.core.StringRedisTemplate;
  9. import org.springframework.scheduling.annotation.Scheduled;
  10. import org.springframework.stereotype.Component;
  11. import javax.annotation.Resource;
  12. import java.io.File;
  13. import java.util.Set;
  14. /**
  15. * 扫描档案文件定时任务
  16. *
  17. * @author LiuchangLan
  18. * @date 2021/3/16 13:36
  19. */
  20. @Slf4j
  21. @Component
  22. @Profile("prod")
  23. public class ScanArchiveFileScheduled {
  24. @Resource
  25. private StringRedisTemplate stringRedisTemplate;
  26. /**
  27. * @description 递归扫描文件夹 pdf、tif 文件放入队列 其他文件、空文件夹删除处理
  28. * @author LiuChangLan
  29. * @since 2021/3/23 15:52
  30. */
  31. private void sendFileToQUeue(File directory) throws InterruptedException {
  32. if (directory.isDirectory()) {
  33. // 获取文件夹下的所有文件
  34. File[] files = directory.listFiles();
  35. // 文件夹为空、非扫描根目录、文件夹不在运行 删除目录
  36. if (files.length == 0 && !directory.getPath().equals(MountArchiveFileConfig.SCAN_BASE_PATH)) {
  37. FileUtil.del(directory);
  38. log.info("文件夹{}为空,自动删除", directory.getPath());
  39. }
  40. for (File file : files) {
  41. if (file.isDirectory()) {
  42. // 扫描到文件夹 递归
  43. sendFileToQUeue(file);
  44. } else {
  45. // 等待处理中 跳过 处理错误 跳过
  46. if (MountArchiveFileConfig.WAIT_HANDLE_FILE.containsKey(file.getPath())) {
  47. log.info("文件[{}]待处理 跳过待处理文件", file.getPath());
  48. continue;
  49. }
  50. if (stringRedisTemplate.hasKey(String.format(MountArchiveFileConfig.ARCHIVE_FILE_MOUNT_ERROR_FOMAT, file.getName()))) {
  51. log.info("文件[{}]处理错误 跳过处理错误文件", file.getPath());
  52. continue;
  53. }
  54. // pdf 文件
  55. if ("pdf".equals(FileUtil.getType(file))) {
  56. MountArchiveFileConfig.WAIT_HANDLE_FILE.put(file.getPath(), file);
  57. MountArchiveFileConfig.PDF_QUEUE.put(file);
  58. log.info("扫描到pdf文件,当前队列还剩余{}个", MountArchiveFileConfig.PDF_QUEUE.size());
  59. // tif 文件
  60. } else if ("tif".equals(FileUtil.getType(file))) {
  61. MountArchiveFileConfig.WAIT_HANDLE_FILE.put(file.getPath(), file);
  62. MountArchiveFileConfig.TIF_QUEUE.put(file);
  63. log.info("扫描到tif文件,当前队列还剩余{}个", MountArchiveFileConfig.TIF_QUEUE.size());
  64. } else {
  65. FileUtil.del(file);
  66. log.info("扫描到其他类型文件,删除文件[{}]", file);
  67. }
  68. }
  69. }
  70. } else {
  71. throw new BusinessException(500, "入参非路径");
  72. }
  73. }
  74. /**
  75. * @description 扫描根目录下的文件 进行入队
  76. * @author LiuChangLan
  77. * @since 2021/3/17 14:16
  78. */
  79. @Scheduled(cron = "0/30 * * * * ?")
  80. public void startScan() throws InterruptedException {
  81. if (Boolean.parseBoolean(stringRedisTemplate.opsForValue().get(MountArchiveFileConfig.FILE_RUNNING))){
  82. log.warn("文件传输中 跳过扫描");
  83. return;
  84. }
  85. if (StrUtil.isEmpty(MountArchiveFileConfig.SCAN_BASE_PATH)) {
  86. log.warn("未配置扫描根目录 跳过扫描");
  87. return;
  88. }
  89. log.debug("开始扫描[{}]下的档案文件", MountArchiveFileConfig.SCAN_BASE_PATH);
  90. File scanPath = new File(MountArchiveFileConfig.SCAN_BASE_PATH);
  91. if (!scanPath.exists()) {
  92. throw new BusinessException(500, "档案根目录不存在,无法扫描");
  93. }
  94. this.sendFileToQUeue(scanPath);
  95. }
  96. /**
  97. * @description 挂载错误 定时重新入队
  98. * @author LiuChangLan
  99. * @since 2021/3/17 14:15
  100. */
  101. @Scheduled(cron = "0 0 1 * * ?")
  102. // @Scheduled(cron = "0/30 * * * * ?")
  103. public void errorRejoinQueue() throws InterruptedException {
  104. log.debug("开始读取错误队列,重新放入队列进行挂载");
  105. Set<String> keys = stringRedisTemplate.keys(String.format(MountArchiveFileConfig.ARCHIVE_FILE_MOUNT_ERROR_FOMAT, "*"));
  106. for (String key : keys) {
  107. String pdfPath = stringRedisTemplate.opsForHash().get(key, MountArchiveFileConfig.ERROR_FILE_PATH_KEY).toString();
  108. MountArchiveFileConfig.PDF_QUEUE.put(new File(pdfPath));
  109. log.debug("错误文件:[{}]重新入队", pdfPath);
  110. stringRedisTemplate.delete(key);
  111. }
  112. }
  113. }