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