mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
抽取Spring API为公共抽象父类
This commit is contained in:
parent
46165ccd97
commit
47b050aba2
@ -0,0 +1,65 @@
|
||||
package tech.powerjob.worker.processor.impl;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import tech.powerjob.common.enums.ProcessorType;
|
||||
import tech.powerjob.worker.extension.processor.ProcessorFactory;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@Slf4j
|
||||
public abstract class AbstractBuildInSpringProcessorFactory implements ProcessorFactory {
|
||||
|
||||
protected final ApplicationContext applicationContext;
|
||||
|
||||
protected AbstractBuildInSpringProcessorFactory(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> supportTypes() {
|
||||
return Sets.newHashSet(ProcessorType.BUILT_IN.name());
|
||||
}
|
||||
|
||||
protected boolean checkCanLoad() {
|
||||
try {
|
||||
ApplicationContext.class.getClassLoader();
|
||||
return applicationContext != null;
|
||||
} catch (Throwable ignore) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected static <T> T getBean(String className, ApplicationContext ctx) throws Exception {
|
||||
|
||||
// 0. 尝试直接用 Bean 名称加载
|
||||
try {
|
||||
final Object bean = ctx.getBean(className);
|
||||
if (bean != null) {
|
||||
return (T) bean;
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
|
||||
// 1. ClassLoader 存在,则直接使用 clz 加载
|
||||
ClassLoader classLoader = ctx.getClassLoader();
|
||||
if (classLoader != null) {
|
||||
return (T) ctx.getBean(classLoader.loadClass(className));
|
||||
}
|
||||
// 2. ClassLoader 不存在(系统类加载器不可见),尝试用类名称小写加载
|
||||
String[] split = className.split("\\.");
|
||||
String beanName = split[split.length - 1];
|
||||
// 小写转大写
|
||||
char[] cs = beanName.toCharArray();
|
||||
cs[0] += 32;
|
||||
String beanName0 = String.valueOf(cs);
|
||||
log.warn("[SpringUtils] can't get ClassLoader from context[{}], try to load by beanName:{}", ctx, beanName0);
|
||||
return (T) ctx.getBean(beanName0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -1,20 +1,16 @@
|
||||
package tech.powerjob.worker.processor.impl;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import tech.powerjob.common.enums.ProcessorType;
|
||||
import tech.powerjob.worker.annotation.PowerJob;
|
||||
import tech.powerjob.worker.extension.processor.ProcessorBean;
|
||||
import tech.powerjob.worker.extension.processor.ProcessorDefinition;
|
||||
import tech.powerjob.worker.extension.processor.ProcessorFactory;
|
||||
import tech.powerjob.worker.processor.MethodBasicProcessor;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 内建的 SpringBean 处理器工厂,用于加载 Spring 管理Bean下的方法(使用PowerJob注解),非核心依赖
|
||||
@ -24,9 +20,7 @@ import java.util.Set;
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
public class BuildInSpringMethodProcessorFactory implements ProcessorFactory {
|
||||
|
||||
private final ApplicationContext applicationContext;
|
||||
public class BuildInSpringMethodProcessorFactory extends AbstractBuildInSpringProcessorFactory {
|
||||
|
||||
private static final List<String> jobHandlerRepository = new LinkedList<>();
|
||||
|
||||
@ -34,15 +28,10 @@ public class BuildInSpringMethodProcessorFactory implements ProcessorFactory {
|
||||
|
||||
|
||||
public BuildInSpringMethodProcessorFactory(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
super(applicationContext);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Set<String> supportTypes() {
|
||||
return Sets.newHashSet(ProcessorType.BUILT_IN.name());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProcessorBean build(ProcessorDefinition processorDefinition) {
|
||||
try {
|
||||
@ -91,7 +80,6 @@ public class BuildInSpringMethodProcessorFactory implements ProcessorFactory {
|
||||
log.warn("[ProcessorFactory] load by BuiltInSpringProcessorFactory failed. If you are using Spring, make sure this bean was managed by Spring", t);
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -104,43 +92,5 @@ public class BuildInSpringMethodProcessorFactory implements ProcessorFactory {
|
||||
return jobHandlerRepository.contains(name);
|
||||
}
|
||||
|
||||
private boolean checkCanLoad() {
|
||||
try {
|
||||
ApplicationContext.class.getClassLoader();
|
||||
return applicationContext != null;
|
||||
} catch (Throwable ignore) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Object getBean(String className, ApplicationContext ctx) throws Exception {
|
||||
|
||||
// 0. 尝试直接用 Bean 名称加载
|
||||
try {
|
||||
final Object bean = ctx.getBean(className);
|
||||
if (bean != null) {
|
||||
return bean;
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
|
||||
// 1. ClassLoader 存在,则直接使用 clz 加载
|
||||
ClassLoader classLoader = ctx.getClassLoader();
|
||||
if (classLoader != null) {
|
||||
return ctx.getBean(classLoader.loadClass(className));
|
||||
}
|
||||
// 2. ClassLoader 不存在(系统类加载器不可见),尝试用类名称小写加载
|
||||
String[] split = className.split("\\.");
|
||||
String beanName = split[split.length - 1];
|
||||
// 小写转大写
|
||||
char[] cs = beanName.toCharArray();
|
||||
cs[0] += 32;
|
||||
String beanName0 = String.valueOf(cs);
|
||||
log.warn("[SpringUtils] can't get ClassLoader from context[{}], try to load by beanName:{}", ctx, beanName0);
|
||||
return ctx.getBean(beanName0);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -19,17 +19,11 @@ import java.util.Set;
|
||||
* @since 2023/1/17
|
||||
*/
|
||||
@Slf4j
|
||||
public class BuiltInSpringProcessorFactory implements ProcessorFactory {
|
||||
public class BuiltInSpringProcessorFactory extends AbstractBuildInSpringProcessorFactory {
|
||||
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
public BuiltInSpringProcessorFactory(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> supportTypes() {
|
||||
return Sets.newHashSet(ProcessorType.BUILT_IN.name());
|
||||
super(applicationContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -59,42 +53,5 @@ public class BuiltInSpringProcessorFactory implements ProcessorFactory {
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean checkCanLoad() {
|
||||
try {
|
||||
ApplicationContext.class.getClassLoader();
|
||||
return applicationContext != null;
|
||||
} catch (Throwable ignore) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> T getBean(String className, ApplicationContext ctx) throws Exception {
|
||||
|
||||
// 0. 尝试直接用 Bean 名称加载
|
||||
try {
|
||||
final Object bean = ctx.getBean(className);
|
||||
if (bean != null) {
|
||||
return (T) bean;
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
|
||||
// 1. ClassLoader 存在,则直接使用 clz 加载
|
||||
ClassLoader classLoader = ctx.getClassLoader();
|
||||
if (classLoader != null) {
|
||||
return (T) ctx.getBean(classLoader.loadClass(className));
|
||||
}
|
||||
// 2. ClassLoader 不存在(系统类加载器不可见),尝试用类名称小写加载
|
||||
String[] split = className.split("\\.");
|
||||
String beanName = split[split.length - 1];
|
||||
// 小写转大写
|
||||
char[] cs = beanName.toCharArray();
|
||||
cs[0] += 32;
|
||||
String beanName0 = String.valueOf(cs);
|
||||
log.warn("[SpringUtils] can't get ClassLoader from context[{}], try to load by beanName:{}", ctx, beanName0);
|
||||
return (T) ctx.getBean(beanName0);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user