下面列出了怎么用org.apache.commons.io.filefilter.AgeFileFilter的API类实例代码及写法,或者点击链接到github查看源代码。
private void deleteBackupFilesOlderThen(File backupDir, int days){
try {
if (backupDir.isDirectory()) {
Collection<File> filesToDelete = FileUtils.listFiles(backupDir,
new AgeFileFilter(DateUtils.addDays(new Date(), days * -1)),
TrueFileFilter.TRUE);
for (File file : filesToDelete) {
boolean success = FileUtils.deleteQuietly(file);
if (!success) {
LoggerFactory.getLogger(getClass())
.warn("Cannot delete old backup file at: " + file.getAbsolutePath());
}
}
}
} catch (Exception e) {
LoggerFactory.getLogger(getClass()).warn("Cannot delete old backup files.", e);
}
}
public static void unlockLockedFilesOnQueue() {
final PropertyHandler propertyHandler = PropertyHandler.getInstance();
if (propertyHandler.hasProperty("MESSAGE_QUEUE_FOLDER")) {
String messageQueueFolderPath = propertyHandler.getProperty("MESSAGE_QUEUE_FOLDER");
File messageQueueFolder = new File(messageQueueFolderPath);
if (messageQueueFolder.exists()) {
String lockedFileSuffix = "_LOCK";
SuffixFileFilter suffixFileFilter = new SuffixFileFilter(lockedFileSuffix);
Integer numberOfMinutes = propertyHandler.getIntegerProperty("locked.file.retention", "2");
AgeFileFilter ageFileFilter = new AgeFileFilter(System.currentTimeMillis() - (numberOfMinutes * 60 * 1000));
Collection<File> lockedFiles = FileUtils.listFiles(messageQueueFolder, FileFilterUtils.and(suffixFileFilter, ageFileFilter), TrueFileFilter.INSTANCE);
for (File file : lockedFiles) {
String lockedFileName = file.getAbsolutePath();
File unlockedFile = new File(StringUtils.remove(lockedFileName, lockedFileSuffix));
file.setLastModified(new Date().getTime());
Boolean succesFullyUnlocked = file.renameTo(unlockedFile);
if (succesFullyUnlocked) {
LOG.info("File: " + lockedFileName + " successfully unlocked.");
}
}
} else {
LOG.info("No directory found on location: " + messageQueueFolderPath + ". No files unlocked");
}
} else {
LOG.info("No MESSAGE_QUEUE_FOLDER property in properties file. No files unlocked.");
}
}
public static void unlockLockedFilesOnQueue() {
PropertyHandler propertyHandler = PropertyHandler.getInstance();
if (propertyHandler.hasProperty("MESSAGE_QUEUE_FOLDER")) {
String messageQueueFolderPath = propertyHandler.getProperty("MESSAGE_QUEUE_FOLDER");
File messageQueueFolder = new File(messageQueueFolderPath);
if (messageQueueFolder.exists()) {
String lockedFileSuffix = "_LOCK";
SuffixFileFilter suffixFileFilter = new SuffixFileFilter(lockedFileSuffix);
Integer numberOfMinutes = propertyHandler.getIntegerProperty("locked.file.retention", "2");
AgeFileFilter ageFileFilter = new AgeFileFilter(System.currentTimeMillis() - (long)(numberOfMinutes.intValue() * 60 * 1000));
Collection<File> lockedFiles = FileUtils.listFiles(messageQueueFolder, FileFilterUtils.and(suffixFileFilter, ageFileFilter), TrueFileFilter.INSTANCE);
Iterator var9 = lockedFiles.iterator();
while(var9.hasNext()) {
File file = (File)var9.next();
String lockedFileName = file.getAbsolutePath();
File unlockedFile = new File(StringUtils.remove(lockedFileName, lockedFileSuffix));
file.setLastModified((new Date()).getTime());
Boolean succesFullyUnlocked = file.renameTo(unlockedFile);
if (succesFullyUnlocked.booleanValue()) {
LOG.info("File: " + lockedFileName + " successfully unlocked.");
}
}
} else {
LOG.info("No directory found on location: " + messageQueueFolderPath + ". No files unlocked");
}
} else {
LOG.info("No MESSAGE_QUEUE_FOLDER property in properties file. No files unlocked.");
}
}
public static void unlockLockedFilesOnQueue() {
final PropertyHandler propertyHandler = PropertyHandler.getInstance();
if (propertyHandler.hasProperty("MESSAGE_QUEUE_FOLDER")) {
String messageQueueFolderPath = propertyHandler.getProperty("MESSAGE_QUEUE_FOLDER");
File messageQueueFolder = new File(messageQueueFolderPath);
if (messageQueueFolder.exists()) {
String lockedFileSuffix = "_LOCK";
SuffixFileFilter suffixFileFilter = new SuffixFileFilter(lockedFileSuffix);
Integer numberOfMinutes = propertyHandler.getIntegerProperty("locked.file.retention", "2");
AgeFileFilter ageFileFilter = new AgeFileFilter(System.currentTimeMillis() - (numberOfMinutes * 60 * 1000));
Collection<File> lockedFiles = FileUtils.listFiles(messageQueueFolder, FileFilterUtils.and(suffixFileFilter, ageFileFilter), TrueFileFilter.INSTANCE);
for (File file : lockedFiles) {
String lockedFileName = file.getAbsolutePath();
File unlockedFile = new File(StringUtils.remove(lockedFileName, lockedFileSuffix));
file.setLastModified(new Date().getTime());
Boolean succesFullyUnlocked = file.renameTo(unlockedFile);
if (succesFullyUnlocked) {
LOG.info("File: " + lockedFileName + " successfully unlocked.");
}
}
} else {
LOG.info("No directory found on location: " + messageQueueFolderPath + ". No files unlocked");
}
} else {
LOG.info("No MESSAGE_QUEUE_FOLDER property in properties file. No files unlocked.");
}
}
public static void unlockLockedFilesOnQueue() {
final PropertyHandler propertyHandler = PropertyHandler.getInstance();
if (propertyHandler.hasProperty("MESSAGE_QUEUE_FOLDER")) {
String messageQueueFolderPath = propertyHandler.getProperty("MESSAGE_QUEUE_FOLDER");
File messageQueueFolder = new File(messageQueueFolderPath);
if (messageQueueFolder.exists()) {
String lockedFileSuffix = "_LOCK";
SuffixFileFilter suffixFileFilter = new SuffixFileFilter(lockedFileSuffix);
Integer numberOfMinutes = propertyHandler.getIntegerProperty("locked.file.retention", "2");
AgeFileFilter ageFileFilter = new AgeFileFilter(System.currentTimeMillis() - (numberOfMinutes * 60 * 1000));
Collection<File> lockedFiles = FileUtils.listFiles(messageQueueFolder, FileFilterUtils.and(suffixFileFilter, ageFileFilter), TrueFileFilter.INSTANCE);
for (File file : lockedFiles) {
String lockedFileName = file.getAbsolutePath();
File unlockedFile = new File(StringUtils.remove(lockedFileName, lockedFileSuffix));
file.setLastModified(new Date().getTime());
Boolean succesFullyUnlocked = file.renameTo(unlockedFile);
if (succesFullyUnlocked) {
LOG.info("File: " + lockedFileName + " successfully unlocked.");
}
}
} else {
LOG.info("No directory found on location: " + messageQueueFolderPath + ". No files unlocked");
}
} else {
LOG.info("No MESSAGE_QUEUE_FOLDER property in properties file. No files unlocked.");
}
}
void cleanup() {
String taskLogsHome = getTaskLogHome();
if (taskLogsHome == null) {
// we are forgiving if the task logs home is not defined. Just log a message with a call to action.
log.warn("Unable to cleanup task log files. Please check that the 'tasklogfile' appender exists in logback.xml");
return;
}
File logFilesHome = new File(taskLogsHome);
log.info("Cleaning up log files in {} older than {} days", logFilesHome.getAbsolutePath(), numberOfDays);
LocalDate now = LocalDate.now().minusDays(numberOfDays);
Date thresholdDate = Date.from(now.atStartOfDay(ZoneId.systemDefault()).toInstant());
AgeFileFilter ageFileFilter = new AgeFileFilter(thresholdDate);
Iterator<File> filesToDelete = iterateFiles(logFilesHome, ageFileFilter, ageFileFilter);
filesToDelete.forEachRemaining(f -> {
try {
forceDelete(f);
log.info("Removed task log file {}", f.toString());
}
catch (IOException e) { // NOSONAR
log.error("Unable to delete task file {}. Message was {}.", f.toString(), e.getMessage());
}
}
);
}
/**
* Walks the baseDir recursively and deletes files and directories older than cutoff.
* When traversing directories, does not follow symbolic links.
*
* If a directory is old but contains files (which are not too old), it is not deleted.
*
* TODO better problem handling?
*
* @param baseDir
* @param cutoff milliseconds
* @throws IOException
*/
public static void cleanOldFiles(File baseDir, long cutoff ) throws IOException {
walkAndDelete(baseDir, new AgeFileFilter(System.currentTimeMillis() - cutoff));
}