现在要对一批老项目中的执行器进行升级,原来老项目中的执行器,依赖的任务调度中心xxl-job版本是2.0.2-SNAPSHOT版本,现在要依赖2.3.0版本的xxl-job任务调度中心
老项目的执行器,使用的是spring版本,需要tomcat来启动服务,这里拿一个纯净的spring项目来演示,具体如下:
<parent>
<groupId>com.xuxueligroupId>
<artifactId>xxl-job-executor-samplesartifactId>
<version>2.0.2-SNAPSHOTversion>
parent>
修改执行器依赖xxl-job-core的版本,改为2.3.0版本
如下:
<parent>
<groupId>com.xuxueligroupId>
<artifactId>xxl-job-executor-samplesartifactId>
<version>2.3.0version>
parent>
升级到2.3.0版本之后,我们发现Handler类报错了
原因就是因为,新版本的xxl-job,已经弃用了@JobHandler注解,采用了@XxlJob注解,并且@XxlJob注解只能作用在方法上面哦。
改造步骤:
代码参考:
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;
}
}
版本也升级了,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
刚刚启动服务的时候,报错了,比较核心的错误日志是这一句:
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.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.22.RELEASEversion>
dependency>
我们发现虽然报错了,但至少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就可以了