XXL-Job启动源码详解

本文详细解析了XXL-Job服务的启动流程,重点介绍了XxlJobSpringExecutor如何加载和管理任务,包括扫描注解、初始化任务、执行回调等关键步骤。通过对Spring的ApplicationContextAware和SmartInitializingSingleton接口的利用,实现任务的自动加载。启动过程涉及到的日志初始化、RPC回调线程启动以及执行器端口初始化等环节也进行了详解。
摘要由CSDN通过智能技术生成

服务主要启动是初始化XxlJobSpringExecutor这个bean对象,该对象定义了执行器xxlJobSpringExecutor的相关配置,如注册中心地址,服务提供地址,以及授权token等。该对象类图如下,

XxlJobSpringExecutor执行器继承XxlJobExecutor并实现Spring的ApplicationContextAware等类,对该bean进行了增强。主要核心类为XxlJobExecutor。

因为继承SmartInitializingSingleton,所以bean初始化完成之后会执行afterSingletonsInstantiated方法,该类主要为initJobHandlerMethodRepository这个方法,用于扫描xxl-job注解,进行任务加载和管理

// start

@Override

public void afterSingletonsInstantiated() {

// 扫描xxl-job注解,进行任务加载和管理

initJobHandlerMethodRepository(applicationContext);

// refresh GlueFactory

GlueFactory.refreshInstance(1);

// super start

try {

super.start();

} catch (Exception e) {

throw new RuntimeException(e);

}

}

initJobHandlerMethodRepository方法主要如下

该方法主要是通过获取Spring管理的容器bean,然后扫描带有xxljob注解的方法,将他们保存在jobHandlerRepository对象中

private void initJobHandlerMethodRepository(ApplicationContext applicationContext) {

if (applicationContext == null) {

return;

}

// 扫描Spring管理的bean

String[] beanDefinitionNames = applicationContext.getBeanNamesForType(Object.class, false, true);

for (String beanDefinitionName : beanDefinitionNames) {

Object bean = applicationContext.getBean(beanDefinitionName);

Map<Method, XxlJob> annotatedMethods = null; // referred to :org.springframework.context.event.EventListenerMethodProcessor.processBean

try {

annotatedMethods = MethodIntrospector.selectMethods(bean.getClass(),

new MethodIntrospector.MetadataLookup<XxlJob>() {

@Override

public XxlJob inspect(Method method) {

// 获取注解为XxlJob的方法,并保存在annotatedMethods

return AnnotatedElementUtils.findMergedAnnotation(method, XxlJob.class);

}

});

} catch (Throwable ex) {

logger.error(“xxl-job method-jobhandler resolve error for bean[” + beanDefinitionName + “].”, ex);

}

if (annotatedMethods==null || annotatedMethods.isEmpty()) {

continue;

}

//获取方法属性,并存储在jobHandlerRepository对象中

for (Map.Entry<Method, XxlJob> methodXxlJobEntry : annotatedMethods.entrySet()) {

Method method = methodXxlJobEntry.getKey();

XxlJob xxlJob = methodXxlJobEntry.getValue();

if (xxlJob == null) {

continue;

}

String name = xxlJob.value();

if (name.trim().length() == 0) {

throw new RuntimeException(“xxl-job method-jobhandler name invalid, for[” + bean.getClass() + “#” + method.getName() + “] .”);

}

if (loadJobHandler(name) != null) {

throw new RuntimeException(“xxl-job jobhandler[” + name + “] naming conflicts.”);

}

// execute method

if (!(method.getParameterTypes().length == 1 && method.getParameterTypes()[0].isAssignableFrom(String.class))) {

throw new RuntimeException(“xxl-job method-jobhandler param-classtype invalid, for[” + bean.getClass() + “#” + method.getName() + "] , " +

“The correct method format like " public ReturnT<String> execute(String param) " .”);

}

if (!method.getReturnType().isAssignableFrom(ReturnT.class)) {

throw new RuntimeException(“xxl-job method-jobhandler return-classtype invalid, for[” + bean.getClass() + “#” + method.getName() + "] , " +

“The correct method format like " public ReturnT<String> execute(String param) " .”);

}

method.setAccessible(true);

// init and destory

Method initMethod = null;

Method destroyMethod = null;

if (xxlJob.init().trim().length() > 0) {

try {

initMethod = bean.getClass().getDeclaredMethod(xxlJob.init());

initMethod.setAccessible(tru

好的,下面是xxl-job的配置文件详解: 1. application.properties application.properties是xxl-job的主配置文件,主要包含一些全局配置,例如: - xxl.job.executor.appname:执行器名称,可自定义,作为在调度中心展示的名称,例如:xxl-job-executor。 - xxl.job.executor.ip:执行器所在机器的IP地址或者域名,例如:192.168.1.2。 - xxl.job.executor.port:执行器服务监听的端口号,例如:9999。 - xxl.job.admin.addresses:调度中心地址列表,例如:http://192.168.1.3:8080/xxl-job-admin。 2. xxl-job-admin.properties xxl-job-admin.properties是调度中心的配置文件,主要包含一些调度中心的全局配置,例如: - xxl.job.admin.login.username:调度中心登录用户名,例如:admin。 - xxl.job.admin.login.password:调度中心登录密码,例如:123456。 - xxl.job.executor.fail.retrycount:调度中心任务失败重试次数,默认为0。 3. xxl-job-executor.properties xxl-job-executor.properties是执行器的配置文件,主要包含一些执行器的全局配置,例如: - xxl.job.executor.logpath:执行器日志文件路径,例如:/data/applogs/xxl-job/jobhandler。 - xxl.job.executor.logretentiondays:执行器日志保留天数,例如:30。 - xxl.job.executor.fail.retrialinterval:执行器任务失败重试间隔时间,例如:10000。 以上是xxl-job的主要配置文件及其详解,如有需要可自行查看官方文档进行更深入的了解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值