Skip to content

Commit 35388ac

Browse files
authored
Fix the apm-jdk-threadpool-plugin does not work in certain scenarios (#556)
1 parent 6621f82 commit 35388ac

1 file changed

Lines changed: 40 additions & 30 deletions

File tree

  • apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/logging/core

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/logging/core/FileWriter.java

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
package org.apache.skywalking.apm.agent.core.logging.core;
2020

21+
import org.apache.skywalking.apm.agent.core.conf.Config;
22+
import org.apache.skywalking.apm.agent.core.conf.Constants;
23+
import org.apache.skywalking.apm.util.RunnableWithExceptionProtection;
24+
2125
import java.io.File;
2226
import java.io.FileNotFoundException;
2327
import java.io.FileOutputStream;
@@ -30,13 +34,8 @@
3034
import java.util.Date;
3135
import java.util.concurrent.ArrayBlockingQueue;
3236
import java.util.concurrent.Callable;
33-
import java.util.concurrent.Executors;
3437
import java.util.concurrent.TimeUnit;
3538
import java.util.regex.Pattern;
36-
import org.apache.skywalking.apm.agent.core.boot.DefaultNamedThreadFactory;
37-
import org.apache.skywalking.apm.agent.core.conf.Config;
38-
import org.apache.skywalking.apm.agent.core.conf.Constants;
39-
import org.apache.skywalking.apm.util.RunnableWithExceptionProtection;
4039

4140
/**
4241
* The <code>FileWriter</code> support async file output, by using a queue as buffer.
@@ -63,31 +62,42 @@ public static FileWriter get() {
6362
private FileWriter() {
6463
logBuffer = new ArrayBlockingQueue(1024);
6564
final ArrayList<String> outputLogs = new ArrayList<String>(200);
66-
Executors.newSingleThreadScheduledExecutor(new DefaultNamedThreadFactory("LogFileWriter"))
67-
.scheduleAtFixedRate(new RunnableWithExceptionProtection(new Runnable() {
68-
@Override
69-
public void run() {
70-
try {
71-
logBuffer.drainTo(outputLogs);
72-
for (String log : outputLogs) {
73-
writeToFile(log + Constants.LINE_SEPARATOR);
74-
}
75-
try {
76-
if (fileOutputStream != null) {
77-
fileOutputStream.flush();
78-
}
79-
} catch (IOException e) {
80-
e.printStackTrace();
81-
}
82-
} finally {
83-
outputLogs.clear();
84-
}
85-
}
86-
}, new RunnableWithExceptionProtection.CallbackWhenException() {
87-
@Override
88-
public void handle(Throwable t) {
89-
}
90-
}), 0, 1, TimeUnit.SECONDS);
65+
Thread logFlusherThread = new Thread(new RunnableWithExceptionProtection(new Runnable() {
66+
67+
@Override
68+
public void run() {
69+
while (true) {
70+
// flush log to file
71+
try {
72+
logBuffer.drainTo(outputLogs);
73+
for (String log : outputLogs) {
74+
writeToFile(log + Constants.LINE_SEPARATOR);
75+
}
76+
try {
77+
if (fileOutputStream != null) {
78+
fileOutputStream.flush();
79+
}
80+
} catch (IOException e) {
81+
e.printStackTrace();
82+
}
83+
} finally {
84+
outputLogs.clear();
85+
}
86+
87+
// flush log once per second
88+
try {
89+
TimeUnit.SECONDS.sleep(1);
90+
} catch (InterruptedException e) {
91+
}
92+
}
93+
94+
}
95+
}, new RunnableWithExceptionProtection.CallbackWhenException() {
96+
@Override
97+
public void handle(Throwable t) {
98+
}
99+
}), "SkywalkingAgent-LogFileWriter");
100+
logFlusherThread.start();
91101
}
92102

93103
/**

0 commit comments

Comments
 (0)