Skip to content

Commit

Permalink
Merge pull request dromara#525 from dromara/optimize
Browse files Browse the repository at this point in the history
[ISSUE dromara#524] optimize code and fix could not get thread pool for higher version okhttp3
  • Loading branch information
yanhom1314 authored Feb 7, 2025
2 parents e997ad8 + 01b2f2a commit 4b9fe75
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 28 deletions.
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,18 @@ protected TpMainFields getTpMainFields(ExecutorWrapper executorWrapper, TpExecut

protected void enhanceOriginExecutor(String tpName, ThreadPoolExecutor executor, String fieldName, Object targetObj) {
ThreadPoolExecutorProxy proxy = new ThreadPoolExecutorProxy(executor);
ReflectionUtil.setFieldValue(fieldName, targetObj, proxy);
putAndFinalize(tpName, executor, proxy);
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 putAndFinalize(String tpName, ExecutorService origin, Executor targetForWrapper) {
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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Environment getEnvironment() {

@Override
public String getEnvironmentProperty(String key) {
return getInstance().getEnvironment().getProperty(key);
return getEnvironment().getProperty(key);
}

@Override
Expand All @@ -84,6 +84,6 @@ public String getEnvironmentProperty(String key, Object environment) {

@Override
public String getEnvironmentProperty(String key, String defaultValue) {
return getInstance().getEnvironment().getProperty(key, defaultValue);
return getEnvironment().getProperty(key, defaultValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* SpringDtpInitializer related
*
* @author yanhom
* @since 1.1.0
* @since 1.2.0
*/
public class SpringDtpInitializer implements DtpInitializer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.dromara.dynamictp.core.lifecycle.LifeCycleManagement;
import org.springframework.context.SmartLifecycle;

import java.util.concurrent.atomic.AtomicBoolean;

/**
* Adapts LifeCycleManagement to Spring's SmartLifecycle interface.
*
Expand All @@ -30,33 +32,36 @@ public class DtpLifecycleSpringAdapter implements SmartLifecycle {

private final LifeCycleManagement lifeCycleManagement;

private boolean isRunning = false;
private final AtomicBoolean running = new AtomicBoolean(false);

public DtpLifecycleSpringAdapter(LifeCycleManagement lifeCycleManagement) {
this.lifeCycleManagement = lifeCycleManagement;
}

@Override
public void start() {
lifeCycleManagement.start();
isRunning = true;
if (this.running.compareAndSet(false, true)) {
lifeCycleManagement.start();
}
}

@Override
public void stop() {
lifeCycleManagement.stop();
isRunning = false;
if (this.running.compareAndSet(true, false)) {
lifeCycleManagement.stop();
}
}

@Override
public boolean isRunning() {
return isRunning;
return this.running.get();
}

@Override
public void stop(Runnable callback) {
lifeCycleManagement.stop(callback);
isRunning = false;
if (this.running.compareAndSet(true, false)) {
lifeCycleManagement.stop(callback);
}
}

/**
Expand Down

0 comments on commit 4b9fe75

Please sign in to comment.