Add common dependencies and realize basic functions.

pull/1295/head
yanrongzhen 2 years ago
parent 0035e970dd
commit 29ebc070cf

@ -76,8 +76,6 @@
<dependency> <dependency>
<groupId>cn.hippo4j</groupId> <groupId>cn.hippo4j</groupId>
<artifactId>hippo4j-threadpool-common</artifactId> <artifactId>hippo4j-threadpool-common</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>

@ -41,7 +41,6 @@
<dependency> <dependency>
<groupId>net.bytebuddy</groupId> <groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId> <artifactId>byte-buddy</artifactId>
<version>${bytebuddy.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>

@ -17,6 +17,7 @@
package cn.hippo4j.agent.plugin.spring.boot.v1.define; 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.ConstructorInterceptPoint;
import cn.hippo4j.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; import cn.hippo4j.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import cn.hippo4j.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; 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.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher; import net.bytebuddy.matcher.ElementMatcher;
import java.util.Collections;
import java.util.List;
import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.named;
import static cn.hippo4j.agent.core.plugin.match.NameMatch.byName; import static cn.hippo4j.agent.core.plugin.match.NameMatch.byName;
@ -83,4 +87,10 @@ public class EventPublishingRunListenerInstrumentation extends ClassInstanceMeth
} }
}; };
} }
@Override
protected List<WitnessMethod> witnessMethods() {
return Collections.singletonList(new WitnessMethod("org.springframework.boot.context.event.EventPublishingRunListener",
named("finished")));
}
} }

@ -47,7 +47,7 @@ public class EventPublishingFinishedInterceptor implements InstanceMethodsAround
ConfigurableApplicationContext context = (ConfigurableApplicationContext) allArguments[0]; ConfigurableApplicationContext context = (ConfigurableApplicationContext) allArguments[0];
if (context.getParent() != null) { if (context.getParent() != null) {
// After the child container is started, the thread pool registration will be carried out // After the child container is started, the thread pool registration will be carried out
SpringThreadPoolRegisterSupport.registerThreadPoolInstances(); SpringThreadPoolRegisterSupport.registerThreadPoolInstances(context);
return ret; return ret;
} }
SpringPropertiesLoader.loadSpringProperties(context.getEnvironment()); SpringPropertiesLoader.loadSpringProperties(context.getEnvironment());

@ -17,6 +17,7 @@
package cn.hippo4j.agent.plugin.spring.boot.v2.define; 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.ConstructorInterceptPoint;
import cn.hippo4j.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; import cn.hippo4j.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import cn.hippo4j.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; 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.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher; import net.bytebuddy.matcher.ElementMatcher;
import java.util.Collections;
import java.util.List;
import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.named;
import static cn.hippo4j.agent.core.plugin.match.NameMatch.byName; import static cn.hippo4j.agent.core.plugin.match.NameMatch.byName;
@ -83,4 +87,10 @@ public class EventPublishingRunListenerInstrumentation extends ClassInstanceMeth
} }
}; };
} }
@Override
protected List<WitnessMethod> witnessMethods() {
return Collections.singletonList(new WitnessMethod("org.springframework.boot.context.event.EventPublishingRunListener",
named("started")));
}
} }

