xxl-job任务调度2.0.2升级到2.3.0版本,执行器改造过程中经验总结

简介: xxl-job任务调度2.0.2升级到2.3.0版本,执行器改造过程中经验总结

一、背景

现在要对一批老项目中的执行器进行升级,原来老项目中的执行器,依赖的任务调度中心xxl-job版本是2.0.2-SNAPSHOT版本,现在要依赖2.3.0版本的xxl-job任务调度中心

二、开始改造

老项目的执行器,使用的是spring版本,需要tomcat来启动服务,这里拿一个纯净的spring项目来演示,具体如下:

1、修改pom.xml

打开pom.xml文件,原文件是这样的

如下:

<parent>
    <groupId>com.xuxueli</groupId>
    <artifactId>xxl-job-executor-samples</artifactId>
    <version>2.0.2-SNAPSHOT</version>
  </parent>

修改执行器依赖xxl-job-core的版本,改为2.3.0版本

如下:

<parent>
    <groupId>com.xuxueli</groupId>
    <artifactId>xxl-job-executor-samples</artifactId>
    <version>2.3.0</version>
  </parent>

2、修改Handler

升级到2.3.0版本之后,我们发现Handler类报错了

原因就是因为,新版本的xxl-job,已经弃用了@JobHandler注解,采用了@XxlJob注解,并且@XxlJob注解只能作用在方法上面哦。

改造步骤:

  • 1、去掉@JobHandler注解
  • 2、去掉 extends IJobHandler
  • 3、方法上加上@XxlJob注解
  • 4、返回值 SUCCESS 改为 ReturnT.SUCCESS

代码参考:

package com.xxl.job.executor.service.jobhandler;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
public class DemoJobHandler {
  @XxlJob("demoJobHandler")
  public ReturnT<String> execute(String param) throws Exception {
    XxlJobLogger.log("XXL-JOB, Hello World.");
    for (int i = 0; i < 5; i++) {
      XxlJobLogger.log("beat at:" + i);
      TimeUnit.SECONDS.sleep(2);
    }
    return ReturnT.SUCCESS;
  }
}

3、启动服务并验证

版本也升级了,handler类也改造好了,貌似没什么问题了,那我们直接启动tomcat服务,来验证一下。

启动之后,我们看一下控制台日志

