From 7117b2a0027aab5b8661d75d914003d8f168ca6a Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Sun, 26 Oct 2025 00:26:17 +0800 Subject: [PATCH] =?UTF-8?q?-=2011=E3=80=81=E3=80=90=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E3=80=91=E6=89=A7=E8=A1=8C=E5=99=A8=E4=BB=BB=E5=8A=A1Bean?= =?UTF-8?q?=E6=89=AB=E6=8F=8F=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96=EF=BC=8C?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=87=92=E5=8A=A0=E8=BD=BDBean=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E5=8F=8A=E8=BF=87=E6=BB=A4=E6=9C=BA=E5=88=B6=EF=BC=9B?= =?UTF-8?q?=20-=2012=E3=80=81=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E5=99=A8=E6=96=B0=E5=A2=9E=E2=80=9C=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E6=89=AB=E6=8F=8F=E6=8E=92=E9=99=A4=E8=B7=AF=E5=BE=84=E2=80=9D?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=A1=B9(xxl.job.executor.excludedpackage)?= =?UTF-8?q?=EF=BC=8C=E4=BB=BB=E5=8A=A1=E6=89=AB=E6=8F=8F=E6=97=B6=E5=BF=BD?= =?UTF-8?q?=E7=95=A5=E6=8C=87=E5=AE=9A=E5=8C=85=E8=B7=AF=E5=BE=84=E4=B8=8B?= =?UTF-8?q?=E7=9A=84Bean=EF=BC=9B=E6=94=AF=E6=8C=81=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E5=8C=85=E8=B7=AF=E5=BE=84=E3=80=81=E9=80=97?= =?UTF-8?q?=E5=8F=B7=E5=88=86=E9=9A=94=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/XXL-JOB官方文档.md | 2 +- .../xxl/job/core/executor/XxlJobExecutor.java | 6 +- .../executor/impl/XxlJobSimpleExecutor.java | 4 +- .../executor/impl/XxlJobSpringExecutor.java | 117 +++++++++++------- 4 files changed, 75 insertions(+), 54 deletions(-) diff --git a/doc/XXL-JOB官方文档.md b/doc/XXL-JOB官方文档.md index 5dfd095c..58081f3b 100644 --- a/doc/XXL-JOB官方文档.md +++ b/doc/XXL-JOB官方文档.md @@ -981,7 +981,7 @@ xxl.job.executor.port=9999 xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler ### 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能; xxl.job.executor.logretentiondays=30 -### 任务扫描排除路径,任务扫描时忽略指定包路径下的Bean;支持配置多个包路径、逗号分隔; +### 任务扫描排除路径 [选填] :任务扫描时忽略指定包路径下的Bean;支持配置包路径前缀,多个逗号分隔; xxl.job.executor.excludedpackage=org.springframework,spring ``` diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java b/xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java index 443090d7..ae94795d 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java @@ -183,11 +183,11 @@ public class XxlJobExecutor { public static IJobHandler loadJobHandler(String name){ return jobHandlerRepository.get(name); } - public static IJobHandler registJobHandler(String name, IJobHandler jobHandler){ + public static IJobHandler registryJobHandler(String name, IJobHandler jobHandler){ logger.info(">>>>>>>>>>> xxl-job register jobhandler success, name:{}, jobHandler:{}", name, jobHandler); return jobHandlerRepository.put(name, jobHandler); } - protected void registJobHandler(XxlJob xxlJob, Object bean, Method executeMethod){ + protected void registryJobHandler(XxlJob xxlJob, Object bean, Method executeMethod){ if (xxlJob == null) { return; } @@ -237,7 +237,7 @@ public class XxlJobExecutor { } // registry jobhandler - registJobHandler(name, new MethodJobHandler(bean, executeMethod, initMethod, destroyMethod)); + registryJobHandler(name, new MethodJobHandler(bean, executeMethod, initMethod, destroyMethod)); } diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSimpleExecutor.java b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSimpleExecutor.java index 53efbb95..aa2bf868 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSimpleExecutor.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSimpleExecutor.java @@ -2,14 +2,12 @@ package com.xxl.job.core.executor.impl; import com.xxl.job.core.executor.XxlJobExecutor; import com.xxl.job.core.handler.annotation.XxlJob; -import com.xxl.job.core.handler.impl.MethodJobHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; -import java.util.Map; /** @@ -65,7 +63,7 @@ public class XxlJobSimpleExecutor extends XxlJobExecutor { for (Method executeMethod : methods) { XxlJob xxlJob = executeMethod.getAnnotation(XxlJob.class); // registry - registJobHandler(xxlJob, bean, executeMethod); + registryJobHandler(xxlJob, bean, executeMethod); } } diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSpringExecutor.java b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSpringExecutor.java index 55faee1e..f313ed90 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSpringExecutor.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSpringExecutor.java @@ -43,12 +43,14 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC // ---------------------- start / stop ---------------------- - // start + /** + * start + */ @Override public void afterSingletonsInstantiated() { - // init JobHandler Repository (for method) - initJobHandlerMethodRepository(applicationContext); + // scan JobHandler method + scanJobHandlerMethod(applicationContext); // refresh GlueFactory GlueFactory.refreshInstance(1); @@ -61,45 +63,27 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC } } - // destroy + /** + * stop + */ @Override public void destroy() { super.destroy(); } - /** - * check bean if excluded - * - * @param excludedPackageList excludedPackageList - * @param beanClassName beanClassName - * @return true if excluded - */ - private boolean isExcluded(List 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) { + private void scanJobHandlerMethod(ApplicationContext applicationContext) { // valid if (applicationContext == null) { return; } - // build excluded package list + // 1、build excluded-package list List excludedPackageList = new ArrayList<>(); if (excludedPackage != null) { for (String excludedPackage : excludedPackage.split(",")) { @@ -109,39 +93,50 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC } } - // init job handler from method - String[] beanDefinitionNames = applicationContext.getBeanNamesForType(Object.class, false, false); // allowEagerInit=false, avoid early initialization - for (String beanDefinitionName : beanDefinitionNames) { + // 2、scan bean form jobhandler + String[] beanNames = applicationContext.getBeanNamesForType(Object.class, false, false); // allowEagerInit=false, avoid early initialization + for (String beanName : beanNames) { - // analyse BeanDefinition + /** + * 2.1、skip by BeanDefinition: + * - skip excluded-package bean + * - skip lazy-init bean + */ if (applicationContext instanceof BeanDefinitionRegistry beanDefinitionRegistry) { // get BeanDefinition - if (!beanDefinitionRegistry.containsBeanDefinition(beanDefinitionName)) { + if (!beanDefinitionRegistry.containsBeanDefinition(beanName)) { continue; } - BeanDefinition beanDefinition = beanDefinitionRegistry.getBeanDefinition(beanDefinitionName); + BeanDefinition beanDefinition = beanDefinitionRegistry.getBeanDefinition(beanName); - // skip excluded bean + // skip excluded-package bean String beanClassName = beanDefinition.getBeanClassName(); if (isExcluded(excludedPackageList, beanClassName)) { - logger.debug(">>>>>>>>>>> xxl-job bean-definition scan, skip excluded-package beanDefinitionName:{}, beanClassName:{}", beanDefinitionName, beanClassName); + logger.debug(">>>>>>>>>>> xxl-job bean-definition scan, skip excluded-package beanName:{}, beanClassName:{}", beanName, beanClassName); continue; } - // skip lazy bean + // skip lazy-init bean if (beanDefinition.isLazyInit()) { - logger.debug(">>>>>>>>>>> xxl-job bean-definition scan, skip lazy-init beanDefinitionName:{}", beanDefinitionName); + logger.debug(">>>>>>>>>>> xxl-job bean-definition scan, skip lazy-init beanName:{}", beanName); continue; } } - // load bean - Object bean = applicationContext.getBean(beanDefinitionName); - + /** + * 2.2、skip by BeanDefinition Class + * - skip beanClass is null + * - skip method annotation(@XxlJob) is null + */ + Class beanClass = applicationContext.getType(beanName, false); + if (beanClass == null) { + logger.debug(">>>>>>>>>>> xxl-job bean-definition scan, skip beanClass-null beanName:{}", beanName); + continue; + } // filter method - Map annotatedMethods = null; // referred to :org.springframework.context.event.EventListenerMethodProcessor.processBean + Map annotatedMethods = null; try { - annotatedMethods = MethodIntrospector.selectMethods(bean.getClass(), + annotatedMethods = MethodIntrospector.selectMethods(beanClass, new MethodIntrospector.MetadataLookup() { @Override public XxlJob inspect(Method method) { @@ -149,23 +144,51 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC } }); } catch (Throwable ex) { - logger.error("xxl-job method-jobhandler resolve error for bean[" + beanDefinitionName + "].", ex); + logger.error(">>>>>>>>>>> xxl-job method-jobhandler resolve error for bean[" + beanName + "].", ex); } if (annotatedMethods==null || annotatedMethods.isEmpty()) { continue; } - // generate and regist method job handler - for (Map.Entry methodXxlJobEntry : annotatedMethods.entrySet()) { - Method executeMethod = methodXxlJobEntry.getKey(); - XxlJob xxlJob = methodXxlJobEntry.getValue(); + // 2.3、scan + registry Jobhandler + Object jobBean = applicationContext.getBean(beanName); + for (Map.Entry jobMethodEntry : annotatedMethods.entrySet()) { + Method jobMethod = jobMethodEntry.getKey(); + XxlJob xxlJob = jobMethodEntry.getValue(); // regist - registJobHandler(xxlJob, bean, executeMethod); + registryJobHandler(xxlJob, jobBean, jobMethod); } } } + /** + * check bean if excluded + * + * @param excludedPackageList excludedPackageList + * @param beanClassName beanClassName + * @return true if excluded + */ + private boolean isExcluded(List excludedPackageList, String beanClassName) { + // excludedPackageList is empty, no excluded + if (excludedPackageList == null || excludedPackageList.isEmpty()) { + return false; + } + + // beanClassName is null, no excluded + if (beanClassName == null) { + return false; + } + + // excludedPackageList match, excluded (not scan) + for (String excludedPackage : excludedPackageList) { + if (beanClassName.startsWith(excludedPackage)) { + return true; + } + } + return false; + } + // ---------------------- applicationContext ---------------------- private static ApplicationContext applicationContext;