|
@@ -2,7 +2,6 @@ package com.vber.common.core.config;
|
|
|
|
|
|
|
|
import com.vber.common.core.config.properties.ThreadPoolProperties;
|
|
import com.vber.common.core.config.properties.ThreadPoolProperties;
|
|
|
import com.vber.common.core.utils.SpringUtils;
|
|
import com.vber.common.core.utils.SpringUtils;
|
|
|
-import com.vber.common.core.utils.Threads;
|
|
|
|
|
import jakarta.annotation.PreDestroy;
|
|
import jakarta.annotation.PreDestroy;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
|
|
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
|
|
@@ -11,9 +10,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.core.task.VirtualThreadTaskExecutor;
|
|
import org.springframework.core.task.VirtualThreadTaskExecutor;
|
|
|
|
|
|
|
|
-import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
|
-import java.util.concurrent.ScheduledThreadPoolExecutor;
|
|
|
|
|
-import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
|
|
+import java.util.concurrent.*;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 线程池配置
|
|
* 线程池配置
|
|
@@ -24,7 +21,6 @@ import java.util.concurrent.ThreadPoolExecutor;
|
|
|
@AutoConfiguration
|
|
@AutoConfiguration
|
|
|
@EnableConfigurationProperties(ThreadPoolProperties.class)
|
|
@EnableConfigurationProperties(ThreadPoolProperties.class)
|
|
|
public class ThreadPoolConfig {
|
|
public class ThreadPoolConfig {
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 核心线程数 = cpu 核心数 + 1
|
|
* 核心线程数 = cpu 核心数 + 1
|
|
|
*/
|
|
*/
|
|
@@ -50,7 +46,7 @@ public class ThreadPoolConfig {
|
|
|
@Override
|
|
@Override
|
|
|
protected void afterExecute(Runnable r, Throwable t) {
|
|
protected void afterExecute(Runnable r, Throwable t) {
|
|
|
super.afterExecute(r, t);
|
|
super.afterExecute(r, t);
|
|
|
- Threads.printException(r, t);
|
|
|
|
|
|
|
+ printException(r, t);
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
this.scheduledExecutorService = scheduledThreadPoolExecutor;
|
|
this.scheduledExecutorService = scheduledThreadPoolExecutor;
|
|
@@ -59,15 +55,57 @@ public class ThreadPoolConfig {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 销毁事件
|
|
* 销毁事件
|
|
|
|
|
+ * 停止线程池
|
|
|
|
|
+ * 先使用shutdown, 停止接收新任务并尝试完成所有已存在任务.
|
|
|
|
|
+ * 如果超时, 则调用shutdownNow, 取消在workQueue中Pending的任务,并中断所有阻塞函数.
|
|
|
|
|
+ * 如果仍然超時,則強制退出.
|
|
|
|
|
+ * 另对在shutdown时线程本身被调用中断做了处理.
|
|
|
*/
|
|
*/
|
|
|
@PreDestroy
|
|
@PreDestroy
|
|
|
public void destroy() {
|
|
public void destroy() {
|
|
|
try {
|
|
try {
|
|
|
log.info("====关闭后台任务任务线程池====");
|
|
log.info("====关闭后台任务任务线程池====");
|
|
|
- Threads.shutdownAndAwaitTermination(scheduledExecutorService);
|
|
|
|
|
|
|
+ ScheduledExecutorService pool = scheduledExecutorService;
|
|
|
|
|
+ if (pool != null && !pool.isShutdown()) {
|
|
|
|
|
+ pool.shutdown();
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (!pool.awaitTermination(120, TimeUnit.SECONDS)) {
|
|
|
|
|
+ pool.shutdownNow();
|
|
|
|
|
+ if (!pool.awaitTermination(120, TimeUnit.SECONDS)) {
|
|
|
|
|
+ log.info("Pool did not terminate");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (InterruptedException ie) {
|
|
|
|
|
+ pool.shutdownNow();
|
|
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error(e.getMessage(), e);
|
|
log.error(e.getMessage(), e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 打印线程异常信息
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void printException(Runnable r, Throwable t) {
|
|
|
|
|
+ if (t == null && r instanceof Future<?>) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Future<?> future = (Future<?>) r;
|
|
|
|
|
+ if (future.isDone()) {
|
|
|
|
|
+ future.get();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (CancellationException ce) {
|
|
|
|
|
+ t = ce;
|
|
|
|
|
+ } catch (ExecutionException ee) {
|
|
|
|
|
+ t = ee.getCause();
|
|
|
|
|
+ } catch (InterruptedException ie) {
|
|
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (t != null) {
|
|
|
|
|
+ log.error(t.getMessage(), t);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|