Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature:支持扩展,目前先支持DtpExecutor和ScheduledDtpExecutor的扩展 #288

Merged
merged 6 commits into from
Jun 28, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ private DynamicTpConst() {
public static final String QUEUE_TIMEOUT = "queueTimeout";

public static final String TASK_WRAPPERS = "taskWrappers";

/**
* alarm
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.entity;

import lombok.Data;

@Data
public class DtpExtensionProps {

private String extensionPath;

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.dromara.dynamictp.common.constant.DynamicTpConst;
import org.dromara.dynamictp.common.entity.DtpExecutorProps;
import org.dromara.dynamictp.common.entity.DtpExtensionProps;
import org.dromara.dynamictp.common.entity.NotifyPlatform;
import org.dromara.dynamictp.common.entity.TpExecutorProps;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -80,6 +81,11 @@ public class DtpProperties {
*/
private List<NotifyPlatform> platforms;

/**
* extension configs
*/
private List<DtpExtensionProps> extensions;

/**
* Nacos config.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,6 @@ private static InetAddress getLocalHostExactAddress() throws SocketException, Un
}
return candidateAddress == null ? InetAddress.getLocalHost() : candidateAddress;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.dromara.dynamictp.common.util;

import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -62,4 +63,16 @@ public static Field getField(Class<?> targetClass, String fieldName) {
ReflectionUtils.makeAccessible(field);
return field;
}

public static Object resolveObject(String path) {
if (StringUtils.isBlank(path)) {
return null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use StringUtils.isBlack()

}

try {
return Class.forName(path).newInstance();
} catch (Exception e) {
throw new RuntimeException("Error resolving object. Cause: " + e, e);
}
}
}
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
</dependency>

<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;


import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -69,7 +70,6 @@ public class DtpRegistry implements ApplicationRunner {
* Maintain all automatically registered and manually registered Executors(DtpExecutors and JUC ThreadPoolExecutors).
*/
private static final Map<String, ExecutorWrapper> EXECUTOR_REGISTRY = new ConcurrentHashMap<>();

/**
* equator for comparing two TpMainFields
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.core.plugin;

import org.dromara.dynamictp.core.thread.DtpExecutor;

import java.lang.reflect.Constructor;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

public class ConstructorUtil {

public static Object[] buildDtpExecutorConstructorArgs(DtpExecutor dtpExecutor) {

return new Object[] {
dtpExecutor.getCorePoolSize(),
dtpExecutor.getMaximumPoolSize(),
dtpExecutor.getKeepAliveTime(TimeUnit.MILLISECONDS),
TimeUnit.MILLISECONDS,
dtpExecutor.getQueue(),
dtpExecutor.getThreadFactory(),
dtpExecutor.getRejectedExecutionHandler()
};
}

public static Class[] buildDtpExecutorConstructorArgTypes() {

return new Class[] {
int.class,
int.class,
long.class,
TimeUnit.class,
BlockingQueue.class,
ThreadFactory.class,
RejectedExecutionHandler.class
};
}


public static Class[] buildConstructorArgs() {

return null;
}

public static Class[] buildConstructorArgTypes(Object obj) {

Class clazz = obj.getClass();
Constructor[] constructors = clazz.getConstructors();
if (constructors != null && constructors.length > 0) {
return constructors[0].getParameterTypes();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.core.plugin;


import java.lang.reflect.InvocationTargetException;

/**
* Extension interface
* @author windsearcher.lq
* @since 22:22
*/
public interface DtpExtension {

Object intercept(DtpInvocation invocation) throws InvocationTargetException, IllegalAccessException;

default Object plugin(Object target) {
return DtpExtensionProxyFactory.wrap(target, this);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an overloaded method.

default Object plugin(Object target, Class[] argumentTypes, Object[] arguments) { }


default Object plugin(Object target, Class[] argumentTypes, Object[] arguments) {
return DtpExtensionProxyFactory.wrap(target, argumentTypes, arguments, this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.core.plugin;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author windsearcher.lq
* @since 2023/6/9 20:47
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface DtpExtensionPoint {

DtpSignature[] value();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.core.plugin;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add Apache Licenses to the class header


import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.commons.collections4.CollectionUtils;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;

/**
* Proxy packaging class
* @author windsearcher.lq
* @since 2023/6/9 21:02
*/
public class DtpExtensionProxy implements MethodInterceptor {

private final Object target;

private final DtpExtension interceptor;

private final Map<Class<?>, Set<Method>> signatureMap;

public DtpExtensionProxy(Object target, DtpExtension interceptor, Map<Class<?>, Set<Method>> signatureMap) {
this.target = target;
this.interceptor = interceptor;
this.signatureMap = signatureMap;
}

@Override
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
Set<Method> methods = signatureMap.get(method.getDeclaringClass());
if (CollectionUtils.isNotEmpty(methods) && methods.contains(method)) {
return interceptor.intercept(new DtpInvocation(target, method, args));
WindSearcher marked this conversation as resolved.
Show resolved Hide resolved
}

return method.invoke(target, args);
}
}
Loading