|
|
|
|
@ -9,13 +9,15 @@ import org.springframework.beans.BeansException;
|
|
|
|
|
import org.springframework.beans.factory.DisposableBean;
|
|
|
|
|
import org.springframework.beans.factory.SmartInitializingSingleton;
|
|
|
|
|
import org.springframework.beans.factory.config.BeanDefinition;
|
|
|
|
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
|
|
import org.springframework.context.support.GenericApplicationContext;
|
|
|
|
|
import org.springframework.core.MethodIntrospector;
|
|
|
|
|
import org.springframework.core.annotation.AnnotatedElementUtils;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -27,6 +29,17 @@ import java.util.Map;
|
|
|
|
|
public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationContextAware, SmartInitializingSingleton, DisposableBean {
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(XxlJobSpringExecutor.class);
|
|
|
|
|
|
|
|
|
|
// ---------------------- field ----------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* excluded package, like "org.springframework"、"org.aaa,org.bbb"
|
|
|
|
|
*/
|
|
|
|
|
private String excludedPackage = "org.springframework.,spring.";
|
|
|
|
|
|
|
|
|
|
public void setExcludedPackage(String excludedPackage) {
|
|
|
|
|
this.excludedPackage = excludedPackage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------- start / stop ----------------------
|
|
|
|
|
|
|
|
|
|
@ -54,50 +67,76 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC
|
|
|
|
|
super.destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* check bean if excluded
|
|
|
|
|
*
|
|
|
|
|
* @param excludedPackageList excludedPackageList
|
|
|
|
|
* @param beanClassName beanClassName
|
|
|
|
|
* @return true if excluded
|
|
|
|
|
*/
|
|
|
|
|
private boolean isExcluded(List<String> excludedPackageList, String beanClassName) {
|
|
|
|
|
if (excludedPackageList == null || excludedPackageList.isEmpty() || beanClassName==null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (String excludedPackage : excludedPackageList) {
|
|
|
|
|
if (beanClassName.startsWith(excludedPackage)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* init job handler from method
|
|
|
|
|
*
|
|
|
|
|
* @param applicationContext applicationContext
|
|
|
|
|
*/
|
|
|
|
|
private void initJobHandlerMethodRepository(ApplicationContext applicationContext) {
|
|
|
|
|
// valid
|
|
|
|
|
if (applicationContext == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// build excluded package list
|
|
|
|
|
List<String> excludedPackageList = new ArrayList<>();
|
|
|
|
|
if (excludedPackage != null) {
|
|
|
|
|
for (String excludedPackage : excludedPackage.split(",")) {
|
|
|
|
|
if (!excludedPackage.trim().isEmpty()){
|
|
|
|
|
excludedPackageList.add(excludedPackage.trim());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// init job handler from method
|
|
|
|
|
String[] beanDefinitionNames = applicationContext.getBeanNamesForType(Object.class, false, false); // allowEagerInit=false, avoid early initialization
|
|
|
|
|
for (String beanDefinitionName : beanDefinitionNames) {
|
|
|
|
|
|
|
|
|
|
// skip system bean
|
|
|
|
|
if (isSystemBean(beanDefinitionName)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// analyse BeanDefinition
|
|
|
|
|
if (applicationContext instanceof BeanDefinitionRegistry beanDefinitionRegistry) {
|
|
|
|
|
// get BeanDefinition
|
|
|
|
|
if (!beanDefinitionRegistry.containsBeanDefinition(beanDefinitionName)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
BeanDefinition beanDefinition = beanDefinitionRegistry.getBeanDefinition(beanDefinitionName);
|
|
|
|
|
|
|
|
|
|
// skip lazy bean
|
|
|
|
|
if (applicationContext instanceof GenericApplicationContext genericApplicationContext) {
|
|
|
|
|
if (!genericApplicationContext.containsBeanDefinition(beanDefinitionName)) {
|
|
|
|
|
// skip excluded bean
|
|
|
|
|
String beanClassName = beanDefinition.getBeanClassName();
|
|
|
|
|
if (isExcluded(excludedPackageList, beanClassName)) {
|
|
|
|
|
logger.debug(">>>>>>>>>>> xxl-job bean-definition scan, skip excluded-package beanDefinitionName:{}, beanClassName:{}", beanDefinitionName, beanClassName);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// valid lazy bean
|
|
|
|
|
BeanDefinition beanDefinition = genericApplicationContext.getBeanDefinition(beanDefinitionName);
|
|
|
|
|
|
|
|
|
|
// skip lazy bean
|
|
|
|
|
if (beanDefinition.isLazyInit()) {
|
|
|
|
|
logger.debug("xxl-job bean-definition scan, skip lazy-init bean:{}", beanDefinitionName);
|
|
|
|
|
logger.debug(">>>>>>>>>>> xxl-job bean-definition scan, skip lazy-init beanDefinitionName:{}", beanDefinitionName);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// load bean
|
|
|
|
|
Object bean = applicationContext.getBean(beanDefinitionName);
|
|
|
|
|
/*
|
|
|
|
|
skip lazy bean2
|
|
|
|
|
Object bean = null;
|
|
|
|
|
Lazy onBean = applicationContext.findAnnotationOnBean(beanDefinitionName, Lazy.class);
|
|
|
|
|
if (onBean!=null){
|
|
|
|
|
logger.debug("xxl-job annotation scan, skip @Lazy Bean:{}", beanDefinitionName);
|
|
|
|
|
continue;
|
|
|
|
|
}else {
|
|
|
|
|
bean = applicationContext.getBean(beanDefinitionName);
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// filter method
|
|
|
|
|
Map<Method, XxlJob> annotatedMethods = null; // referred to :org.springframework.context.event.EventListenerMethodProcessor.processBean
|
|
|
|
|
@ -127,12 +166,6 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if system bean, not job bean
|
|
|
|
|
private boolean isSystemBean(String beanClassName) {
|
|
|
|
|
return beanClassName.startsWith("org.springframework")
|
|
|
|
|
|| beanClassName.startsWith("spring.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------- applicationContext ----------------------
|
|
|
|
|
private static ApplicationContext applicationContext;
|
|
|
|
|
|