@ -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.SpringPropertiesLoader;
import cn.hippo4j.agent.plugin.spring.common.support.IDynamicThreadPoolChangeHandlerSpring; import cn.hippo4j.agent.plugin.spring.common.support.IDynamicThreadPoolChangeHandlerSpring;
import cn.hippo4j.agent.plugin.spring.common.support.SpringThreadPoolRegisterSupport; 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 org.springframework.context.ConfigurableApplicationContext;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Map;
public class EventPublishingStartedInterceptor implements InstanceMethodsAroundInterceptor { public class EventPublishingStartedInterceptor implements InstanceMethodsAroundInterceptor {
@ -44,7 +47,7 @@ public class EventPublishingStartedInterceptor implements InstanceMethodsAroundI
ConfigurableApplicationContext context = (ConfigurableApplicationContext) allArguments[0]; ConfigurableApplicationContext context = (ConfigurableApplicationContext) allArguments[0];
if (context.getParent() != null) { if (context.getParent() != null) {
// After the child container is started, the thread pool registration will be carried out // After the child container is started, the thread pool registration will be carried out
SpringThreadPoolRegisterSupport.registerThreadPoolInstances(); SpringThreadPoolRegisterSupport.registerThreadPoolInstances(context);
return ret; return ret;
} }
SpringPropertiesLoader.loadSpringProperties(context.getEnvironment()); SpringPropertiesLoader.loadSpringProperties(context.getEnvironment());

@ -23,13 +23,18 @@ import cn.hippo4j.agent.core.util.ThreadPoolPropertyKey;
import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum; import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum;
import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum; import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum;
import cn.hippo4j.common.toolkit.BooleanUtil; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -37,7 +42,7 @@ public class SpringThreadPoolRegisterSupport {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringThreadPoolRegisterSupport.class); private static final Logger LOGGER = LoggerFactory.getLogger(SpringThreadPoolRegisterSupport.class);
public static void registerThreadPoolInstances() { public static void registerThreadPoolInstances(ApplicationContext context) {
Map<ThreadPoolExecutor, Class<?>> earlyConstructMap = AgentThreadPoolInstanceRegistry.getInstance().earlyConstructMap; Map<ThreadPoolExecutor, Class<?>> earlyConstructMap = AgentThreadPoolInstanceRegistry.getInstance().earlyConstructMap;
for (Map.Entry<ThreadPoolExecutor, Class<?>> entry : earlyConstructMap.entrySet()) { for (Map.Entry<ThreadPoolExecutor, Class<?>> entry : earlyConstructMap.entrySet()) {
ThreadPoolExecutor enhancedInstance = entry.getKey(); ThreadPoolExecutor enhancedInstance = entry.getKey();
@ -56,10 +61,30 @@ public class SpringThreadPoolRegisterSupport {
} }
} }
} }
Map<String, Executor> beansWithAnnotation = context.getBeansOfType(Executor.class);
for (Map.Entry<String, Executor> 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."); LOGGER.info("[Hippo4j-Agent] Registered thread pool instances successfully.");
} }
public static void register(String threadPoolId, ThreadPoolExecutor executor) { public static void register(String threadPoolId, ThreadPoolExecutor executor) {
if (executor == null) {
return;
}
// build parameter properties. // build parameter properties.
Properties properties = new Properties(); Properties properties = new Properties();
properties.put(ThreadPoolPropertyKey.THREAD_POOL_ID, threadPoolId); properties.put(ThreadPoolPropertyKey.THREAD_POOL_ID, threadPoolId);

@ -69,6 +69,13 @@
<artifactId>hippo4j-agent-core</artifactId> <artifactId>hippo4j-agent-core</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>cn.hippo4j</groupId>
<artifactId>hippo4j-threadpool-common</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>

@ -52,7 +52,6 @@ public class AbstractBuildThreadPoolTemplateTest {
.setTaskDecorator(runnable -> runnable); .setTaskDecorator(runnable -> runnable);
} }
@Test @Test
public void testBuildPool() { public void testBuildPool() {
ThreadPoolExecutor executor = AbstractBuildThreadPoolTemplate.buildPool(initParam); ThreadPoolExecutor executor = AbstractBuildThreadPoolTemplate.buildPool(initParam);
@ -74,7 +73,6 @@ public class AbstractBuildThreadPoolTemplateTest {
} }
@Test @Test
public void testBuildFastPool() { public void testBuildFastPool() {
ThreadPoolExecutor executor = AbstractBuildThreadPoolTemplate.buildFastPool(initParam); ThreadPoolExecutor executor = AbstractBuildThreadPoolTemplate.buildFastPool(initParam);
@ -96,8 +94,6 @@ public class AbstractBuildThreadPoolTemplateTest {
} }
@Test @Test
public void testBuildDynamicPool() { public void testBuildDynamicPool() {
initParam.setWaitForTasksToCompleteOnShutdown(true); initParam.setWaitForTasksToCompleteOnShutdown(true);

Loading…
Cancel
Save