diff --git a/agents/threadpool/hippo4j-agent-core/pom.xml b/agents/threadpool/hippo4j-agent-core/pom.xml index add15fd7..3af3f0f6 100644 --- a/agents/threadpool/hippo4j-agent-core/pom.xml +++ b/agents/threadpool/hippo4j-agent-core/pom.xml @@ -76,8 +76,6 @@ cn.hippo4j hippo4j-threadpool-common - ${project.version} - provided diff --git a/agents/threadpool/hippo4j-agent-plugin/pom.xml b/agents/threadpool/hippo4j-agent-plugin/pom.xml index a49097de..0a4ed758 100644 --- a/agents/threadpool/hippo4j-agent-plugin/pom.xml +++ b/agents/threadpool/hippo4j-agent-plugin/pom.xml @@ -41,7 +41,6 @@ net.bytebuddy byte-buddy - ${bytebuddy.version} provided diff --git a/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-1.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/define/EventPublishingRunListenerInstrumentation.java b/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-1.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/define/EventPublishingRunListenerInstrumentation.java index e8f5d73f..db860dbe 100644 --- a/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-1.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/define/EventPublishingRunListenerInstrumentation.java +++ b/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-1.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/define/EventPublishingRunListenerInstrumentation.java @@ -17,6 +17,7 @@ package cn.hippo4j.agent.plugin.spring.boot.v1.define; +import cn.hippo4j.agent.core.plugin.WitnessMethod; import cn.hippo4j.agent.core.plugin.interceptor.ConstructorInterceptPoint; import cn.hippo4j.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; import cn.hippo4j.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; @@ -24,6 +25,9 @@ import cn.hippo4j.agent.core.plugin.match.ClassMatch; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.matcher.ElementMatcher; +import java.util.Collections; +import java.util.List; + import static net.bytebuddy.matcher.ElementMatchers.named; import static cn.hippo4j.agent.core.plugin.match.NameMatch.byName; @@ -83,4 +87,10 @@ public class EventPublishingRunListenerInstrumentation extends ClassInstanceMeth } }; } + + @Override + protected List witnessMethods() { + return Collections.singletonList(new WitnessMethod("org.springframework.boot.context.event.EventPublishingRunListener", + named("finished"))); + } } diff --git a/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-1.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/interceptor/EventPublishingFinishedInterceptor.java b/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-1.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/interceptor/EventPublishingFinishedInterceptor.java index 1ab39c2e..8f347d98 100644 --- a/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-1.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/interceptor/EventPublishingFinishedInterceptor.java +++ b/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-1.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/interceptor/EventPublishingFinishedInterceptor.java @@ -47,7 +47,7 @@ public class EventPublishingFinishedInterceptor implements InstanceMethodsAround ConfigurableApplicationContext context = (ConfigurableApplicationContext) allArguments[0]; if (context.getParent() != null) { // After the child container is started, the thread pool registration will be carried out - SpringThreadPoolRegisterSupport.registerThreadPoolInstances(); + SpringThreadPoolRegisterSupport.registerThreadPoolInstances(context); return ret; } SpringPropertiesLoader.loadSpringProperties(context.getEnvironment()); diff --git a/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-2.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/define/EventPublishingRunListenerInstrumentation.java b/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-2.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/define/EventPublishingRunListenerInstrumentation.java index 56c60d5a..b2123688 100644 --- a/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-2.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/define/EventPublishingRunListenerInstrumentation.java +++ b/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-2.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/define/EventPublishingRunListenerInstrumentation.java @@ -17,6 +17,7 @@ package cn.hippo4j.agent.plugin.spring.boot.v2.define; +import cn.hippo4j.agent.core.plugin.WitnessMethod; import cn.hippo4j.agent.core.plugin.interceptor.ConstructorInterceptPoint; import cn.hippo4j.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; import cn.hippo4j.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; @@ -24,6 +25,9 @@ import cn.hippo4j.agent.core.plugin.match.ClassMatch; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.matcher.ElementMatcher; +import java.util.Collections; +import java.util.List; + import static net.bytebuddy.matcher.ElementMatchers.named; import static cn.hippo4j.agent.core.plugin.match.NameMatch.byName; @@ -83,4 +87,10 @@ public class EventPublishingRunListenerInstrumentation extends ClassInstanceMeth } }; } + + @Override + protected List witnessMethods() { + return Collections.singletonList(new WitnessMethod("org.springframework.boot.context.event.EventPublishingRunListener", + named("started"))); + } } diff --git a/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-2.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/interceptor/EventPublishingStartedInterceptor.java b/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-2.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/interceptor/EventPublishingStartedInterceptor.java index 1c782722..b4478b37 100644 --- a/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-2.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/interceptor/EventPublishingStartedInterceptor.java +++ b/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-boot-2.x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/interceptor/EventPublishingStartedInterceptor.java @@ -26,9 +26,12 @@ import cn.hippo4j.agent.plugin.spring.boot.v2.DynamicThreadPoolChangeHandlerSpri import cn.hippo4j.agent.plugin.spring.common.support.SpringPropertiesLoader; import cn.hippo4j.agent.plugin.spring.common.support.IDynamicThreadPoolChangeHandlerSpring; import cn.hippo4j.agent.plugin.spring.common.support.SpringThreadPoolRegisterSupport; +import cn.hippo4j.core.executor.DynamicThreadPool; +import cn.hippo4j.core.executor.SpringDynamicThreadPool; import org.springframework.context.ConfigurableApplicationContext; import java.lang.reflect.Method; +import java.util.Map; public class EventPublishingStartedInterceptor implements InstanceMethodsAroundInterceptor { @@ -44,7 +47,7 @@ public class EventPublishingStartedInterceptor implements InstanceMethodsAroundI ConfigurableApplicationContext context = (ConfigurableApplicationContext) allArguments[0]; if (context.getParent() != null) { // After the child container is started, the thread pool registration will be carried out - SpringThreadPoolRegisterSupport.registerThreadPoolInstances(); + SpringThreadPoolRegisterSupport.registerThreadPoolInstances(context); return ret; } SpringPropertiesLoader.loadSpringProperties(context.getEnvironment()); diff --git a/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-plugin-common/src/main/java/cn/hippo4j/agent/plugin/spring/common/support/SpringThreadPoolRegisterSupport.java b/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-plugin-common/src/main/java/cn/hippo4j/agent/plugin/spring/common/support/SpringThreadPoolRegisterSupport.java index bde71b97..19dd2534 100644 --- a/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-plugin-common/src/main/java/cn/hippo4j/agent/plugin/spring/common/support/SpringThreadPoolRegisterSupport.java +++ b/agents/threadpool/hippo4j-agent-plugin/spring-plugins/spring-plugin-common/src/main/java/cn/hippo4j/agent/plugin/spring/common/support/SpringThreadPoolRegisterSupport.java @@ -23,13 +23,18 @@ import cn.hippo4j.agent.core.util.ThreadPoolPropertyKey; import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum; import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum; import cn.hippo4j.common.toolkit.BooleanUtil; +import cn.hippo4j.core.executor.DynamicThreadPool; +import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; +import cn.hippo4j.core.executor.support.adpter.DynamicThreadPoolAdapterChoose; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; import java.lang.reflect.Field; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -37,7 +42,7 @@ public class SpringThreadPoolRegisterSupport { private static final Logger LOGGER = LoggerFactory.getLogger(SpringThreadPoolRegisterSupport.class); - public static void registerThreadPoolInstances() { + public static void registerThreadPoolInstances(ApplicationContext context) { Map> earlyConstructMap = AgentThreadPoolInstanceRegistry.getInstance().earlyConstructMap; for (Map.Entry> entry : earlyConstructMap.entrySet()) { ThreadPoolExecutor enhancedInstance = entry.getKey(); @@ -56,10 +61,30 @@ public class SpringThreadPoolRegisterSupport { } } } + + Map beansWithAnnotation = context.getBeansOfType(Executor.class); + for (Map.Entry entry : beansWithAnnotation.entrySet()) { + String beanName = entry.getKey(); + Executor bean = entry.getValue(); + ThreadPoolExecutor executor = null; + if (DynamicThreadPoolAdapterChoose.match(bean)) { + executor = DynamicThreadPoolAdapterChoose.unwrap(bean); + } else { + executor = (ThreadPoolExecutor) bean; + } + if (executor == null) { + LOGGER.warn("[Hippo4j-Agent] Thread pool is null, ignore bean registration. beanName={}, beanClass={}", beanName, bean.getClass().getName()); + } else { + register(beanName, executor); + } + } LOGGER.info("[Hippo4j-Agent] Registered thread pool instances successfully."); } public static void register(String threadPoolId, ThreadPoolExecutor executor) { + if (executor == null) { + return; + } // build parameter properties. Properties properties = new Properties(); properties.put(ThreadPoolPropertyKey.THREAD_POOL_ID, threadPoolId); diff --git a/agents/threadpool/pom.xml b/agents/threadpool/pom.xml index 0ccc62a9..60d235d0 100644 --- a/agents/threadpool/pom.xml +++ b/agents/threadpool/pom.xml @@ -69,6 +69,13 @@ hippo4j-agent-core ${project.version} + + cn.hippo4j + hippo4j-threadpool-common + ${project.version} + provided + + com.google.code.gson gson diff --git a/threadpool/core/src/test/java/cn/hippo4j/core/executor/support/AbstractBuildThreadPoolTemplateTest.java b/threadpool/core/src/test/java/cn/hippo4j/core/executor/support/AbstractBuildThreadPoolTemplateTest.java index 67cbc82a..8fcf7140 100644 --- a/threadpool/core/src/test/java/cn/hippo4j/core/executor/support/AbstractBuildThreadPoolTemplateTest.java +++ b/threadpool/core/src/test/java/cn/hippo4j/core/executor/support/AbstractBuildThreadPoolTemplateTest.java @@ -52,7 +52,6 @@ public class AbstractBuildThreadPoolTemplateTest { .setTaskDecorator(runnable -> runnable); } - @Test public void testBuildPool() { ThreadPoolExecutor executor = AbstractBuildThreadPoolTemplate.buildPool(initParam); @@ -74,7 +73,6 @@ public class AbstractBuildThreadPoolTemplateTest { } - @Test public void testBuildFastPool() { ThreadPoolExecutor executor = AbstractBuildThreadPoolTemplate.buildFastPool(initParam); @@ -96,8 +94,6 @@ public class AbstractBuildThreadPoolTemplateTest { } - - @Test public void testBuildDynamicPool() { initParam.setWaitForTasksToCompleteOnShutdown(true);