Skip to content

Commit

Permalink
Fix could not properly update complex types in tp prop
Browse files Browse the repository at this point in the history
  • Loading branch information
yanhom1314 committed Sep 24, 2024
1 parent a80a717 commit 5c11b1e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public EagerThreadPoolExecutorProxy(EagerThreadPoolExecutor executor) {
executor.getKeepAliveTime(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS,
(TaskQueue<Runnable>) executor.getQueue(), executor.getThreadFactory(),
executor.getRejectedExecutionHandler());
allowCoreThreadTimeOut(executor.allowsCoreThreadTimeOut());
this.rejectHandlerType = getRejectedExecutionHandler().getClass().getSimpleName();
setRejectedExecutionHandler(RejectHandlerGetter.getProxy(getRejectedExecutionHandler()));
((TaskQueue<Runnable>) getQueue()).setExecutor(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public StandardThreadExecutorProxy(StandardThreadExecutor executor) {
executor.getKeepAliveTime(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS,
executor.getMaxSubmittedTaskCount() - executor.getMaximumPoolSize(),
executor.getThreadFactory(), executor.getRejectedExecutionHandler());
allowCoreThreadTimeOut(executor.allowsCoreThreadTimeOut());
RejectedExecutionHandler handler = getRejectedExecutionHandler();
this.rejectHandlerType = handler.getClass().getSimpleName();
setRejectedExecutionHandler(RejectHandlerGetter.getProxy(handler));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -39,6 +38,7 @@
import static org.dromara.dynamictp.common.constant.DynamicTpConst.MAIN_PROPERTIES_PREFIX;
import static org.dromara.dynamictp.common.constant.DynamicTpConst.NOTIFY_ITEMS;
import static org.dromara.dynamictp.common.constant.DynamicTpConst.PLATFORM_IDS;
import static org.dromara.dynamictp.common.constant.DynamicTpConst.PLUGIN_NAMES;

/**
* DtpPropertiesBinderUtil related
Expand Down Expand Up @@ -76,11 +76,12 @@ private static void tryResetCusExecutors(DtpProperties dtpProperties, Object sou
String executorFieldKey = EXECUTORS_CONFIG_PREFIX + idx[0] + "]." + field.getName();
setBasicField(source, field, executor, executorFieldKey);
});
setListField(dtpProperties, executor);
setListField(dtpProperties, executor, "executors[" + idx[0] + "]");
String prefix = MAIN_PROPERTIES_PREFIX + "." + "executors[" + idx[0] + "]";
val globalExecutorProps = dtpProperties.getGlobalExecutorProps();
if (CollectionUtils.isEmpty(executor.getPluginNames()) &&
if (!contains(prefix + ".pluginNames[0]", dtpProperties) &&
CollectionUtils.isNotEmpty(globalExecutorProps.getPluginNames())) {
executor.setPluginNames(globalExecutorProps.getPluginNames());
ReflectUtil.setFieldValue(executor, PLUGIN_NAMES, globalExecutorProps.getPluginNames());
}
idx[0]++;
});
Expand All @@ -96,7 +97,7 @@ private static void tryResetAdapterExecutors(DtpProperties dtpProperties, Object
}
if (dtpPropertiesField.getType().isAssignableFrom(TpExecutorProps.class)) {
tpExecutorPropFields.forEach(tpField -> setBasicField(source, tpField, dtpPropertiesField.getName(), targetObj));
setListField(dtpProperties, targetObj);
setListField(dtpProperties, targetObj, dtpPropertiesField.getName());
} else if (dtpPropertiesField.getGenericType() instanceof ParameterizedType) {
ParameterizedType paramType = (ParameterizedType) dtpPropertiesField.getGenericType();
Type[] argTypes = paramType.getActualTypeArguments();
Expand All @@ -108,7 +109,7 @@ private static void tryResetAdapterExecutors(DtpProperties dtpProperties, Object
int[] idx = {0};
tpExecutorProps.forEach(tpProp -> {
tpExecutorPropFields.forEach(tpField -> setBasicField(source, tpField, dtpPropertiesField.getName(), tpProp, idx));
setListField(dtpProperties, tpProp);
setListField(dtpProperties, tpProp, dtpPropertiesField.getName() + "[" + idx[0] + "]");
idx[0]++;
});
}
Expand All @@ -127,6 +128,17 @@ private static Object getProperty(String key, Object environment) {
return null;
}

private static boolean contains(String key, Object environment) {
if (environment instanceof Environment) {
Environment env = (Environment) environment;
return env.containsProperty(key);
} else if (environment instanceof Map) {
Map<?, Object> properties = (Map<?, Object>) environment;
return properties.containsKey(key);
}
return false;
}

private static void setBasicField(Object source, Field tpPropField, String targetObjName, Object targetObj, int[] idx) {
String executorFieldKey = MAIN_PROPERTIES_PREFIX + "." + targetObjName + "[" + idx[0] + "]." + tpPropField.getName();
setBasicField(source, tpPropField, targetObj, executorFieldKey);
Expand All @@ -149,27 +161,22 @@ private static void setBasicField(Object source, Field tpPropField, Object targe
ReflectUtil.setFieldValue(targetObj, tpPropField.getName(), globalFieldVal);
}

private static void setListField(DtpProperties dtpProperties, Object fieldVal) {
private static void setListField(DtpProperties dtpProperties, Object fieldVal, String fieldName) {
val globalExecutorProps = dtpProperties.getGlobalExecutorProps();
val taskWrappers = (Collection<?>) ReflectUtil.getFieldValue(fieldVal, "taskWrapperNames");
if (CollectionUtils.isEmpty(taskWrappers) &&
String prefix = MAIN_PROPERTIES_PREFIX + "." + fieldName;
if (!contains(prefix + ".taskWrapperNames[0]", dtpProperties) &&
CollectionUtils.isNotEmpty(globalExecutorProps.getTaskWrapperNames())) {
ReflectUtil.setFieldValue(fieldVal, "taskWrapperNames", globalExecutorProps.getTaskWrapperNames());
}
val platformIds = (List<?>) ReflectUtil.getFieldValue(fieldVal, PLATFORM_IDS);
if (CollectionUtils.isEmpty(platformIds) &&
if (!contains(prefix + ".platformIds[0]", dtpProperties) &&
CollectionUtils.isNotEmpty(globalExecutorProps.getPlatformIds())) {
ReflectUtil.setFieldValue(fieldVal, PLATFORM_IDS, globalExecutorProps.getPlatformIds());
}

val notifyItems = (List<?>) ReflectUtil.getFieldValue(fieldVal, NOTIFY_ITEMS);
if (CollectionUtils.isEmpty(notifyItems) &&
if (!contains(prefix + ".notifyItems[0].type", dtpProperties) &&
CollectionUtils.isNotEmpty(globalExecutorProps.getNotifyItems())) {
ReflectUtil.setFieldValue(fieldVal, NOTIFY_ITEMS, globalExecutorProps.getNotifyItems());
}

val awareNames = (List<?>) ReflectUtil.getFieldValue(fieldVal, AWARE_NAMES);
if (CollectionUtils.isEmpty(awareNames) &&
if (!contains(prefix + ".awareNames[0]", dtpProperties) &&
CollectionUtils.isNotEmpty(globalExecutorProps.getAwareNames())) {
ReflectUtil.setFieldValue(fieldVal, AWARE_NAMES, globalExecutorProps.getAwareNames());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public ThreadPoolExecutorProxy(ThreadPoolExecutor executor) {
executor.getKeepAliveTime(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS,
executor.getQueue(), executor.getThreadFactory(),
executor.getRejectedExecutionHandler());
allowCoreThreadTimeOut(executor.allowsCoreThreadTimeOut());
this.rejectHandlerType = getRejectedExecutionHandler().getClass().getSimpleName();
setRejectedExecutionHandler(RejectHandlerGetter.getProxy(getRejectedExecutionHandler()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public TomcatExecutorProxy(ThreadPoolExecutor executor) {
executor.getKeepAliveTime(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS,
executor.getQueue(), executor.getThreadFactory());
setThreadRenewalDelay(executor.getThreadRenewalDelay());
allowCoreThreadTimeOut(executor.allowsCoreThreadTimeOut());
Object handler = getRejectedExecutionHandler(executor);
this.rejectHandlerType = handler.getClass().getSimpleName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public EnhancedQueueExecutorProxy(final EnhancedQueueExecutor executor) {
.setTerminationTask(executor.getTerminationTask())
.setRegisterMBean(true)
.setMBeanName(executor.getMBeanName()));
allowCoreThreadTimeOut(executor.allowsCoreThreadTimeOut());
}

@Override
Expand Down

0 comments on commit 5c11b1e

Please sign in to comment.