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

wusheng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-java.git


The following commit(s) were added to refs/heads/main by this push:
     new 35388ac062 Fix the apm-jdk-threadpool-plugin does not work in certain 
scenarios (#556)
35388ac062 is described below

commit 35388ac062385815de57a900b2c9f0a3f1cb05ca
Author: xiaqi1210 <[email protected]>
AuthorDate: Fri Jun 16 21:26:34 2023 +0800

    Fix the apm-jdk-threadpool-plugin does not work in certain scenarios (#556)
---
 .../apm/agent/core/logging/core/FileWriter.java    | 70 ++++++++++++----------
 1 file changed, 40 insertions(+), 30 deletions(-)

diff --git 
a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/logging/core/FileWriter.java
 
b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/logging/core/FileWriter.java
index eed675055b..4020b7a98a 100644
--- 
a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/logging/core/FileWriter.java
+++ 
b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/logging/core/FileWriter.java
@@ -18,6 +18,10 @@
 
 package org.apache.skywalking.apm.agent.core.logging.core;
 
+import org.apache.skywalking.apm.agent.core.conf.Config;
+import org.apache.skywalking.apm.agent.core.conf.Constants;
+import org.apache.skywalking.apm.util.RunnableWithExceptionProtection;
+
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
@@ -30,13 +34,8 @@ import java.util.Comparator;
 import java.util.Date;
 import java.util.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.Callable;
-import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Pattern;
-import org.apache.skywalking.apm.agent.core.boot.DefaultNamedThreadFactory;
-import org.apache.skywalking.apm.agent.core.conf.Config;
-import org.apache.skywalking.apm.agent.core.conf.Constants;
-import org.apache.skywalking.apm.util.RunnableWithExceptionProtection;
 
 /**
  * The <code>FileWriter</code> support async file output, by using a queue as 
buffer.
@@ -63,31 +62,42 @@ public class FileWriter implements IWriter {
     private FileWriter() {
         logBuffer = new ArrayBlockingQueue(1024);
         final ArrayList<String> outputLogs = new ArrayList<String>(200);
-        Executors.newSingleThreadScheduledExecutor(new 
DefaultNamedThreadFactory("LogFileWriter"))
-                 .scheduleAtFixedRate(new RunnableWithExceptionProtection(new 
Runnable() {
-                     @Override
-                     public void run() {
-                         try {
-                             logBuffer.drainTo(outputLogs);
-                             for (String log : outputLogs) {
-                                 writeToFile(log + Constants.LINE_SEPARATOR);
-                             }
-                             try {
-                                 if (fileOutputStream != null) {
-                                     fileOutputStream.flush();
-                                 }
-                             } catch (IOException e) {
-                                 e.printStackTrace();
-                             }
-                         } finally {
-                             outputLogs.clear();
-                         }
-                     }
-                 }, new 
RunnableWithExceptionProtection.CallbackWhenException() {
-                     @Override
-                     public void handle(Throwable t) {
-                     }
-                 }), 0, 1, TimeUnit.SECONDS);
+        Thread logFlusherThread = new Thread(new 
RunnableWithExceptionProtection(new Runnable() {
+
+            @Override
+            public void run() {
+                while (true) {
+                    // flush log to file
+                    try {
+                        logBuffer.drainTo(outputLogs);
+                        for (String log : outputLogs) {
+                            writeToFile(log + Constants.LINE_SEPARATOR);
+                        }
+                        try {
+                            if (fileOutputStream != null) {
+                                fileOutputStream.flush();
+                            }
+                        } catch (IOException e) {
+                            e.printStackTrace();
+                        }
+                    } finally {
+                        outputLogs.clear();
+                    }
+
+                    // flush log once per second
+                    try {
+                        TimeUnit.SECONDS.sleep(1);
+                    } catch (InterruptedException e) {
+                    }
+                }
+
+            }
+        }, new RunnableWithExceptionProtection.CallbackWhenException() {
+            @Override
+            public void handle(Throwable t) {
+            }
+        }), "SkywalkingAgent-LogFileWriter");
+        logFlusherThread.start();
     }
 
     /**

Reply via email to