yanps
2 months ago
12 changed files with 151 additions and 54 deletions
@ -0,0 +1,78 @@ |
|||||
|
package org.nl.wms.sch.task_manage; |
||||
|
|
||||
|
import cn.hutool.core.util.ObjectUtil; |
||||
|
import lombok.SneakyThrows; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.nl.config.SpringContextHolder; |
||||
|
import org.nl.system.service.param.dao.Param; |
||||
|
import org.nl.system.service.param.impl.SysParamServiceImpl; |
||||
|
import org.reflections.Reflections; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; |
||||
|
import org.springframework.context.annotation.Profile; |
||||
|
import org.springframework.core.io.ClassPathResource; |
||||
|
import org.springframework.core.io.Resource; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.io.File; |
||||
|
import java.io.FileInputStream; |
||||
|
import java.io.IOException; |
||||
|
import java.util.Properties; |
||||
|
import java.util.concurrent.TimeUnit; |
||||
|
|
||||
|
/** |
||||
|
* @author LENOVO |
||||
|
* 定时清理日志文件 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Component |
||||
|
@Profile("prod") |
||||
|
public class AutoCleanFile { |
||||
|
|
||||
|
private static long DAYS_TO_MILLIS = TimeUnit.DAYS.toMillis(14); |
||||
|
|
||||
|
@Value("${logging.file.path}") |
||||
|
private String logPath; |
||||
|
|
||||
|
@SneakyThrows |
||||
|
public void run() { |
||||
|
log.info("定时清理日志文件AutoCleanFile开始:"); |
||||
|
File directory = new File(logPath); |
||||
|
if (directory.exists() && directory.isDirectory()) { |
||||
|
cleanDirectory(directory); |
||||
|
} else { |
||||
|
System.out.println("指定的路径不存在或不是一个目录。"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 递归清理文件夹内的过期文件
|
||||
|
private static void cleanDirectory(File directory) { |
||||
|
File[] files = directory.listFiles(); |
||||
|
if (files != null) { |
||||
|
long currentTime = System.currentTimeMillis(); |
||||
|
for (File file : files) { |
||||
|
if (file.isDirectory()) { |
||||
|
// 如果是目录,递归调用
|
||||
|
cleanDirectory(file); |
||||
|
} else if (file.isFile()) { |
||||
|
SysParamServiceImpl sysParamService = SpringContextHolder.getBean(SysParamServiceImpl.class); |
||||
|
Param byCode = sysParamService.findByCode(GeneralDefinition.LOGIN_TIME); |
||||
|
if (ObjectUtil.isNotNull(byCode)) { |
||||
|
DAYS_TO_MILLIS = TimeUnit.DAYS.toMillis(Long.parseLong(byCode.getValue())); |
||||
|
} |
||||
|
if (currentTime - file.lastModified() > DAYS_TO_MILLIS) { |
||||
|
boolean deleted = file.delete(); |
||||
|
if (deleted) { |
||||
|
log.info("已删除文件: " + file.getAbsolutePath()); |
||||
|
} else { |
||||
|
log.info("无法删除文件: " + file.getAbsolutePath()); |
||||
|
} |
||||
|
} else { |
||||
|
log.info("文件未超过七天, 不做任何修改: " + file.getAbsolutePath()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue