|
@@ -0,0 +1,91 @@
|
|
|
+package com.shkpr.service.alambizplugin.configuration;
|
|
|
+
|
|
|
+import com.global.base.log.LogLevelFlag;
|
|
|
+import com.global.base.log.LogPrintMgr;
|
|
|
+import com.shkpr.service.alambizplugin.commproperties.TempFileProperties;
|
|
|
+import com.shkpr.service.alambizplugin.constants.LogFlagBusiType;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.UncheckedIOException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.time.Duration;
|
|
|
+import java.time.Instant;
|
|
|
+import java.util.stream.Stream;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 临时文件配置
|
|
|
+ *
|
|
|
+ * @author 欧阳劲驰
|
|
|
+ * @since 1.0.0
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+@EnableScheduling
|
|
|
+public class TempFileConfiguration {
|
|
|
+ /**
|
|
|
+ * log
|
|
|
+ */
|
|
|
+ private final String mStrClassName;
|
|
|
+ private final String mBizType;
|
|
|
+
|
|
|
+ private final TempFileProperties tempFileProperties;
|
|
|
+
|
|
|
+ public TempFileConfiguration(TempFileProperties tempFileProperties) {
|
|
|
+ mStrClassName = "TempFileConfiguration";
|
|
|
+ mBizType = LogFlagBusiType.BUSI_GIS_SURVEY.toStrValue();
|
|
|
+ this.tempFileProperties = tempFileProperties;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Scheduled(fixedRateString = "${temp-file.check-cycle}")
|
|
|
+ public void scheduled() {
|
|
|
+ //读取子文件
|
|
|
+ try (Stream<Path> list = Files.list(tempFileProperties.getResourcePath())) {
|
|
|
+ //遍历子文件
|
|
|
+ list.forEach(path -> {
|
|
|
+ try {
|
|
|
+ //获取文件存在时间
|
|
|
+ Instant fileInstant = Files.getLastModifiedTime(path).toInstant();
|
|
|
+ Duration fileDuration = Duration.between(fileInstant, Instant.now());
|
|
|
+ //比较生命周期,如大于生命周期,则删除
|
|
|
+ Duration lifecycle = tempFileProperties.getLifecycle();
|
|
|
+ if (fileDuration.compareTo(lifecycle) > 0)
|
|
|
+ if (Files.isDirectory(path)) {
|
|
|
+ //删除文件夹
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
|
|
|
+ , String.format(
|
|
|
+ "清理临时文件夹;路径:%s 存在时间:%s 生命周期:%s"
|
|
|
+ , path
|
|
|
+ , fileDuration
|
|
|
+ , lifecycle
|
|
|
+ )
|
|
|
+ );
|
|
|
+ FileUtils.deleteDirectory(path.toFile());
|
|
|
+ } else if (Files.isRegularFile(path)) {
|
|
|
+ //删除文件
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
|
|
|
+ , String.format(
|
|
|
+ "清理临时文件;路径:%s 存在时间:%s 生命周期:%s"
|
|
|
+ , path
|
|
|
+ , fileDuration
|
|
|
+ , lifecycle
|
|
|
+ )
|
|
|
+ );
|
|
|
+ FileUtils.delete(path.toFile());
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new UncheckedIOException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ } catch (IOException | UncheckedIOException e) {
|
|
|
+ //打印报错信息
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
|
|
|
+ , String.format("清理临时文件失败 error:%s", e)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|