SpringBatch4升级SpringBatch5踩坑指南

SpringBatch5使用JDK17作为基线版本,若项目未升级,亦无需升级springbatch。

报错1:JobParameter类型问题

报错内容:
java.util.Map.Entry<java.lang.String,org.springframework.batch.core.JobParameter<?>>无法转换为java.util.Map.Entry<java.lang.String,org.springframework.batch.core.JobParameter>

参考:

原因:SpringBatch4只有4种预定义的类型(long、double、string、date),SpringBatch5版本增加了对使用任何类型作为 job 参数的支持

解决方案:将使用到JobParameter的地方适配为JobParameter<?>

报错2:JobBuilderFactory已弃用

报错内容:
JobBuilderFactory和StepBuilderFactory已弃用

参考:https://github.com/spring-projects/spring-batch/wiki/Spring-Batch-5.0-Migration-Guide#infrastructure-beans-configuration-with-enablebatchprocessing

https://springdoc.cn/spring-batch/job.html#configuringAJob

https://docs.spring.io/spring-batch/reference/job/configuring.html

原因:JobBuilderFactory和StepBuilderFactory在SpringBatch5中已弃用

解决方案:使用JobBuilder和StepBuilder创建

JobConfigurer上添加注解@EnableBatchProcessing,自动注入以下bean或重写以下bean

@Autowired

private JobRepository jobRepository;

@Autowired

private PlatformTransactionManager transactionManager;

或者可将JobConfigurer继承自DefaultBatchJobConfigur

报错3:@AfterWrite参数变更

报错内容:
Error creating bean with name 'JobControllerImpl' Caused by: java.lang.IllegalArgumentException: The method [afterWrite] on target class [XxxListener] is incompatible with the signature [(Chunk)] expected for the annotation [AfterWrite].

位置:Listener

参考:https://springdoc.cn/spring-batch/readersAndWriters.html#itemWriter

解决方案:将@AfterWrite注解的方法参数由List改为Chunk

报错4:SimpleJobLauncher不再使用

报错内容:
SimpleJobLauncher不再使用

位置:SpringBatchConfigurer

参考:https://springdoc.cn/spring-batch/job.html#configuringJobLauncher

https://docs.spring.io/spring-batch/reference/job/configuring-launcher.html

解决方案:使用TaskExecutorJobLauncher jobLauncher = new TaskExecutorJobLauncher();替代

报错5:springbatch表前缀不生效

报错内容:
springbatch表前缀不生效

(application-dev.yaml中spring.batch.jdbc.table-prefix:xxx_)

本项目遇到的报错为使用MySQL作为job数据管理所遇见,其他数据库尚未测试

参考:https://springdoc.cn/spring-batch/job.html#repositoryTablePrefix

临时解决方案:在 SpringBatchConfigurer的注解上添加@EnableBatchProcessing(tablePrefix = "xxx_")

结论:本地生效,部署到容器中不生效,未解决

报错6:java.lang.ClassNotFoundException: DATE

java.lang.RuntimeException: java.lang.ClassNotFoundException: DATE at org.springframework.batch.core.repository.dao.JdbcJobExecutionDao.lambda$getJobParameters$2(JdbcJobExecutionDao.java:472) at org.springframework.jdbc.core.JdbcTemplate$RowCallbackHandlerResultSetExtractor.extractData(JdbcTemplate.java:1762) at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:733) ... xxx ...
Caused by: java.lang.ClassNotFoundException: DATE at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:375) at org.springframework.batch.core.repository.dao.JdbcJobExecutionDao.lambda$getJobParameters$2(JdbcJobExecutionDao.java:469) ... 91 common frames omitted

此处遇到的可能是java.lang.RuntimeException: java.lang.ClassNotFoundException: STRING,原理相同

相关原因:https://github.com/spring-projects/spring-batch/wiki/Spring-Batch-5.0-Migration-Guide#data-types-updates

根本原因:https://github.com/spring-projects/spring-batch/issues/4352

相关说明:https://github.com/spring-projects/spring-batch/wiki/Spring-Batch-5.0-Migration-Guide#historical-data-access-implications.

新启动的job会查询最后一个实例来进行参数变更,如id自增等。查询到最后一个实例后,进行映射。老数据的parameter类型为DATE,新的为java.util.Date,故无法找到类型DATE

在以上issue中,给出声明,老的数据和新数据不应该再有交互。

解决方案:

planA.将表中的DATE修改为java.util.Date

planB.使用springBatch5的建表语句重建表

报错7:其他报错参考

相关推荐
行百里er15 小时前
Gateway 上别硬塞 MVC:DeferredImportSelector 看菜下锅
spring boot·后端·开源
黑马程序员毕设16 小时前
基于Java的医院药品管理系统的优化设计与实现
java·开发语言·spring boot·小程序·架构·课程设计·毕设
xcl092517 小时前
酒馆预约系统开发实战:从需求分析到上线全流程指南
java·spring boot·需求分析
wxwx_bscxy32217 小时前
基于springboot宠物领养系统的设计与实现
数据库·spring boot·后端·spring·宠物
苏生Susheng17 小时前
【软件实施】Linux系统Shell脚本教程
linux·运维·服务器·chrome·spring boot·学习·实施
计算机毕设定制辅导-无忧学长19 小时前
《基于SpringBoot的马术俱乐部管理系统》
java·spring boot·后端
摇滚侠19 小时前
《SpringBoot 3:入门与应用实战》第 12 章 JDBC 与事务 JDBC 事务管理 阅读笔记 33
spring boot·笔记·后端
学长毕业设计20 小时前
基于SpringBoot的民间艺术传承管理系统(源码+文档+讲解视频)
java·spring boot·后端
小蒜学长20 小时前
基于Springboot+Vue的环保行动志愿者招募系统设计与实现(代码+数据库+LW)
java·数据库·vue.js·spring boot·后端
cfm_291420 小时前
Spring事务
数据库·spring·oracle