线程池 ID 替换 Spring Bean Name.

pull/12/head
chen.ma 3 years ago
parent b9c928c75d
commit 483e3dd636

@ -1,7 +1,6 @@
package cn.hippo4j.starter.core; package cn.hippo4j.starter.core;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
@ -15,9 +14,9 @@ import java.util.concurrent.*;
*/ */
@Slf4j @Slf4j
public abstract class DynamicExecutorConfigurationSupport extends ThreadPoolExecutor public abstract class DynamicExecutorConfigurationSupport extends ThreadPoolExecutor
implements BeanNameAware, InitializingBean, DisposableBean { implements InitializingBean, DisposableBean {
private String beanName; private String threadPoolId;
private ExecutorService executor; private ExecutorService executor;
@ -32,9 +31,11 @@ public abstract class DynamicExecutorConfigurationSupport extends ThreadPoolExec
boolean waitForTasksToCompleteOnShutdown, boolean waitForTasksToCompleteOnShutdown,
long awaitTerminationMillis, long awaitTerminationMillis,
BlockingQueue<Runnable> workQueue, BlockingQueue<Runnable> workQueue,
String threadPoolId,
ThreadFactory threadFactory, ThreadFactory threadFactory,
RejectedExecutionHandler handler) { RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
this.threadPoolId = threadPoolId;
this.waitForTasksToCompleteOnShutdown = waitForTasksToCompleteOnShutdown; this.waitForTasksToCompleteOnShutdown = waitForTasksToCompleteOnShutdown;
this.awaitTerminationMillis = awaitTerminationMillis; this.awaitTerminationMillis = awaitTerminationMillis;
} }
@ -48,11 +49,6 @@ public abstract class DynamicExecutorConfigurationSupport extends ThreadPoolExec
*/ */
protected abstract ExecutorService initializeExecutor(); protected abstract ExecutorService initializeExecutor();
@Override
public void setBeanName(String name) {
this.beanName = name;
}
/** /**
* Calls {@code initialize()} after the container applied all property values. * Calls {@code initialize()} after the container applied all property values.
* *
@ -79,7 +75,7 @@ public abstract class DynamicExecutorConfigurationSupport extends ThreadPoolExec
*/ */
public void initialize() { public void initialize() {
if (log.isInfoEnabled()) { if (log.isInfoEnabled()) {
log.info("Initializing ExecutorService" + (this.beanName != null ? " '" + this.beanName + "'" : "")); log.info("Initializing ExecutorService" + (this.threadPoolId != null ? " '" + this.threadPoolId + "'" : ""));
} }
this.executor = initializeExecutor(); this.executor = initializeExecutor();
@ -93,7 +89,7 @@ public abstract class DynamicExecutorConfigurationSupport extends ThreadPoolExec
*/ */
public void shutdownSupport() { public void shutdownSupport() {
if (log.isInfoEnabled()) { if (log.isInfoEnabled()) {
log.info("Shutting down ExecutorService" + (this.beanName != null ? " '" + this.beanName + "'" : "")); log.info("Shutting down ExecutorService" + (this.threadPoolId != null ? " '" + this.threadPoolId + "'" : ""));
} }
if (this.executor != null) { if (this.executor != null) {
if (this.waitForTasksToCompleteOnShutdown) { if (this.waitForTasksToCompleteOnShutdown) {
@ -131,13 +127,13 @@ public abstract class DynamicExecutorConfigurationSupport extends ThreadPoolExec
if (!executor.awaitTermination(this.awaitTerminationMillis, TimeUnit.MILLISECONDS)) { if (!executor.awaitTermination(this.awaitTerminationMillis, TimeUnit.MILLISECONDS)) {
if (log.isWarnEnabled()) { if (log.isWarnEnabled()) {
log.warn("Timed out while waiting for executor" + log.warn("Timed out while waiting for executor" +
(this.beanName != null ? " '" + this.beanName + "'" : "") + " to terminate"); (this.threadPoolId != null ? " '" + this.threadPoolId + "'" : "") + " to terminate");
} }
} }
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
if (log.isWarnEnabled()) { if (log.isWarnEnabled()) {
log.warn("Interrupted while waiting for executor" + log.warn("Interrupted while waiting for executor" +
(this.beanName != null ? " '" + this.beanName + "'" : "") + " to terminate"); (this.threadPoolId != null ? " '" + this.threadPoolId + "'" : "") + " to terminate");
} }
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }

@ -72,7 +72,7 @@ public class DynamicThreadPoolExecutor extends DynamicExecutorConfigurationSuppo
@NonNull ThreadFactory threadFactory, @NonNull ThreadFactory threadFactory,
@NonNull ThreadPoolAlarm threadPoolAlarm, @NonNull ThreadPoolAlarm threadPoolAlarm,
@NonNull RejectedExecutionHandler handler) { @NonNull RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, waitForTasksToCompleteOnShutdown, awaitTerminationMillis, workQueue, threadFactory, handler); super(corePoolSize, maximumPoolSize, keepAliveTime, unit, waitForTasksToCompleteOnShutdown, awaitTerminationMillis, workQueue, threadPoolId, threadFactory, handler);
if (corePoolSize < 0 || if (corePoolSize < 0 ||
maximumPoolSize <= 0 || maximumPoolSize <= 0 ||

Loading…
Cancel
Save