diff --git a/hippo4j-core/.gitignore b/hippo4j-core/.gitignore
new file mode 100644
index 00000000..549e00a2
--- /dev/null
+++ b/hippo4j-core/.gitignore
@@ -0,0 +1,33 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
diff --git a/hippo4j-core/pom.xml b/hippo4j-core/pom.xml
new file mode 100644
index 00000000..7b146444
--- /dev/null
+++ b/hippo4j-core/pom.xml
@@ -0,0 +1,59 @@
+
+
+ 4.0.0
+
+ cn.hippo4j
+ hippo4j-all
+ ${revision}
+
+
+ hippo4j-core
+ ${project.artifactId}
+ ${project.artifactId}
+
+
+
+ org.projectlombok
+ lombok
+
+
+
+ cn.hippo4j
+ hippo4j-common
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-jar-plugin
+
+
+
+ ${project.artifactId}
+ ${project.version}
+ ${maven.build.timestamp}
+ chen.ma
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-javadoc-plugin
+ 2.10.3
+
+
+
+ jar
+
+
+
+
+
+
+
+
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/DynamicThreadPoolExecutor.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolExecutor.java
similarity index 69%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/DynamicThreadPoolExecutor.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolExecutor.java
index 12ad39f2..ccd24306 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/DynamicThreadPoolExecutor.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolExecutor.java
@@ -1,13 +1,14 @@
-package cn.hippo4j.starter.core;
+package cn.hippo4j.core.executor;
+import cn.hippo4j.core.executor.support.AbstractDynamicExecutorSupport;
+import cn.hippo4j.core.proxy.RejectedProxyUtil;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.springframework.core.task.TaskDecorator;
-import java.lang.reflect.Proxy;
import java.util.concurrent.*;
-import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
/**
* Dynamic threadPool wrap.
@@ -15,16 +16,21 @@ import java.util.concurrent.atomic.AtomicInteger;
* @author chen.ma
* @date 2021/7/8 21:47
*/
-public class DynamicThreadPoolExecutor extends DynamicExecutorConfigurationSupport {
+public class DynamicThreadPoolExecutor extends AbstractDynamicExecutorSupport {
@Getter
@Setter
private TaskDecorator taskDecorator;
+ @Getter
+ @Setter
+ private RejectedExecutionHandler redundancyHandler;
+
@Getter
private final String threadPoolId;
- private final AtomicInteger rejectCount = new AtomicInteger();
+ @Getter
+ private final AtomicLong rejectCount = new AtomicLong();
public DynamicThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
@@ -39,12 +45,12 @@ public class DynamicThreadPoolExecutor extends DynamicExecutorConfigurationSuppo
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, waitForTasksToCompleteOnShutdown, awaitTerminationMillis, workQueue, threadPoolId, threadFactory, handler);
this.threadPoolId = threadPoolId;
- RejectedExecutionHandler rejectedProxy = (RejectedExecutionHandler) Proxy
- .newProxyInstance(
- handler.getClass().getClassLoader(),
- new Class[]{RejectedExecutionHandler.class},
- new RejectedProxyInvocationHandler(handler, rejectCount));
+ // Number of dynamic proxy denial policies.
+ RejectedExecutionHandler rejectedProxy = RejectedProxyUtil.createProxy(handler, rejectCount);
setRejectedExecutionHandler(rejectedProxy);
+
+ // Redundant fields to avoid reflecting the acquired fields when sending change information.
+ redundancyHandler = handler;
}
@Override
@@ -66,7 +72,7 @@ public class DynamicThreadPoolExecutor extends DynamicExecutorConfigurationSuppo
*
* @return
*/
- public Integer getRejectCount() {
+ public Long getRejectCountNum() {
return rejectCount.get();
}
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/wrapper/DynamicThreadPoolWrapper.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolWrapper.java
similarity index 83%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/wrapper/DynamicThreadPoolWrapper.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolWrapper.java
index 438e6829..df4a23ab 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/wrapper/DynamicThreadPoolWrapper.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolWrapper.java
@@ -1,7 +1,7 @@
-package cn.hippo4j.starter.wrapper;
+package cn.hippo4j.core.executor;
-import cn.hippo4j.starter.common.CommonDynamicThreadPool;
-import cn.hippo4j.starter.core.DynamicExecutorConfigurationSupport;
+import cn.hippo4j.core.executor.support.AbstractDynamicExecutorSupport;
+import cn.hippo4j.core.executor.support.CommonDynamicThreadPool;
import lombok.Data;
import org.springframework.beans.factory.DisposableBean;
@@ -40,7 +40,7 @@ public class DynamicThreadPoolWrapper implements DisposableBean {
/**
* executor
- * {@link cn.hippo4j.starter.core.DynamicThreadPoolExecutor}
+ * {@link DynamicThreadPoolExecutor}
*/
private ThreadPoolExecutor executor;
@@ -96,8 +96,8 @@ public class DynamicThreadPoolWrapper implements DisposableBean {
@Override
public void destroy() throws Exception {
- if (executor != null && executor instanceof DynamicExecutorConfigurationSupport) {
- ((DynamicExecutorConfigurationSupport) executor).destroy();
+ if (executor != null && executor instanceof AbstractDynamicExecutorSupport) {
+ ((AbstractDynamicExecutorSupport) executor).destroy();
}
}
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/GlobalThreadPoolManage.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/manage/GlobalThreadPoolManage.java
similarity index 96%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/GlobalThreadPoolManage.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/executor/manage/GlobalThreadPoolManage.java
index 5b8c728e..f14cd514 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/GlobalThreadPoolManage.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/manage/GlobalThreadPoolManage.java
@@ -1,7 +1,7 @@
-package cn.hippo4j.starter.core;
+package cn.hippo4j.core.executor.manage;
import cn.hippo4j.common.model.PoolParameter;
-import cn.hippo4j.starter.wrapper.DynamicThreadPoolWrapper;
+import cn.hippo4j.core.executor.DynamicThreadPoolWrapper;
import com.google.common.collect.Lists;
import java.util.List;
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/AbstractBuildThreadPoolTemplate.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/AbstractBuildThreadPoolTemplate.java
similarity index 98%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/AbstractBuildThreadPoolTemplate.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/AbstractBuildThreadPoolTemplate.java
index 5751e47d..ceb712c8 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/AbstractBuildThreadPoolTemplate.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/AbstractBuildThreadPoolTemplate.java
@@ -1,7 +1,7 @@
-package cn.hippo4j.starter.toolkit.thread;
+package cn.hippo4j.core.executor.support;
import cn.hippo4j.common.toolkit.Assert;
-import cn.hippo4j.starter.core.DynamicThreadPoolExecutor;
+import cn.hippo4j.core.executor.DynamicThreadPoolExecutor;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
@@ -210,3 +210,4 @@ public class AbstractBuildThreadPoolTemplate {
}
}
+
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/DynamicExecutorConfigurationSupport.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/AbstractDynamicExecutorSupport.java
similarity index 82%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/DynamicExecutorConfigurationSupport.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/AbstractDynamicExecutorSupport.java
index a80ba92c..2c265bda 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/DynamicExecutorConfigurationSupport.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/AbstractDynamicExecutorSupport.java
@@ -1,4 +1,4 @@
-package cn.hippo4j.starter.core;
+package cn.hippo4j.core.executor.support;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
@@ -13,27 +13,27 @@ import java.util.concurrent.*;
* @date 2021/11/28 12:17
*/
@Slf4j
-public abstract class DynamicExecutorConfigurationSupport extends ThreadPoolExecutor
+public abstract class AbstractDynamicExecutorSupport extends ThreadPoolExecutor
implements InitializingBean, DisposableBean {
private String threadPoolId;
private ExecutorService executor;
- protected long awaitTerminationMillis;
+ public long awaitTerminationMillis;
- protected boolean waitForTasksToCompleteOnShutdown;
+ public boolean waitForTasksToCompleteOnShutdown;
- public DynamicExecutorConfigurationSupport(int corePoolSize,
- int maximumPoolSize,
- long keepAliveTime,
- TimeUnit unit,
- boolean waitForTasksToCompleteOnShutdown,
- long awaitTerminationMillis,
- BlockingQueue workQueue,
- String threadPoolId,
- ThreadFactory threadFactory,
- RejectedExecutionHandler handler) {
+ public AbstractDynamicExecutorSupport(int corePoolSize,
+ int maximumPoolSize,
+ long keepAliveTime,
+ TimeUnit unit,
+ boolean waitForTasksToCompleteOnShutdown,
+ long awaitTerminationMillis,
+ BlockingQueue workQueue,
+ String threadPoolId,
+ ThreadFactory threadFactory,
+ RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
this.threadPoolId = threadPoolId;
this.waitForTasksToCompleteOnShutdown = waitForTasksToCompleteOnShutdown;
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/common/CommonDynamicThreadPool.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/CommonDynamicThreadPool.java
similarity index 76%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/common/CommonDynamicThreadPool.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/CommonDynamicThreadPool.java
index 6d14b77e..54f6afdd 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/common/CommonDynamicThreadPool.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/CommonDynamicThreadPool.java
@@ -1,8 +1,6 @@
-package cn.hippo4j.starter.common;
+package cn.hippo4j.core.executor.support;
-import cn.hippo4j.starter.core.DynamicThreadPoolExecutor;
-import cn.hippo4j.starter.toolkit.thread.QueueTypeEnum;
-import cn.hippo4j.starter.toolkit.thread.ThreadPoolBuilder;
+import cn.hippo4j.core.executor.DynamicThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/FastThreadPoolExecutor.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/FastThreadPoolExecutor.java
similarity index 97%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/FastThreadPoolExecutor.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/FastThreadPoolExecutor.java
index d898bde5..200769d7 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/FastThreadPoolExecutor.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/FastThreadPoolExecutor.java
@@ -1,4 +1,4 @@
-package cn.hippo4j.starter.toolkit.thread;
+package cn.hippo4j.core.executor.support;
import lombok.extern.slf4j.Slf4j;
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/QueueTypeEnum.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/QueueTypeEnum.java
similarity index 87%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/QueueTypeEnum.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/QueueTypeEnum.java
index 57717d48..0552ed04 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/QueueTypeEnum.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/QueueTypeEnum.java
@@ -1,7 +1,7 @@
-package cn.hippo4j.starter.toolkit.thread;
+package cn.hippo4j.core.executor.support;
-import cn.hippo4j.starter.spi.DynamicTpServiceLoader;
-import cn.hippo4j.starter.spi.CustomBlockingQueue;
+import cn.hippo4j.core.spi.DynamicThreadPoolServiceLoader;
+import cn.hippo4j.core.spi.CustomBlockingQueue;
import java.util.Arrays;
import java.util.Collection;
@@ -62,9 +62,16 @@ public enum QueueTypeEnum {
}
static {
- DynamicTpServiceLoader.register(CustomBlockingQueue.class);
+ DynamicThreadPoolServiceLoader.register(CustomBlockingQueue.class);
}
+ /**
+ * Create blocking queue.
+ *
+ * @param type
+ * @param capacity
+ * @return
+ */
public static BlockingQueue createBlockingQueue(int type, Integer capacity) {
BlockingQueue blockingQueue = null;
if (Objects.equals(type, ARRAY_BLOCKING_QUEUE.type)) {
@@ -83,7 +90,7 @@ public enum QueueTypeEnum {
blockingQueue = new ResizableCapacityLinkedBlockIngQueue(capacity);
}
- Collection customBlockingQueues = DynamicTpServiceLoader
+ Collection customBlockingQueues = DynamicThreadPoolServiceLoader
.getSingletonServiceInstances(CustomBlockingQueue.class);
blockingQueue = Optional.ofNullable(blockingQueue).orElseGet(() -> customBlockingQueues.stream()
.filter(each -> Objects.equals(type, each.getType()))
@@ -94,6 +101,12 @@ public enum QueueTypeEnum {
return blockingQueue;
}
+ /**
+ * Get blocking queue name by type.
+ *
+ * @param type
+ * @return
+ */
public static String getBlockingQueueNameByType(int type) {
Optional queueTypeEnum = Arrays.stream(QueueTypeEnum.values())
.filter(each -> each.type == type)
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedPolicies.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/RejectedPolicies.java
similarity index 97%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedPolicies.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/RejectedPolicies.java
index d98e143b..8ed4c5e8 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedPolicies.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/RejectedPolicies.java
@@ -1,4 +1,4 @@
-package cn.hippo4j.starter.toolkit.thread;
+package cn.hippo4j.core.executor.support;
import lombok.extern.slf4j.Slf4j;
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedTypeEnum.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/RejectedTypeEnum.java
similarity index 85%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedTypeEnum.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/RejectedTypeEnum.java
index 5ec08a33..66fc0f4f 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedTypeEnum.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/RejectedTypeEnum.java
@@ -1,7 +1,7 @@
-package cn.hippo4j.starter.toolkit.thread;
+package cn.hippo4j.core.executor.support;
-import cn.hippo4j.starter.spi.CustomRejectedExecutionHandler;
-import cn.hippo4j.starter.spi.DynamicTpServiceLoader;
+import cn.hippo4j.core.spi.CustomRejectedExecutionHandler;
+import cn.hippo4j.core.spi.DynamicThreadPoolServiceLoader;
import java.util.Collection;
import java.util.Objects;
@@ -64,9 +64,15 @@ public enum RejectedTypeEnum {
}
static {
- DynamicTpServiceLoader.register(CustomRejectedExecutionHandler.class);
+ DynamicThreadPoolServiceLoader.register(CustomRejectedExecutionHandler.class);
}
+ /**
+ * Create policy.
+ *
+ * @param type
+ * @return
+ */
public static RejectedExecutionHandler createPolicy(int type) {
Optional rejectedTypeEnum = Stream.of(RejectedTypeEnum.values())
.filter(each -> Objects.equals(type, each.type))
@@ -75,7 +81,7 @@ public enum RejectedTypeEnum {
// 使用 SPI 匹配拒绝策略
RejectedExecutionHandler resultRejected = rejectedTypeEnum.orElseGet(() -> {
- Collection customRejectedExecutionHandlers = DynamicTpServiceLoader
+ Collection customRejectedExecutionHandlers = DynamicThreadPoolServiceLoader
.getSingletonServiceInstances(CustomRejectedExecutionHandler.class);
Optional customRejected = customRejectedExecutionHandlers.stream()
.filter(each -> Objects.equals(type, each.getType()))
@@ -88,6 +94,12 @@ public enum RejectedTypeEnum {
return resultRejected;
}
+ /**
+ * Get rejected name by type.
+ *
+ * @param type
+ * @return
+ */
public static String getRejectedNameByType(int type) {
return createPolicy(type).getClass().getSimpleName();
}
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/ResizableCapacityLinkedBlockIngQueue.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ResizableCapacityLinkedBlockIngQueue.java
similarity index 95%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/ResizableCapacityLinkedBlockIngQueue.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ResizableCapacityLinkedBlockIngQueue.java
index cc5340eb..ff900969 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/ResizableCapacityLinkedBlockIngQueue.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ResizableCapacityLinkedBlockIngQueue.java
@@ -1,4 +1,4 @@
-package cn.hippo4j.starter.toolkit.thread;
+package cn.hippo4j.core.executor.support;
import cn.hutool.core.util.ReflectUtil;
import lombok.extern.slf4j.Slf4j;
@@ -34,7 +34,6 @@ public class ResizableCapacityLinkedBlockIngQueue extends LinkedBlockingQueue
ReflectUtil.invoke(this, "signalNotFull");
}
} catch (Exception ex) {
- // ignore
log.error("Dynamic modification of blocking queue size failed.", ex);
successFlag = false;
}
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/TaskQueue.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/TaskQueue.java
similarity index 97%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/TaskQueue.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/TaskQueue.java
index f1de9ede..7bfd78ad 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/TaskQueue.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/TaskQueue.java
@@ -1,4 +1,4 @@
-package cn.hippo4j.starter.toolkit.thread;
+package cn.hippo4j.core.executor.support;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/ThreadFactoryBuilder.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ThreadFactoryBuilder.java
similarity index 99%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/ThreadFactoryBuilder.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ThreadFactoryBuilder.java
index ff1d5fea..4a395f77 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/ThreadFactoryBuilder.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ThreadFactoryBuilder.java
@@ -1,4 +1,4 @@
-package cn.hippo4j.starter.toolkit.thread;
+package cn.hippo4j.core.executor.support;
import cn.hippo4j.common.design.builder.Builder;
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/ThreadPoolBuilder.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ThreadPoolBuilder.java
similarity index 99%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/ThreadPoolBuilder.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ThreadPoolBuilder.java
index 039caca4..0e228bb4 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/ThreadPoolBuilder.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ThreadPoolBuilder.java
@@ -1,4 +1,4 @@
-package cn.hippo4j.starter.toolkit.thread;
+package cn.hippo4j.core.executor.support;
import cn.hippo4j.common.design.builder.Builder;
import cn.hippo4j.common.toolkit.Assert;
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/ThreadPoolExecutorTemplate.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ThreadPoolExecutorTemplate.java
similarity index 95%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/ThreadPoolExecutorTemplate.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ThreadPoolExecutorTemplate.java
index 2b9ec640..6810b57d 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/ThreadPoolExecutorTemplate.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ThreadPoolExecutorTemplate.java
@@ -1,6 +1,7 @@
-package cn.hippo4j.starter.toolkit.thread;
+package cn.hippo4j.core.executor.support;
-import cn.hippo4j.starter.toolkit.ArrayUtil;
+
+import cn.hippo4j.common.toolkit.ArrayUtil;
import java.util.concurrent.*;
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/RejectedProxyInvocationHandler.java b/hippo4j-core/src/main/java/cn/hippo4j/core/proxy/RejectedProxyInvocationHandler.java
similarity index 84%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/RejectedProxyInvocationHandler.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/proxy/RejectedProxyInvocationHandler.java
index 2db78af8..9f95b3e7 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/RejectedProxyInvocationHandler.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/proxy/RejectedProxyInvocationHandler.java
@@ -1,11 +1,11 @@
-package cn.hippo4j.starter.core;
+package cn.hippo4j.core.proxy;
import lombok.AllArgsConstructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
/**
* Rejected proxy invocation handler.
@@ -18,7 +18,7 @@ public class RejectedProxyInvocationHandler implements InvocationHandler {
private final Object target;
- private final AtomicInteger rejectCount;
+ private final AtomicLong rejectCount;
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/proxy/RejectedProxyUtil.java b/hippo4j-core/src/main/java/cn/hippo4j/core/proxy/RejectedProxyUtil.java
new file mode 100644
index 00000000..439009fb
--- /dev/null
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/proxy/RejectedProxyUtil.java
@@ -0,0 +1,33 @@
+package cn.hippo4j.core.proxy;
+
+import java.lang.reflect.Proxy;
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * Rejected proxy util.
+ *
+ * @author chen.ma
+ * @date 2022/2/22 21:56
+ */
+public class RejectedProxyUtil {
+
+ /**
+ * Proxy rejected execution.
+ *
+ * @param rejectedExecutionHandler
+ * @param rejectedNum
+ * @return
+ */
+ public static RejectedExecutionHandler createProxy(RejectedExecutionHandler rejectedExecutionHandler, AtomicLong rejectedNum) {
+ RejectedExecutionHandler rejectedProxy = (RejectedExecutionHandler) Proxy
+ .newProxyInstance(
+ rejectedExecutionHandler.getClass().getClassLoader(),
+ new Class[]{RejectedExecutionHandler.class},
+ new RejectedProxyInvocationHandler(rejectedExecutionHandler, rejectedNum)
+ );
+
+ return rejectedProxy;
+ }
+
+}
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/ThreadPoolDynamicRefresh.java b/hippo4j-core/src/main/java/cn/hippo4j/core/refresh/ThreadPoolDynamicRefresh.java
similarity index 63%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/ThreadPoolDynamicRefresh.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/refresh/ThreadPoolDynamicRefresh.java
index 55d4c368..b8d0f1f6 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/ThreadPoolDynamicRefresh.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/refresh/ThreadPoolDynamicRefresh.java
@@ -1,17 +1,22 @@
-package cn.hippo4j.starter.core;
+package cn.hippo4j.core.refresh;
import cn.hippo4j.common.enums.EnableEnum;
import cn.hippo4j.common.model.PoolParameterInfo;
import cn.hippo4j.common.toolkit.JSONUtil;
-import cn.hippo4j.starter.alarm.ThreadPoolAlarmManage;
-import cn.hippo4j.starter.toolkit.thread.QueueTypeEnum;
-import cn.hippo4j.starter.toolkit.thread.RejectedTypeEnum;
-import cn.hippo4j.starter.toolkit.thread.ResizableCapacityLinkedBlockIngQueue;
+import cn.hippo4j.core.executor.DynamicThreadPoolExecutor;
+import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage;
+import cn.hippo4j.core.executor.support.AbstractDynamicExecutorSupport;
+import cn.hippo4j.core.executor.support.QueueTypeEnum;
+import cn.hippo4j.core.executor.support.RejectedTypeEnum;
+import cn.hippo4j.core.executor.support.ResizableCapacityLinkedBlockIngQueue;
+import cn.hippo4j.core.proxy.RejectedProxyUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.Objects;
+import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
/**
* ThreadPool dynamic refresh.
@@ -24,7 +29,8 @@ public class ThreadPoolDynamicRefresh {
public static void refreshDynamicPool(String content) {
PoolParameterInfo parameter = JSONUtil.parseObject(content, PoolParameterInfo.class);
- ThreadPoolAlarmManage.sendPoolConfigChange(parameter);
+ // TODO 抽象报警通知模块
+ // ThreadPoolAlarmManage.sendPoolConfigChange(parameter);
ThreadPoolDynamicRefresh.refreshDynamicPool(parameter);
}
@@ -37,14 +43,28 @@ public class ThreadPoolDynamicRefresh {
String originalQuery = executor.getQueue().getClass().getSimpleName();
int originalCapacity = executor.getQueue().remainingCapacity() + executor.getQueue().size();
long originalKeepAliveTime = executor.getKeepAliveTime(TimeUnit.SECONDS);
- String originalRejected = executor.getRejectedExecutionHandler().getClass().getSimpleName();
boolean originalAllowCoreThreadTimeOut = executor.allowsCoreThreadTimeOut();
+ String originalRejected;
+ RejectedExecutionHandler rejectedExecutionHandler = executor.getRejectedExecutionHandler();
+ if (executor instanceof AbstractDynamicExecutorSupport) {
+ DynamicThreadPoolExecutor dynamicExecutor = (DynamicThreadPoolExecutor) executor;
+ rejectedExecutionHandler = dynamicExecutor.getRedundancyHandler();
+ }
+ originalRejected = rejectedExecutionHandler.getClass().getSimpleName();
+
changePoolInfo(executor, parameter);
ThreadPoolExecutor afterExecutor = GlobalThreadPoolManage.getExecutorService(threadPoolId).getExecutor();
log.info(
- "[🔥 {}] Changed thread pool. \ncoreSize :: [{}], maxSize :: [{}], queueType :: [{}], capacity :: [{}], keepAliveTime :: [{}], rejectedType :: [{}], allowCoreThreadTimeOut :: [{}]",
+ "[🔥 {}] Changed thread pool. " +
+ "\n coreSize :: [{}]" +
+ "\n maxSize :: [{}]" +
+ "\n queueType :: [{}]" +
+ "\n capacity :: [{}]" +
+ "\n keepAliveTime :: [{}]" +
+ "\n rejectedType :: [{}]" +
+ "\n allowCoreThreadTimeOut :: [{}]",
threadPoolId.toUpperCase(),
String.format("%s => %s", originalCoreSize, afterExecutor.getCorePoolSize()),
String.format("%s => %s", originalMaximumPoolSize, afterExecutor.getMaximumPoolSize()),
@@ -81,7 +101,15 @@ public class ThreadPoolDynamicRefresh {
}
if (parameter.getRejectedType() != null) {
- executor.setRejectedExecutionHandler(RejectedTypeEnum.createPolicy(parameter.getRejectedType()));
+ RejectedExecutionHandler rejectedExecutionHandler = RejectedTypeEnum.createPolicy(parameter.getRejectedType());
+ if (executor instanceof AbstractDynamicExecutorSupport) {
+ DynamicThreadPoolExecutor dynamicExecutor = (DynamicThreadPoolExecutor) executor;
+ dynamicExecutor.setRedundancyHandler(rejectedExecutionHandler);
+ AtomicLong rejectCount = dynamicExecutor.getRejectCount();
+ rejectedExecutionHandler = RejectedProxyUtil.createProxy(rejectedExecutionHandler, rejectCount);
+ }
+
+ executor.setRejectedExecutionHandler(rejectedExecutionHandler);
}
if (parameter.getAllowCoreThreadTimeOut() != null) {
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/spi/CustomBlockingQueue.java b/hippo4j-core/src/main/java/cn/hippo4j/core/spi/CustomBlockingQueue.java
similarity index 92%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/spi/CustomBlockingQueue.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/spi/CustomBlockingQueue.java
index b62795ca..37f665f0 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/spi/CustomBlockingQueue.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/spi/CustomBlockingQueue.java
@@ -1,4 +1,4 @@
-package cn.hippo4j.starter.spi;
+package cn.hippo4j.core.spi;
import java.util.concurrent.BlockingQueue;
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/spi/CustomRejectedExecutionHandler.java b/hippo4j-core/src/main/java/cn/hippo4j/core/spi/CustomRejectedExecutionHandler.java
similarity index 92%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/spi/CustomRejectedExecutionHandler.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/spi/CustomRejectedExecutionHandler.java
index c3f6ade8..1e466781 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/spi/CustomRejectedExecutionHandler.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/spi/CustomRejectedExecutionHandler.java
@@ -1,4 +1,4 @@
-package cn.hippo4j.starter.spi;
+package cn.hippo4j.core.spi;
import java.util.concurrent.RejectedExecutionHandler;
diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/spi/DynamicTpServiceLoader.java b/hippo4j-core/src/main/java/cn/hippo4j/core/spi/DynamicThreadPoolServiceLoader.java
similarity index 73%
rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/spi/DynamicTpServiceLoader.java
rename to hippo4j-core/src/main/java/cn/hippo4j/core/spi/DynamicThreadPoolServiceLoader.java
index f9e8e40f..c9d6ab86 100644
--- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/spi/DynamicTpServiceLoader.java
+++ b/hippo4j-core/src/main/java/cn/hippo4j/core/spi/DynamicThreadPoolServiceLoader.java
@@ -1,4 +1,4 @@
-package cn.hippo4j.starter.spi;
+package cn.hippo4j.core.spi;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@@ -10,16 +10,28 @@ import java.util.stream.Collectors;
* @author chen.ma
* @date 2021/7/10 23:45
*/
-public class DynamicTpServiceLoader {
+public class DynamicThreadPoolServiceLoader {
private static final Map, Collection