This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
commit c93030ac0d20a7bc455207cca04b404187b310a1 Author: Gary Gregory <[email protected]> AuthorDate: Mon May 8 08:48:41 2023 -0400 Allow start the tailer thread to start on a custom executor service. TODO: The work is scheduled manually but at least you can allocate the thread from an application thread pool. Consider using a scheduled executor service as a future enhancement. --- .../java/org/apache/commons/io/input/Tailer.java | 33 ++++++++++++++++++++-- .../org/apache/commons/io/input/TailerTest.java | 7 ++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/commons/io/input/Tailer.java b/src/main/java/org/apache/commons/io/input/Tailer.java index 121f981c..a58d20f2 100644 --- a/src/main/java/org/apache/commons/io/input/Tailer.java +++ b/src/main/java/org/apache/commons/io/input/Tailer.java @@ -34,6 +34,8 @@ import java.nio.file.attribute.FileTime; import java.time.Duration; import java.util.Arrays; import java.util.Objects; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import org.apache.commons.io.IOUtils; import org.apache.commons.io.ThreadUtils; @@ -160,6 +162,7 @@ public class Tailer implements Runnable, AutoCloseable { * .setPath(path) * .setCharset(StandardCharsets.UTF_8) * .setDelayDuration(Duration.ofSeconds(1)) + * .setExecutorService(Executors.newSingleThreadExecutor(Builder::newDaemonThread)) * .setReOpen(false) * .setStartThread(true) * .setTailable(tailable) @@ -172,25 +175,38 @@ public class Tailer implements Runnable, AutoCloseable { */ public static class Builder extends AbstractStreamBuilder<Tailer, Builder> { + /** + * Creates a new daemon thread. + * + * @param runnable the thread's runnable. + * @return a new daemon thread. + */ + private static Thread newDaemonThread(final Runnable runnable) { + final Thread thread = new Thread(runnable, "commons-io-tailer"); + thread.setDaemon(true); + return thread; + } + private Tailable tailable; private TailerListener tailerListener; private Duration delayDuration = Duration.ofMillis(DEFAULT_DELAY_MILLIS); private boolean end; private boolean reOpen; private boolean startThread = true; + private ExecutorService executorService = Executors.newSingleThreadExecutor(Builder::newDaemonThread); /** * Builds and starts a new configured instance. * + * The tailer is started if {@code startThread} is true. + * * @return a new configured instance. */ @Override public Tailer get() { final Tailer tailer = new Tailer(tailable, getCharset(), tailerListener, delayDuration, end, reOpen, getBufferSize()); if (startThread) { - final Thread thread = new Thread(tailer); - thread.setDaemon(true); - thread.start(); + executorService.submit(tailer); } return tailer; } @@ -206,6 +222,17 @@ public class Tailer implements Runnable, AutoCloseable { return this; } + /** + * Sets an executor service to use when startThread is true. + * + * @param executorService + * @return this + */ + public Builder setExecutorService(final ExecutorService executorService) { + this.executorService = executorService; + return this; + } + /** * Sets the origin. * diff --git a/src/test/java/org/apache/commons/io/input/TailerTest.java b/src/test/java/org/apache/commons/io/input/TailerTest.java index d35e9f92..a6315eba 100644 --- a/src/test/java/org/apache/commons/io/input/TailerTest.java +++ b/src/test/java/org/apache/commons/io/input/TailerTest.java @@ -43,6 +43,7 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; +import java.util.concurrent.Executors; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -244,7 +245,11 @@ public class TailerTest { final File file = new File(temporaryFolder, "tailer-create-with-delay-and-from-start-with-reopen-and-buffersize-and-charset.txt"); createFile(file, 0); final TestTailerListener listener = new TestTailerListener(1); - try (Tailer tailer = Tailer.builder().setTailable(new NonStandardTailable(file)).setTailerListener(listener).get()) { + try (Tailer tailer = Tailer.builder() + .setExecutorService(Executors.newSingleThreadExecutor()) + .setTailable(new NonStandardTailable(file)) + .setTailerListener(listener) + .get()) { assertTrue(tailer.getTailable() instanceof NonStandardTailable); validateTailer(listener, file); }
