Spring Batch
I have already written some posts on Spring Batch. I am going to show you here what are the changes required while you are migrating your existing spring batch application to spring batch version 5.x or while you are using spring batch in spring boot 3.x.
Requirements For Spring Batch 5
- Spring batch 5 framework is based on spring framework version 6 which requires Java 17 as a minimum version.
- In spring batch 5, you must create meta data tables and without meta data tables you cannot run spring batch applications. Meta data tables where spring batch application stores job related information. You may find which tables are created for meta data storage in my spring batch posts.
- The
JobBuilderFactory
has been replaced byJobBuilder
andStepBuilderFactory
has been replaced byStepBuilder
. The JobRepository instance has to be passed while creating instance forStep
withStepBuilder
orJob
withJobBuilder
. org.springframework:spring-jdbc
is a required dependency inspring-batch-core
,junit:junit
andcom.fasterxml.jackson.core:jackson-core
are optional dependencies inspring-batch-core
.- DDL script
org/springframework/batch/core/schema-drop-oracle10g.sql
for Oracle database has been renamed toorg/springframework/batch/core/schema-drop-oracle.sql
andorg/springframework/batch/core/schema-oracle10g.sql
has been renamed toorg/springframework/batch/core/schema-oracle.sql
. - Columns have been removed and added to the meta data table BATCH_JOB_EXECUTION_PARAMS.
@EnableBatchProcessing
annotation can be excluded now from the Java configuration class. This annotation exposed a transaction manager bean until spring batch 4.3 version, but it does not expose a transaction manager bean for spring batch 5 and it requires now a manual transaction manager bean to be configured.- The transaction manager is now needed on any tasklet step definition.
- Import statements have to be updated from
javax.*
tojakarta.*
for all EE APIs you use. For example, you need to replacejavax.xml*
byjakarta.xml*
.
Implementation Related Changes
Here are some changes which are required for spring batch 5 applications:
JobBuilder
Previously you used to get the Job
instance from the JobBuilderFactory
in the following way:
Job job = jobBuilderFactory.get("jobName").incrementer(new RunIdIncrementer()).flow(step).end().build();
Now you need to get the Job
instance from the JobBuilder
in the following way:
Job job = new JobBuilder("jobName", jobRepository).flow(step).end().build();
In the JobBuilder
you need to pass the JobRepository
instance for creating a Job
instance.
StepBuilder
The Step
was created from the StepBuilderFactory
in the following way:
Step step = stepBuilderFactory.get("stepName").transactionManager(transactionManager).<Object, Object>chunk(2).reader(reader).processor(processor).writer(writer).build();
Now the Step
is created from the StepBuilder
in the following way:
Step step = new StepBuilder("stepName", jobRepository).<Object, Object>chunk(2, transactionManager).reader(reader)
.processor(processor).writer(writer).build();
The JobRepository
instance is passed to the StepBuilder
. Also notice the required transaction manager instance is passed to the chunk()
method.
JobParameters
The Map was used to configure JobParameter, for example, the following could have been used to pass in the job:
Map<String, JobParameter> map = new HashMap<>();
map.put("time", new JobParameter(System.currentTimeMillis()));
map.put("fileName", "TextFile5434535.txt");
JobParameters params = new JobParameters(map);
Now you need to use JobParametersBuilder
in place of JobParameters
in the following way:
JobParametersBuilder params = new JobParametersBuilder().addLong("time", System.currentTimeMillis()).addString("fileName", "TextFile5434535.txt").toJobParameters();
Jakarta Imports
If you have used any XML related import statements from javax.*
then replace it by jakarta.*
import statements.
You need to update the version of the related dependencies also. For example, javax.*
, com.sun.*
of 2.x version should be replaced by jakarta.*
, com.sun.*
of 4.x version.
javax.xml.bind:jaxb-api:2.2.12
com.sun.xml.bind:jaxb-core:2.2.11
com.sun.xml.bind:jaxb-impl:2.2.11
To
jakarta.xml.bind:jakarta.xml.bind-api:4.0.0
com.sun.xml.bind:jaxb-impl:4.0.3
Jacoco
If you are using junit tests and jacoco plugin for test reports then make sure you use the jacoco version at least 0.8.8.
The xml.enabled = true
should be updated by xml.required = true
inside the following tag:
jacocoTestReport {
reports {
xml.required = true //xml.enabled = true
...
}
}
Servlet
Make sure you upgrade Servlet API version to at least 4.0.0 otherwise your servlet import statements will not work.
Swagger
If you are using swagger version 2, then it won’t work, so you need to upgrade to Open API version 3 to work with Spring Boot 3.x version.
Gradle
Though you may find gradle documentation that gradle version 7.3 is fully compatible with Java 17 but in spring boot 3.x and spring batch 5, I could not make it work with gradle version 7.3 and I had to use gradle version at least 7.4.
That’s all about basic migration steps and if you have anything to add kindly let me know in the comment section.