Connected to server
[2023-10-20 10:34:31,713] Artifact xxl-job-executor-sample-spring:war exploded: Artifact is being deployed, please wait...
20-Oct-2023 10:34:38.787 信息 [RMI TCP Connection(5)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
20-Oct-2023 10:34:38.885 严重 [RMI TCP Connection(5)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal One or more listeners failed to start. Full details will be found in the appropriate container log file
20-Oct-2023 10:34:38.887 严重 [RMI TCP Connection(5)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal Context [] startup failed due to previous errors
[2023-10-20 10:34:38,903] Artifact xxl-job-executor-sample-spring:war exploded: Error during artifact deployment. See server log for details.

如果这个看不出来,可以看一下另外的输出日志

org.apache.catalina.core.ApplicationContext.log No Spring WebApplicationInitializer types detected on classpath
20-Oct-2023 10:34:38.834 严重 [RMI TCP Connection(5)-127.0.0.1] org.apache.catalina.core.StandardContext.listenerStart Error configuring application listener of class org.springframework.web.util.Log4jConfigListener
 java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener

4、解决异常

刚刚启动服务的时候,报错了,比较核心的错误日志是这一句:

java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener

就是说tomcat启动的时候,没有找到spring中的Log4jConfigListener类,OK,我们先看一下spring的版本,升级到2.3.0之后,spring-webmvc的版本也跟着改变了,变成了5.3.3版本

经过查询,发现org.springframework.web.util.Log4jConfigListener这个类在spring5.0及以上版本已经废弃删除,如果想使用这个类,spring版本需要降低。原来如此,那我们直接将spring的版本降为升级前的版本

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.22.RELEASE</version>
    </dependency>

OK,接下来启动服务,看一下

我们发现虽然报错了,但至少spring容器在加载初始化一些配置了,我们分析一下错误:

Invalid property 'appName' of bean class [com.xxl.job.core.executor.impl.XxlJobSpringExecutor]: Bean property 'appName' is not writable or has an invalid setter method. Did you mean 'appname'?

应该是属性名称不一样,我们找一下源码,打开一下XxlJobSpringExecutor类

public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationContextAware, SmartInitializingSingleton, DisposableBean {
    private static final Logger logger = LoggerFactory.getLogger(XxlJobSpringExecutor.class);
    private static ApplicationContext applicationContext;
    public XxlJobSpringExecutor() {
    }
    public void afterSingletonsInstantiated() {
        this.initJobHandlerMethodRepository(applicationContext);
        GlueFactory.refreshInstance(1);
        try {
            super.start();
        } catch (Exception var2) {
            throw new RuntimeException(var2);
        }
    }
    .....

发现没有,继续找他的父类XxlJobExecutor,终于找到了,我们看到appname这个字段是这样命名的

所以,我们回到项目中,看一下项目中的配置文件

原来这个地方写成了appName,所以只要把他改成appname就可以了

5、再次启动服务并验证

我们发现终于启动成功了,再看一下,执行器是否注册到任务调度中心了

我们随便建一个任务执行一下看看

执行成功

至此,执行器的版本升级结束


相关文章
|
11月前
|
监控 安全 调度
彻底解决5大开源痛点,阿里云发布任务调度 XXL-JOB 版
阿里云任务调度XXL-JOB版 迎来重磅发布,以任务调度SchedulerX为内核,0代码改造,完全兼容开源XXL-JOB客户端接入,解决开源XXL-JOB痛点问题。
1435 127
|
前端开发 Java 调度
XXL-JOB 日志表和日志文件自动清理
XXL-JOB 日志表和日志文件自动清理
|
关系型数据库 MySQL 数据库
项目实战24—xxljob控制台不打印日志
项目实战24—xxljob控制台不打印日志
787 0
xxl-job执行器启动报错读取不到配置文件Could not resolve placeholder ‘xxl.job.executor.address‘ in value “${xxl.job
有几个不用配置的属性,也要写出来,不填值就行 但是最后一个日志天数得写,写个-1。不然空字符串无法转成数字
|
消息中间件 数据可视化 NoSQL
XXL-Job搭建(传统方式&Docker方式)与使用(Linux环境下)
XXL-Job搭建(传统方式&Docker方式)与使用(Linux环境下)
6588 0
XXL-Job搭建(传统方式&Docker方式)与使用(Linux环境下)
|
10月前
|
Java 中间件 调度
SpringBoot整合XXL-JOB【03】- 执行器的使用
本文介绍了如何将调度中心与项目结合,通过配置“执行器”实现定时任务控制。首先新建SpringBoot项目并引入依赖,接着配置xxl-job相关参数,如调度中心地址、执行器名称等。然后通过Java代码将执行器注册为Spring Bean,并声明测试方法使用`@XxlJob`注解。最后,在调度中心配置并启动定时任务,验证任务是否按预期执行。通过这些步骤,读者可以掌握Xxl-Job的基本使用,专注于业务逻辑的编写而无需关心定时器本身的实现。
2007 10
SpringBoot整合XXL-JOB【03】-  执行器的使用
|
前端开发 Java 调度
SpringCloud微服务实战——搭建企业级开发框架(四十二):集成分布式任务调度平台XXL-JOB,实现定时任务功能
定时任务几乎是每个业务系统必不可少的功能,计算到期时间、过期时间等,定时触发某项任务操作。在使用单体应用时,基本使用Spring提供的注解即可实现定时任务,而在使用微服务集群时,这种方式就要考虑添加分布式锁来防止多个微服务同时运行定时任务而导致同一个任务重复执行。
1717 55
SpringCloud微服务实战——搭建企业级开发框架(四十二):集成分布式任务调度平台XXL-JOB,实现定时任务功能
|
SQL Shell 数据库连接
死磕xxl-job(二)
死磕xxl-job(二)
|
消息中间件 JSON Java
Spring Boot、Spring Cloud与Spring Cloud Alibaba版本对应关系
Spring Boot、Spring Cloud与Spring Cloud Alibaba版本对应关系
26410 0
|
Docker 容器
SrpingBoot 集成 xxl-job 部署在 Docker 上碰到的坑
SrpingBoot 集成 xxl-job 部署在 Docker 上碰到的坑
405 0