From 73c900d4e862bb36ca39738da3cb6641fa39dded Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 27 Aug 2020 16:26:08 -0700 Subject: [PATCH] Add tryDelete(:File) method to try to delete a given File in a given number of attempts and wait duration between attempts. --- ...eResourceCollectorApplicationListener.java | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/context/event/GemFireResourceCollectorApplicationListener.java b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/context/event/GemFireResourceCollectorApplicationListener.java index af22e1a..75da9f8 100644 --- a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/context/event/GemFireResourceCollectorApplicationListener.java +++ b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/context/event/GemFireResourceCollectorApplicationListener.java @@ -17,6 +17,7 @@ package org.springframework.data.gemfire.tests.integration.context.event; import java.io.File; import java.io.FileFilter; +import java.time.Duration; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; @@ -36,6 +37,7 @@ import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.data.gemfire.tests.integration.annotation.GemFireResourceCollectorConfiguration; import org.springframework.data.gemfire.tests.util.FileSystemUtils; +import org.springframework.data.gemfire.tests.util.ThreadUtils; import org.springframework.data.gemfire.util.ArrayUtils; import org.springframework.data.gemfire.util.CollectionUtils; import org.springframework.lang.NonNull; @@ -66,6 +68,10 @@ import org.slf4j.LoggerFactory; public class GemFireResourceCollectorApplicationListener implements ApplicationContextAware, ApplicationListener { + protected static final int DEFAULT_DELETE_ATTEMPTS = 2; + + protected static final long DEFAULT_DELETE_TIMED_WAIT_INTERVAL = Duration.ofMillis(250).toMillis(); + protected static final File DEFAULT_SEARCH_DIRECTORY = FileSystemUtils.WORKING_DIRECTORY; /** @@ -346,7 +352,7 @@ public class GemFireResourceCollectorApplicationListener FileSystemUtils.deleteRecursive(file); } else { - file.delete(); + tryDelete(file); } } @@ -389,6 +395,45 @@ public class GemFireResourceCollectorApplicationListener return this; } + /** + * Tries to delete a {@link File} refering to a single {@link File#isFile() "file"} (not a {@literal directory}) + * in the file system. + * + * @param file {@link File} to delete. + * @return a boolean value indicating whether the given {@link File} was successfully deleted. + * @see #tryDelete(File, int, long) + * @see java.io.File + */ + protected boolean tryDelete(File file) { + return tryDelete(file, DEFAULT_DELETE_ATTEMPTS, DEFAULT_DELETE_TIMED_WAIT_INTERVAL); + } + + /** + * Tries to delete a {@link File} refering to a single {@link File#isFile() "file"} (not a {@literal directory}) + * in the file system. + * + * @param file {@link File} to delete. + * @param attempts {@link Integer} indicating the number of attemps to try and delete the {@link File}. + * @param waitDurationBetweenAttempts {@link Long} indicating the number of milliseconds to wait + * between delete attempts. + * @return a boolean value indicating whether the given {@link File} was successfully deleted. + * @see #tryDelete(File, int, long) + * @see java.io.File + */ + @SuppressWarnings("all") + protected boolean tryDelete(File file, int attempts, long waitDurationBetweenAttempts) { + + if (FileSystemUtils.isFile(file)) { + + long interval = waitDurationBetweenAttempts; + long duration = attempts * interval; + + return ThreadUtils.timedWait(duration, interval, file::delete); + } + + return false; + } + /** * {@link FileFilter} implementation matching {@link File directories} or GemFire/Geode resources (e.g. files). */