You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/05/08 12:48:46 UTC

[commons-io] branch master updated (0f6f414b -> c93030ac)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


    from 0f6f414b Format tweak
     new 9577adbc Better internal method name
     new c93030ac Allow start the tailer thread to start on a custom executor service.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../commons/io/input/ReadAheadInputStream.java     | 20 ++++++-------
 .../java/org/apache/commons/io/input/Tailer.java   | 33 ++++++++++++++++++++--
 .../org/apache/commons/io/input/TailerTest.java    |  7 ++++-
 3 files changed, 46 insertions(+), 14 deletions(-)


[commons-io] 02/02: Allow start the tailer thread to start on a custom executor service.

Posted by gg...@apache.org.
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 <ga...@gmail.com>
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);
         }


[commons-io] 01/02: Better internal method name

Posted by gg...@apache.org.
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 9577adbc5fd701c84087bb3b627bddbe10bea420
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun May 7 14:55:48 2023 -0400

    Better internal method name
---
 .../commons/io/input/ReadAheadInputStream.java       | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java b/src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java
index 72e06335..7d03a5a8 100644
--- a/src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java
@@ -100,27 +100,27 @@ public class ReadAheadInputStream extends FilterInputStream {
         return new Builder();
     }
 
-    /**
-     * Creates a new daemon executor service.
-     *
-     * @return a new daemon executor service.
-     */
-    private static ExecutorService newExecutorService() {
-        return Executors.newSingleThreadExecutor(ReadAheadInputStream::newThread);
-    }
-
     /**
      * Creates a new daemon thread.
      *
      * @param r the thread's runnable.
      * @return a new daemon thread.
      */
-    private static Thread newThread(final Runnable r) {
+    private static Thread newDaemonThread(final Runnable r) {
         final Thread thread = new Thread(r, "commons-io-read-ahead");
         thread.setDaemon(true);
         return thread;
     }
 
+    /**
+     * Creates a new daemon executor service.
+     *
+     * @return a new daemon executor service.
+     */
+    private static ExecutorService newExecutorService() {
+        return Executors.newSingleThreadExecutor(ReadAheadInputStream::newDaemonThread);
+    }
+
     private final ReentrantLock stateChangeLock = new ReentrantLock();
 
     // @GuardedBy("stateChangeLock")