Skip to content

Commit 0b69411

Browse files
committed
Add Spring Batch version in the execution context
Resolves #4215
1 parent 866fe37 commit 0b69411

File tree

6 files changed

+159
-4
lines changed

6 files changed

+159
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core;
17+
18+
import org.springframework.lang.Nullable;
19+
20+
/**
21+
* Class that exposes the Spring Batch version. Fetches the "Implementation-Version"
22+
* manifest attribute from the jar file.
23+
*
24+
* <p>
25+
* Note that some ClassLoaders do not expose the package metadata, hence this class might
26+
* not be able to determine the Spring Batch version in all environments.
27+
*
28+
* @author Mahmoud Ben Hassine
29+
* @since 5.0
30+
*/
31+
public final class SpringBatchVersion {
32+
33+
/**
34+
* The key to use in the execution context for batch version.
35+
*/
36+
public static final String BATCH_VERSION_KEY = "batch.version";
37+
38+
private SpringBatchVersion() {
39+
}
40+
41+
/**
42+
* Return the full version string of the present Spring Batch codebase, or
43+
* {@code "N/A"} if it cannot be determined.
44+
* @see Package#getImplementationVersion()
45+
*/
46+
@Nullable
47+
public static String getVersion() {
48+
Package pkg = SpringBatchVersion.class.getPackage();
49+
if (pkg != null && pkg.getImplementationVersion() != null) {
50+
return pkg.getImplementationVersion();
51+
}
52+
return "N/A";
53+
}
54+
55+
}

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.batch.core.JobInterruptedException;
3737
import org.springframework.batch.core.JobParametersIncrementer;
3838
import org.springframework.batch.core.JobParametersValidator;
39+
import org.springframework.batch.core.SpringBatchVersion;
3940
import org.springframework.batch.core.StartLimitExceededException;
4041
import org.springframework.batch.core.Step;
4142
import org.springframework.batch.core.StepExecution;
@@ -276,6 +277,7 @@ protected JobRepository getJobRepository() {
276277
public final void execute(JobExecution execution) {
277278

278279
Assert.notNull(execution, "jobExecution must not be null");
280+
execution.getExecutionContext().put(SpringBatchVersion.BATCH_VERSION_KEY, SpringBatchVersion.getVersion());
279281

280282
if (logger.isDebugEnabled()) {
281283
logger.debug("Job execution starting: " + execution);

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.batch.core.BatchStatus;
2828
import org.springframework.batch.core.ExitStatus;
2929
import org.springframework.batch.core.JobInterruptedException;
30+
import org.springframework.batch.core.SpringBatchVersion;
3031
import org.springframework.batch.core.Step;
3132
import org.springframework.batch.core.StepExecution;
3233
import org.springframework.batch.core.StepExecutionListener;
@@ -193,6 +194,7 @@ public final void execute(StepExecution stepExecution)
193194
throws JobInterruptedException, UnexpectedJobExecutionException {
194195

195196
Assert.notNull(stepExecution, "stepExecution must not be null");
197+
stepExecution.getExecutionContext().put(SpringBatchVersion.BATCH_VERSION_KEY, SpringBatchVersion.getVersion());
196198

197199
if (logger.isDebugEnabled()) {
198200
logger.debug("Executing: id=" + stepExecution.getId());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core;
17+
18+
import javax.sql.DataSource;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
23+
import org.springframework.batch.core.job.builder.JobBuilder;
24+
import org.springframework.batch.core.launch.JobLauncher;
25+
import org.springframework.batch.core.repository.JobRepository;
26+
import org.springframework.batch.core.step.builder.StepBuilder;
27+
import org.springframework.batch.repeat.RepeatStatus;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.context.annotation.Bean;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
32+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
33+
import org.springframework.jdbc.support.JdbcTransactionManager;
34+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
35+
import org.springframework.transaction.PlatformTransactionManager;
36+
37+
import static org.junit.jupiter.api.Assertions.assertEquals;
38+
import static org.junit.jupiter.api.Assertions.assertNotNull;
39+
import static org.junit.jupiter.api.Assertions.assertTrue;
40+
41+
/**
42+
* Test class for {@link SpringBatchVersion}.
43+
*
44+
* @author Mahmoud Ben Hassine
45+
*/
46+
@SpringJUnitConfig
47+
public class SpringBatchVersionTests {
48+
49+
@Autowired
50+
private JobLauncher jobLauncher;
51+
52+
@Autowired
53+
private Job job;
54+
55+
@Test
56+
void testBatchVersionInExecutionContext() throws Exception {
57+
// given
58+
JobParameters jobParameters = new JobParametersBuilder().toJobParameters();
59+
60+
// when
61+
JobExecution jobExecution = this.jobLauncher.run(this.job, jobParameters);
62+
63+
// then
64+
assertNotNull(jobExecution);
65+
assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
66+
assertTrue(jobExecution.getExecutionContext().containsKey(SpringBatchVersion.BATCH_VERSION_KEY));
67+
assertTrue(jobExecution.getStepExecutions().iterator().next().getExecutionContext()
68+
.containsKey(SpringBatchVersion.BATCH_VERSION_KEY));
69+
}
70+
71+
@Configuration
72+
@EnableBatchProcessing
73+
static class TestConfiguration {
74+
75+
@Bean
76+
public DataSource dataSource() {
77+
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
78+
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").generateUniqueName(true).build();
79+
}
80+
81+
@Bean
82+
public JdbcTransactionManager transactionManager(DataSource dataSource) {
83+
return new JdbcTransactionManager(dataSource);
84+
}
85+
86+
@Bean
87+
public Job job(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
88+
return new JobBuilder("job", jobRepository)
89+
.start(new StepBuilder("step", jobRepository)
90+
.tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED, transactionManager).build())
91+
.build();
92+
}
93+
94+
}
95+
96+
}

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ void testRunNormally() {
214214
assertNotNull(jobExecution.getEndTime());
215215
assertNotNull(jobExecution.getStartTime());
216216

217-
assertTrue(step1.passedInJobContext.isEmpty());
217+
assertEquals(1, step1.passedInJobContext.size());
218218
assertFalse(step2.passedInJobContext.isEmpty());
219219

220220
// Observability
@@ -442,7 +442,7 @@ void testRestartAndExecutionContextRestored() throws Exception {
442442
Throwable e = jobExecution.getAllFailureExceptions().get(0);
443443
assertSame(exception, e);
444444

445-
assertTrue(step1.passedInJobContext.isEmpty());
445+
assertEquals(1, step1.passedInJobContext.size());
446446
assertFalse(step2.passedInJobContext.isEmpty());
447447

448448
assertFalse(jobExecution.getExecutionContext().isEmpty());

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/step/item/TaskletStepExceptionTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext attribut
251251
assertEquals(1, stepExecution.getRollbackCount()); // Failed transaction
252252
// counts as
253253
// rollback
254-
assertEquals(2, stepExecution.getExecutionContext().size());
254+
assertEquals(3, stepExecution.getExecutionContext().size());
255255
assertTrue(stepExecution.getExecutionContext().containsKey(Step.STEP_TYPE_KEY));
256256
assertTrue(stepExecution.getExecutionContext().containsKey(TaskletStep.TASKLET_TYPE_KEY));
257257
}
@@ -286,7 +286,7 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext attribut
286286
assertEquals(1, stepExecution.getRollbackCount()); // Failed transaction
287287
// counts as
288288
// rollback
289-
assertEquals(2, stepExecution.getExecutionContext().size());
289+
assertEquals(3, stepExecution.getExecutionContext().size());
290290
assertTrue(stepExecution.getExecutionContext().containsKey(Step.STEP_TYPE_KEY));
291291
assertTrue(stepExecution.getExecutionContext().containsKey(TaskletStep.TASKLET_TYPE_KEY));
292292
}

0 commit comments

Comments
 (0)