Skip to content

Commit

Permalink
Merge branch 'springboot3' into virtual-thread
Browse files Browse the repository at this point in the history
  • Loading branch information
EachannChan authored Nov 18, 2024
2 parents c40e854 + 6f515f3 commit 4027731
Show file tree
Hide file tree
Showing 31 changed files with 164 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
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.ExecutorAdapter;
import org.dromara.dynamictp.core.support.adapter.ExecutorAdapter;
import org.dromara.dynamictp.core.support.ExecutorWrapper;
import org.dromara.dynamictp.core.support.proxy.ThreadPoolExecutorProxy;
import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ private DynamicTpConst() { }

public static final String UNKNOWN = "---";

public static final String VALUE = "value";

public static final String TRACE_ID = "traceId";

public static final String GLOBAL_CONFIG_PREFIX = MAIN_PROPERTIES_PREFIX + ".globalExecutorProps.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* Interface for managing context in the application.
* Provides methods to access beans, set context, handle events,
* and retrieve environment properties and profiles.
* and retrieve environment properties.
*
* @author vzer200
* @since 1.2.0
Expand Down Expand Up @@ -73,25 +73,20 @@ public interface ContextManager {
String getEnvironmentProperty(String key);

/**
* Retrieves an environment property by its key, with a default value.
* Retrieves an environment property by its key in the specified environment.
*
* @param key the key of the property
* @param defaultValue the default value to return if the property is not found
* @return the value of the property, or the default value if not found
*/
String getEnvironmentProperty(String key, String defaultValue);

/**
* Retrieves the active profiles.
*
* @return an array of active profile names
* @param environment the specified environment object
* @return the value of the property, or null if not found
*/
String[] getActiveProfiles();
String getEnvironmentProperty(String key, Object environment);

/**
* Retrieves the default profiles.
* Retrieves an environment property by its key, with a default value.
*
* @return an array of default profile names
* @param key the key of the property
* @param defaultValue the default value to return if the property is not found
* @return the value of the property, or the default value if not found
*/
String[] getDefaultProfiles();
String getEnvironmentProperty(String key, String defaultValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,56 @@

package org.dromara.dynamictp.common.manager;

import lombok.extern.slf4j.Slf4j;
import org.dromara.dynamictp.common.util.ExtensionServiceLoader;

import java.util.Map;

/**
* Helper class for accessing ContextManager and publishing events.
* Helper class for accessing ContextManager.
*
* @author vzer200
* @since 1.2.0
*/
@Slf4j
public class ContextManagerHelper {

private static final ContextManager CONTEXT_MANAGER;
private static ContextManager contextManager;

static {
CONTEXT_MANAGER = ExtensionServiceLoader.getFirst(ContextManager.class);
if (CONTEXT_MANAGER == null) {
contextManager = ExtensionServiceLoader.getFirst(ContextManager.class);
if (contextManager == null) {
contextManager = new NullContextManager();
throw new IllegalStateException("No ContextManager implementation found");
}
}

public static <T> T getBean(Class<T> clazz) {
return CONTEXT_MANAGER.getBean(clazz);
return contextManager.getBean(clazz);
}

public static <T> T getBean(String name, Class<T> clazz) {
return CONTEXT_MANAGER.getBean(name, clazz);
return contextManager.getBean(name, clazz);
}

public static <T> Map<String, T> getBeansOfType(Class<T> clazz) {
return CONTEXT_MANAGER.getBeansOfType(clazz);
return contextManager.getBeansOfType(clazz);
}

public static Object getEnvironment() {
return CONTEXT_MANAGER.getEnvironment();
return contextManager.getEnvironment();
}

public static String getEnvironmentProperty(String key) {
return CONTEXT_MANAGER.getEnvironmentProperty(key);
return contextManager.getEnvironmentProperty(key);
}

public static String getEnvironmentProperty(String key, String defaultValue) {
return CONTEXT_MANAGER.getEnvironmentProperty(key, defaultValue);
}

public static String[] getActiveProfiles() {
return CONTEXT_MANAGER.getActiveProfiles();
public static String getEnvironmentProperty(String key, Object environment) {
return contextManager.getEnvironmentProperty(key, environment);
}

public static String[] getDefaultProfiles() {
return CONTEXT_MANAGER.getDefaultProfiles();
public static String getEnvironmentProperty(String key, String defaultValue) {
return contextManager.getEnvironmentProperty(key, defaultValue);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dromara.dynamictp.common.manager;

import java.util.Map;

/**
* NullContextManager related
*
* @author yanhom
* @since 1.2.0
*/
public class NullContextManager implements ContextManager {

@Override
public <T> T getBean(Class<T> clazz) {
throw new UnsupportedOperationException();
}

@Override
public <T> T getBean(String name, Class<T> clazz) {
throw new UnsupportedOperationException();
}

@Override
public <T> Map<String, T> getBeansOfType(Class<T> clazz) {
throw new UnsupportedOperationException();
}

@Override
public Object getEnvironment() {
throw new UnsupportedOperationException();
}

@Override
public String getEnvironmentProperty(String key) {
throw new UnsupportedOperationException();
}

@Override
public String getEnvironmentProperty(String key, Object environment) {
throw new UnsupportedOperationException();
}

@Override
public String getEnvironmentProperty(String key, String defaultValue) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* BeanCopierUtils related
*
* @author vzer200
* @since 1.1.8
* @since 1.2.0
*/
@UtilityClass
public class BeanCopierUtil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,8 @@ private CommonUtil() {

String env = DtpProperties.getInstance().getEnv();
if (StringUtils.isBlank(env)) {
// fix #I8SSGQ
env = ContextManagerHelper.getEnvironmentProperty(APP_ENV_KEY);
}
if (StringUtils.isBlank(env)) {
String[] profiles = ContextManagerHelper.getActiveProfiles();
if (profiles.length < 1) {
profiles = ContextManagerHelper.getDefaultProfiles();
}
if (profiles.length >= 1) {
env = profiles[0];
}
}

String appName = ContextManagerHelper.getEnvironmentProperty(APP_NAME_KEY);
String portStr = ContextManagerHelper.getEnvironmentProperty(APP_PORT_KEY);
int port = StringUtils.isNotBlank(portStr) ? Integer.parseInt(portStr) : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* DtpPropertiesBinderUtil related
*
* @author yanhom
* @since 1.1.0
* @since 1.1.9
*/
@SuppressWarnings("unchecked")
public final class DtpPropertiesBinderUtil {
Expand Down Expand Up @@ -123,16 +123,7 @@ private static Object getProperty(String key, Object environment) {
Map<?, Object> properties = (Map<?, Object>) environment;
return properties.get(key);
} else {
return ContextManagerHelper.getEnvironmentProperty(key);
}
}

private static boolean contains(String key, Object environment) {
if (environment instanceof Map) {
Map<?, Object> properties = (Map<?, Object>) environment;
return properties.containsKey(key);
} else {
return StringUtils.isNotBlank(ContextManagerHelper.getEnvironmentProperty(key));
return ContextManagerHelper.getEnvironmentProperty(key, environment);
}
}

Expand All @@ -159,29 +150,42 @@ private static void setBasicField(Object source, Field field, Object executor, S
}

private static void setCollectionField(Object source, DtpExecutorProps globalExecutorProps, Object executor, String prefix) {
if (!contains(prefix + ".taskWrapperNames[0]", source) &&
if (isNotContains(prefix + ".taskWrapperNames[0]", source) &&
CollectionUtils.isNotEmpty(globalExecutorProps.getTaskWrapperNames())) {
ReflectUtil.setFieldValue(executor, "taskWrapperNames", globalExecutorProps.getTaskWrapperNames());
}
if (!contains(prefix + ".platformIds[0]", source) &&
if (isNotContains(prefix + ".platformIds[0]", source) &&
CollectionUtils.isNotEmpty(globalExecutorProps.getPlatformIds())) {
ReflectUtil.setFieldValue(executor, PLATFORM_IDS, globalExecutorProps.getPlatformIds());
}
if (!contains(prefix + ".notifyItems[0].type", source) &&
if (isNotContains(prefix + ".notifyItems[0].type", source) &&
CollectionUtils.isNotEmpty(globalExecutorProps.getNotifyItems())) {
ReflectUtil.setFieldValue(executor, NOTIFY_ITEMS, globalExecutorProps.getNotifyItems());
}
if (!contains(prefix + ".awareNames[0]", source) &&
if (isNotContains(prefix + ".awareNames[0]", source) &&
CollectionUtils.isNotEmpty(globalExecutorProps.getAwareNames())) {
ReflectUtil.setFieldValue(executor, AWARE_NAMES, globalExecutorProps.getAwareNames());
}
try {
if (!contains(prefix + ".pluginNames[0]", source) &&
if (isNotContains(prefix + ".pluginNames[0]", source) &&
CollectionUtils.isNotEmpty(globalExecutorProps.getPluginNames())) {
ReflectUtil.setFieldValue(executor, PLUGIN_NAMES, globalExecutorProps.getPluginNames());
}
} catch (Exception e) {
// ignore
}
}

private static boolean isNotContains(String key, Object environment) {
return !contains(key, environment);
}

private static boolean contains(String key, Object environment) {
if (environment instanceof Map) {
Map<?, Object> properties = (Map<?, Object>) environment;
return properties.containsKey(key);
} else {
return StringUtils.isNotBlank(ContextManagerHelper.getEnvironmentProperty(key, environment));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
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.ExecutorAdapter;
import org.dromara.dynamictp.core.support.adapter.ExecutorAdapter;
import org.dromara.dynamictp.core.support.ExecutorWrapper;
import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper;
import org.dromara.dynamictp.core.support.task.wrapper.TaskWrappers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import cn.hutool.core.text.CharSequenceUtil;
import lombok.extern.slf4j.Slf4j;
import org.dromara.dynamictp.core.notifier.manager.AlarmManager;
import org.dromara.dynamictp.core.support.ExecutorAdapter;
import org.dromara.dynamictp.core.support.adapter.ExecutorAdapter;
import org.dromara.dynamictp.core.support.ThreadPoolStatProvider;
import org.slf4j.MDC;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.dromara.dynamictp.common.entity.TpMainFields;
import org.dromara.dynamictp.core.executor.DtpExecutor;
import org.dromara.dynamictp.core.monitor.PerformanceProvider;
import org.dromara.dynamictp.core.support.ExecutorAdapter;
import org.dromara.dynamictp.core.support.adapter.ExecutorAdapter;
import org.dromara.dynamictp.core.support.ExecutorWrapper;
import org.dromara.dynamictp.core.support.ThreadPoolStatProvider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.dromara.dynamictp.core.aware.TaskEnhanceAware;
import org.dromara.dynamictp.core.notifier.manager.NotifyHelper;
import org.dromara.dynamictp.core.reject.RejectHandlerGetter;
import org.dromara.dynamictp.core.support.ExecutorAdapter;
import org.dromara.dynamictp.core.support.adapter.ExecutorAdapter;
import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.dromara.dynamictp.core.notifier.capture;

import org.dromara.dynamictp.core.support.ExecutorAdapter;
import org.dromara.dynamictp.core.support.adapter.ExecutorAdapter;

import java.util.AbstractQueue;
import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.dromara.dynamictp.core.notifier.AbstractDtpNotifier;
import org.dromara.dynamictp.core.notifier.context.BaseNotifyCtx;
import org.dromara.dynamictp.core.notifier.manager.AlarmManager;
import org.dromara.dynamictp.core.support.ExecutorAdapter;
import org.dromara.dynamictp.core.support.adapter.ExecutorAdapter;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.dromara.dynamictp.core.handler.ConfigHandler;
import org.dromara.dynamictp.core.support.binder.BinderHelper;


import java.io.IOException;
import java.util.Map;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.dromara.dynamictp.core.notifier.manager.AlarmManager;
import org.dromara.dynamictp.core.reject.RejectHandlerGetter;
import org.dromara.dynamictp.core.support.proxy.VirtualThreadExecutorProxy;
import org.dromara.dynamictp.core.support.adapter.ExecutorAdapter;
import org.dromara.dynamictp.core.support.adapter.ThreadPoolExecutorAdapter;
import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper;

import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.dromara.dynamictp.core.support;
package org.dromara.dynamictp.core.support.adapter;

import java.util.AbstractQueue;
import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.dromara.dynamictp.core.support;
package org.dromara.dynamictp.core.support.adapter;

import org.dromara.dynamictp.core.aware.RejectHandlerAware;

Expand Down
Loading

0 comments on commit 4027731

Please sign in to comment.