Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…nto springboot3
  • Loading branch information
yanhom1314 committed Feb 14, 2025
2 parents 9995293 + d2c1dae commit d7d95b0
Show file tree
Hide file tree
Showing 28 changed files with 190 additions and 147 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build-jvmti.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ jobs:
run: |
cd ${{ github.workspace }}/jvmti/jvmti-build
mvn package
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: lib
name: lib-linux
path: jvmti/jvmti-build/target/classes/lib*
if-no-files-found: error

Expand All @@ -35,9 +35,9 @@ jobs:
run: |
cd ${{ github.workspace }}/jvmti/jvmti-build
mvn package
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: lib
name: lib-macos
path: jvmti/jvmti-build/target/classes/lib*
if-no-files-found: error

Expand All @@ -54,8 +54,8 @@ jobs:
run: |
cd ${{ github.workspace }}/jvmti/jvmti-build
mvn package
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: lib
name: lib-windows
path: jvmti/jvmti-build/target/classes/*.dll
if-no-files-found: error
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@
import org.dromara.dynamictp.core.aware.AwareManager;
import org.dromara.dynamictp.core.converter.ExecutorConverter;
import org.dromara.dynamictp.core.notifier.manager.NoticeManager;
import org.dromara.dynamictp.core.support.adapter.ExecutorAdapter;
import org.dromara.dynamictp.core.support.ExecutorWrapper;
import org.dromara.dynamictp.core.support.adapter.ExecutorAdapter;
import org.dromara.dynamictp.core.support.proxy.ThreadPoolExecutorProxy;
import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper;
import org.dromara.dynamictp.core.support.task.wrapper.TaskWrappers;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -168,8 +169,23 @@ protected TpMainFields getTpMainFields(ExecutorWrapper executorWrapper, TpExecut

protected void enhanceOriginExecutor(String tpName, ThreadPoolExecutor executor, String fieldName, Object targetObj) {
ThreadPoolExecutorProxy proxy = new ThreadPoolExecutorProxy(executor);
boolean r = ReflectionUtil.setFieldValue(fieldName, targetObj, proxy);
if (r) {
putAndFinalize(tpName, executor, proxy);
}
}

protected void enhanceOriginExecutor(String tpName, ThreadPoolExecutor executor, Field field, Object targetObj) {
ThreadPoolExecutorProxy proxy = new ThreadPoolExecutorProxy(executor);
boolean r = ReflectionUtil.setFieldValue(field, targetObj, proxy);
if (r) {
putAndFinalize(tpName, executor, proxy);
}
}

protected void enhanceOriginExecutorWithoutFinalize(String tpName, ThreadPoolExecutorProxy proxy, String fieldName, Object targetObj) {
ReflectionUtil.setFieldValue(fieldName, targetObj, proxy);
putAndFinalize(tpName, executor, proxy);
executors.put(tpName, new ExecutorWrapper(tpName, proxy));
}

protected void putAndFinalize(String tpName, ExecutorService origin, Executor targetForWrapper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.dromara.dynamictp.adapter.common.AbstractDtpAdapter;
import org.dromara.dynamictp.common.properties.DtpProperties;
import org.dromara.dynamictp.common.util.ReflectionUtil;
import org.dromara.dynamictp.core.support.proxy.ThreadPoolExecutorProxy;
import org.dromara.dynamictp.jvmti.JVMTI;

import java.net.InetSocketAddress;
Expand Down Expand Up @@ -80,7 +81,10 @@ protected void initialize() {
}
val executor = (Executor) ReflectionUtil.getFieldValue(ServerImpl.class, EXECUTOR_FIELD, serverImpl);
if (Objects.nonNull(executor) && executor instanceof ThreadPoolExecutor) {
enhanceOriginExecutor(genTpName(key), (ThreadPoolExecutor) executor, EXECUTOR_FIELD, serverImpl);
String tpName = genTpName(key);
val proxy = new ThreadPoolExecutorProxy((ThreadPoolExecutor) executor);
// don't shutdown the origin executor, because it's a shared executor, and may be used by other components
enhanceOriginExecutorWithoutFinalize(tpName, proxy, EXECUTOR_FIELD, serverImpl);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
import lombok.val;
import okhttp3.OkHttpClient;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.dromara.dynamictp.adapter.common.AbstractDtpAdapter;
import org.dromara.dynamictp.common.properties.DtpProperties;
import org.dromara.dynamictp.common.manager.ContextManagerHelper;
import org.dromara.dynamictp.common.properties.DtpProperties;
import org.dromara.dynamictp.common.util.ReflectionUtil;

import java.lang.reflect.Field;
import java.util.Objects;
import java.util.concurrent.ThreadPoolExecutor;

/**
Expand All @@ -40,6 +44,8 @@ public class Okhttp3DtpAdapter extends AbstractDtpAdapter {

private static final String EXECUTOR_SERVICE_FIELD = "executorService";

private static final String EXECUTOR_SERVICE_FIELD_ALTERNATIVE = "executorServiceOrNull";

@Override
public void refresh(DtpProperties dtpProperties) {
refresh(dtpProperties.getOkhttp3Tp(), dtpProperties.getPlatforms());
Expand All @@ -59,10 +65,17 @@ protected void initialize() {
return;
}
beans.forEach((k, v) -> {
val executor = v.dispatcher().executorService();
if (executor instanceof ThreadPoolExecutor) {
enhanceOriginExecutor(genTpName(k), (ThreadPoolExecutor) executor, EXECUTOR_SERVICE_FIELD, v.dispatcher());
val dispatcher = v.dispatcher();
val executor = dispatcher.executorService();
if (!(executor instanceof ThreadPoolExecutor)) {
return;
}

Field field = FieldUtils.getField(dispatcher.getClass(), EXECUTOR_SERVICE_FIELD, true);
if (Objects.isNull(field)) {
field = ReflectionUtil.getField(dispatcher.getClass(), EXECUTOR_SERVICE_FIELD_ALTERNATIVE);
}
enhanceOriginExecutor(genTpName(k), (ThreadPoolExecutor) executor, field, dispatcher);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import lombok.Data;
import lombok.EqualsAndHashCode;


/**
* ThreadPoolStats related
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
import org.yaml.snakeyaml.Yaml;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.LinkedHashMap;

/**
* YamlConfigParser related
Expand Down Expand Up @@ -68,8 +69,8 @@ private void flattenMap(Map<Object, Object> result, Map<Object, Object> source,
String fullPath = (path != null ? path + "." + key : key.toString());
if (value instanceof Map) {
flattenMap(result, (Map<Object, Object>) value, fullPath);
} else if (value instanceof List) {
for (int i = 0; i < ((List<?>) value).size(); i++) {
} else if (value instanceof Collection) {
for (int i = 0; i < ((Collection<?>) value).size(); i++) {
flattenMap(result, Collections.singletonMap("[" + i + "]", ((List<?>) value).get(i)), fullPath);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ public static class Etcd {
private String authority = "ssl";

private String key;

private long timeout = 30000L;
}

private static class Holder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -65,27 +64,31 @@ public static Object getFieldValue(Class<?> targetClass, String fieldName, Objec
}
}

public static void setFieldValue(String fieldName, Object targetObj, Object targetVal) {
val field = getField(targetObj.getClass(), fieldName);
public static boolean setFieldValue(String fieldName, Object targetObj, Object targetVal) {
return setFieldValue(targetObj.getClass(), fieldName, targetObj, targetVal);
}

public static boolean setFieldValue(Class<?> targetClass, String fieldName, Object targetObj, Object targetVal) {
val field = getField(targetClass, fieldName);
if (Objects.isNull(field)) {
return;
return false;
}
try {
FieldUtils.writeField(field, targetObj, targetVal, true);
return true;
} catch (IllegalAccessException e) {
log.error("Failed to write value '{}' to field '{}' in object '{}'", targetVal, fieldName, targetObj, e);
return false;
}
}

public static void setFieldValue(Class<?> targetClass, String fieldName, Object targetObj, Object targetVal) {
val field = getField(targetClass, fieldName);
if (Objects.isNull(field)) {
return;
}
public static boolean setFieldValue(Field field, Object targetObj, Object targetVal) {
try {
FieldUtils.writeField(field, targetObj, targetVal, true);
return true;
} catch (IllegalAccessException e) {
log.error("Failed to write value '{}' to field '{}' in object '{}'", targetVal, fieldName, targetObj, e);
log.error("Failed to write value '{}' to field '{}' in object '{}'", targetVal, field.getName(), targetObj, e);
return false;
}
}

Expand All @@ -102,6 +105,7 @@ public static Method findMethod(Class<?> targetClass, String methodName, Class<?
Method method = MethodUtils.getMatchingMethod(targetClass, methodName, parameterTypes);
if (Objects.isNull(method)) {
log.warn("Method '{}' with parameters '{}' not found in class '{}'", methodName, parameterTypes, targetClass.getName());
return null;
}
return method;
}
Expand Down
14 changes: 13 additions & 1 deletion core/src/main/java/org/dromara/dynamictp/core/DtpRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
import org.dromara.dynamictp.core.notifier.manager.NoticeManager;
import org.dromara.dynamictp.core.notifier.manager.NotifyHelper;
import org.dromara.dynamictp.core.reject.RejectHandlerGetter;
import org.dromara.dynamictp.core.support.adapter.ExecutorAdapter;
import org.dromara.dynamictp.core.support.ExecutorWrapper;
import org.dromara.dynamictp.core.support.adapter.ExecutorAdapter;
import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper;
import org.dromara.dynamictp.core.support.task.wrapper.TaskWrappers;

Expand Down Expand Up @@ -117,6 +117,18 @@ public static void registerExecutor(ExecutorWrapper wrapper, String source) {
EXECUTOR_REGISTRY.putIfAbsent(wrapper.getThreadPoolName(), wrapper);
}

/**
* Unregister a executor.
*
* @param name thread pool name
* @return the managed DtpExecutor instance
*/
public static ExecutorWrapper unregisterExecutor(String name) {
ExecutorWrapper executorWrapper = getExecutorWrapper(name);
log.info("DynamicTp unregister executor: {}", executorWrapper);
return EXECUTOR_REGISTRY.remove(name);
}

