make all ThreadPool static final (#3243)
* make all ThreadPool static final * update github workflow
This commit is contained in:
parent
ae42ddd35f
commit
94f92ea635
|
|
@ -20,11 +20,11 @@ jobs:
|
|||
uses: actions/setup-java@v3
|
||||
with:
|
||||
java-version: ${{ matrix.java }}
|
||||
distribution: 'adopt'
|
||||
distribution: 'temurin'
|
||||
architecture: x64
|
||||
|
||||
- name: Test with Maven
|
||||
run: mvn test
|
||||
run: mvn --batch-mode test
|
||||
|
||||
- name: Build with Maven
|
||||
run: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V -DminimumPriority=1
|
||||
|
|
|
|||
|
|
@ -18,14 +18,12 @@ package com.alibaba.csp.sentinel.log.jul;
|
|||
import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.RejectedExecutionHandler;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.logging.Formatter;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.StreamHandler;
|
||||
|
||||
/**
|
||||
* This Handler publishes log records to console by using {@link java.util.logging.StreamHandler}.
|
||||
|
|
@ -41,6 +39,19 @@ import java.util.logging.*;
|
|||
* @author cdfive
|
||||
*/
|
||||
class ConsoleHandler extends Handler {
|
||||
private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(
|
||||
1,
|
||||
5,
|
||||
1,
|
||||
TimeUnit.HOURS,
|
||||
new ArrayBlockingQueue<>(1024),
|
||||
new NamedThreadFactory("sentinel-console-log-executor", true),
|
||||
new LogRejectedExecutionHandler()
|
||||
);
|
||||
|
||||
static {
|
||||
executor.allowCoreThreadTimeOut(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* A Handler which publishes log records to System.out.
|
||||
|
|
@ -52,21 +63,11 @@ class ConsoleHandler extends Handler {
|
|||
*/
|
||||
private StreamHandler stderrHandler;
|
||||
|
||||
private ExecutorService executor;
|
||||
private AtomicReference<Future<?>> lastFuture = new AtomicReference<>();
|
||||
|
||||
public ConsoleHandler() {
|
||||
this.stdoutHandler = new StreamHandler(System.out, new CspFormatter());
|
||||
this.stderrHandler = new StreamHandler(System.err, new CspFormatter());
|
||||
|
||||
int corePoolSize = 1;
|
||||
int maximumPoolSize = 1;
|
||||
long keepAliveTime = 0;
|
||||
/**insure the log can be recorded*/
|
||||
int queueSize = 1024;
|
||||
RejectedExecutionHandler handler = new LogRejectedExecutionHandler();
|
||||
executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
|
||||
keepAliveTime, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(queueSize),
|
||||
new NamedThreadFactory("sentinel-console-log-executor", true), handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -83,7 +84,7 @@ class ConsoleHandler extends Handler {
|
|||
|
||||
@Override
|
||||
public void publish(LogRecord record) {
|
||||
executor.execute(new LogTask(record,stdoutHandler,stderrHandler));
|
||||
lastFuture.set(executor.submit(new LogTask(record, stdoutHandler, stderrHandler)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -94,16 +95,18 @@ class ConsoleHandler extends Handler {
|
|||
|
||||
@Override
|
||||
public void close() throws SecurityException {
|
||||
/**not need to record log if process is killed.*/
|
||||
executor.shutdown();
|
||||
Future<?> future = lastFuture.get();
|
||||
if (future != null) {
|
||||
try {
|
||||
future.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
stdoutHandler.close();
|
||||
stderrHandler.close();
|
||||
}
|
||||
|
||||
public ExecutorService getExecutor() {
|
||||
return executor;
|
||||
}
|
||||
|
||||
static class LogRejectedExecutionHandler implements RejectedExecutionHandler {
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -20,14 +20,11 @@ import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.RejectedExecutionHandler;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.FileHandler;
|
||||
import java.util.logging.Formatter;
|
||||
|
|
@ -43,7 +40,20 @@ class DateFileLogHandler extends Handler {
|
|||
}
|
||||
};
|
||||
|
||||
private ExecutorService executor;
|
||||
private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(
|
||||
1,
|
||||
5,
|
||||
1,
|
||||
TimeUnit.HOURS,
|
||||
new ArrayBlockingQueue<Runnable>(1024),
|
||||
new NamedThreadFactory("sentinel-datafile-log-executor", true),
|
||||
new ThreadPoolExecutor.DiscardOldestPolicy()
|
||||
);
|
||||
|
||||
static {
|
||||
// allow all thread could be stopped
|
||||
executor.allowCoreThreadTimeOut(true);
|
||||
}
|
||||
|
||||
private volatile FileHandler handler;
|
||||
|
||||
|
|
@ -66,22 +76,10 @@ class DateFileLogHandler extends Handler {
|
|||
this.append = append;
|
||||
rotateDate();
|
||||
this.initialized = true;
|
||||
|
||||
int corePoolSize = 1;
|
||||
int maximumPoolSize = 1;
|
||||
long keepAliveTime = 0;
|
||||
/**insure the log can be recorded*/
|
||||
int queueSize = 1024;
|
||||
RejectedExecutionHandler handler = new LogRejectedExecutionHandler();
|
||||
executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
|
||||
keepAliveTime, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(queueSize),
|
||||
new NamedThreadFactory("sentinel-datafile-log-executor", true), handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SecurityException {
|
||||
/**not need to record log if process is killed.*/
|
||||
executor.shutdown();
|
||||
handler.close();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -51,12 +51,7 @@ public class ConsoleHandlerTest {
|
|||
// Test INFO level, should log to stdout
|
||||
logRecord = new LogRecord(Level.INFO, "test info message");
|
||||
consoleHandler.publish(logRecord);
|
||||
try {
|
||||
consoleHandler.getExecutor().shutdown();
|
||||
consoleHandler.getExecutor().awaitTermination(1, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
consoleHandler.close();
|
||||
assertEquals(cspFormatter.format(logRecord), baosOut.toString());
|
||||
assertEquals("", baosErr.toString());
|
||||
|
|
@ -80,12 +75,7 @@ public class ConsoleHandlerTest {
|
|||
// Test INFO level, should log to stderr
|
||||
logRecord = new LogRecord(Level.WARNING, "test warning message");
|
||||
consoleHandler.publish(logRecord);
|
||||
try {
|
||||
consoleHandler.getExecutor().shutdown();
|
||||
consoleHandler.getExecutor().awaitTermination(1, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
consoleHandler.close();
|
||||
assertEquals(cspFormatter.format(logRecord), baosErr.toString());
|
||||
assertEquals("", baosOut.toString());
|
||||
|
|
@ -111,12 +101,7 @@ public class ConsoleHandlerTest {
|
|||
// java.util.logging.StreamHandler.level=FINE
|
||||
logRecord = new LogRecord(Level.FINE, "test fine message");
|
||||
consoleHandler.publish(logRecord);
|
||||
try {
|
||||
consoleHandler.getExecutor().shutdown();
|
||||
consoleHandler.getExecutor().awaitTermination(1, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
consoleHandler.close();
|
||||
assertEquals("", baosOut.toString());
|
||||
assertEquals("", baosErr.toString());
|
||||
|
|
@ -139,12 +124,7 @@ public class ConsoleHandlerTest {
|
|||
// Test SEVERE level, should log to stderr
|
||||
logRecord = new LogRecord(Level.SEVERE, "test severe message");
|
||||
consoleHandler.publish(logRecord);
|
||||
try {
|
||||
consoleHandler.getExecutor().shutdown();
|
||||
consoleHandler.getExecutor().awaitTermination(1, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
consoleHandler.close();
|
||||
assertEquals(cspFormatter.format(logRecord), baosErr.toString());
|
||||
assertEquals("", baosOut.toString());
|
||||
|
|
|
|||
Loading…
Reference in New Issue