/**
* Get DtpExecutor by thread pool name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class ScheduledThreadPoolExecutorProxy extends ScheduledThreadPoolExecuto
public ScheduledThreadPoolExecutorProxy(ScheduledThreadPoolExecutor executor) {
super(executor.getCorePoolSize(), executor.getThreadFactory());
this.rejectHandlerType = executor.getRejectedExecutionHandler().getClass().getSimpleName();
setRejectedExecutionHandler(RejectHandlerGetter.getProxy(getRejectedExecutionHandler()));
setRejectedExecutionHandler(RejectHandlerGetter.getProxy(executor.getRejectedExecutionHandler()));
}

@Override
Expand Down
21 changes: 18 additions & 3 deletions dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<url>https://github.com/yanhom1314/dynamic-tp</url>

<properties>
<revision>1.1.9.1-3.x</revision>
<revision>1.2.0-x</revision>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.compiler.source>17</maven.compiler.source>
Expand All @@ -37,7 +38,7 @@
<apache-dubbo.version>3.0.7</apache-dubbo.version>
<alibaba-dubbo.version>2.6.0</alibaba-dubbo.version>
<alibaba-dubbo-starter.version>2.0.0</alibaba-dubbo-starter.version>
<grpc-starter.version>2.13.1.RELEASE</grpc-starter.version>
<grpc-starter.version>2.15.0.RELEASE</grpc-starter.version>
<hystrix.version>1.5.18</hystrix.version>
<motan.version>1.2.0</motan.version>
<okhttp.version>3.14.9</okhttp.version>
Expand Down Expand Up @@ -65,6 +66,7 @@
<maven-javadoc-plugin.version>3.6.2</maven-javadoc-plugin.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<nexus-staging-maven-plugin.version>1.6.13</nexus-staging-maven-plugin.version>
<central-publishing-maven-plugin.version>0.6.0</central-publishing-maven-plugin.version>

<dropwizard-metrics.version>4.2.20</dropwizard-metrics.version>
<jakarta-mail.version>2.1.0</jakarta-mail.version>
Expand Down Expand Up @@ -112,7 +114,7 @@

<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-server-spring-boot-starter</artifactId>
<artifactId>grpc-spring-boot-starter</artifactId>
<version>${grpc-starter.version}</version>
</dependency>

Expand Down Expand Up @@ -661,6 +663,7 @@
<version>${maven-source-plugin.version}</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
Expand All @@ -674,6 +677,7 @@
<version>${maven-javadoc-plugin.version}</version>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
Expand All @@ -699,6 +703,17 @@
</executions>
</plugin>

<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>${central-publishing-maven-plugin.version}</version>
<extensions>true</extensions>
<configuration>
<publishingServerId>ossrh</publishingServerId>
<autoPublish>false</autoPublish>
</configuration>
</plugin>

<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
Expand Down
14 changes: 10 additions & 4 deletions example/example-adapter/example-adapter-grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand All @@ -70,18 +76,18 @@
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.4.1.Final</version>
<version>1.7.1</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.0.0:exe:${os.detected.classifier}</protocArtifact>
<protocArtifact>com.google.protobuf:protoc:3.18.2:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.0:exe:${os.detected.classifier}</pluginArtifact>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.69.1:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
import org.dromara.dynamictp.spring.annotation.EnableDynamicTp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;

/**
* @author fabian4
*/
@EnableDynamicTp
@SpringBootApplication
@SpringBootApplication(exclude = RedisAutoConfiguration.class)
public class GrpcExampleApplication {
public static void main(String[] args) {
SpringApplication.run(GrpcExampleApplication.class, args);
Expand Down
Loading

0 comments on commit d7d95b0

Please sign in to comment.