Skip to content

Commit b1e4a94

Browse files
Patrick Schwizerfmbenhassine
authored andcommitted
Refactor JobKeyGenerator to use JobParameters instead of generic type
Signed-off-by: Patrick Schwizer <schwizer.p@proton.me>
1 parent e6a8088 commit b1e4a94

File tree

9 files changed

+27
-24
lines changed

9 files changed

+27
-24
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/job/DefaultJobKeyGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@
3636
* @author Mahmoud Ben Hassine
3737
* @since 2.2
3838
*/
39-
public class DefaultJobKeyGenerator implements JobKeyGenerator<JobParameters> {
39+
public class DefaultJobKeyGenerator implements JobKeyGenerator {
4040

4141
/**
4242
* Generates the job key to be used based on the {@link JobParameters} instance

spring-batch-core/src/main/java/org/springframework/batch/core/job/JobKeyGenerator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2024 the original author or authors.
2+
* Copyright 2013-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,25 +15,26 @@
1515
*/
1616
package org.springframework.batch.core.job;
1717

18+
import org.springframework.batch.core.job.parameters.JobParameters;
19+
1820
/**
1921
* Strategy interface for the generation of the key used in identifying unique
2022
* {@link JobInstance} objects.
2123
*
2224
* @author Michael Minella
2325
* @author Mahmoud Ben Hassine
2426
* @author Taeik Lim
25-
* @param <T> The type of the source data used to calculate the key.
2627
* @since 2.2
2728
*/
2829
@FunctionalInterface
29-
public interface JobKeyGenerator<T> {
30+
public interface JobKeyGenerator {
3031

3132
/**
3233
* Method to generate the unique key used to identify a job instance.
3334
* @param source Source information used to generate the key (must not be
3435
* {@code null}).
3536
* @return a unique string identifying the job based on the information supplied.
3637
*/
37-
String generateKey(T source);
38+
String generateKey(JobParameters source);
3839

3940
}

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/jdbc/JdbcJobInstanceDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ SELECT COUNT(*)
127127

128128
private DataFieldMaxValueIncrementer jobInstanceIncrementer;
129129

130-
private JobKeyGenerator<JobParameters> jobKeyGenerator = new DefaultJobKeyGenerator();
130+
private JobKeyGenerator jobKeyGenerator = new DefaultJobKeyGenerator();
131131

132132
/**
133133
* In this JDBC implementation a job instance id is obtained by asking the

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/mongodb/MongoJobInstanceDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class MongoJobInstanceDao implements JobInstanceDao {
4848

4949
private DataFieldMaxValueIncrementer jobInstanceIncrementer;
5050

51-
private JobKeyGenerator<JobParameters> jobKeyGenerator = new DefaultJobKeyGenerator();
51+
private JobKeyGenerator jobKeyGenerator = new DefaultJobKeyGenerator();
5252

5353
private final JobInstanceConverter jobInstanceConverter = new JobInstanceConverter();
5454

@@ -58,7 +58,7 @@ public MongoJobInstanceDao(MongoOperations mongoOperations) {
5858
this.jobInstanceIncrementer = new MongoSequenceIncrementer(mongoOperations, SEQUENCE_NAME);
5959
}
6060

61-
public void setJobKeyGenerator(JobKeyGenerator<JobParameters> jobKeyGenerator) {
61+
public void setJobKeyGenerator(JobKeyGenerator jobKeyGenerator) {
6262
this.jobKeyGenerator = jobKeyGenerator;
6363
}
6464

spring-batch-core/src/test/java/org/springframework/batch/core/DefaultJobKeyGeneratorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
class DefaultJobKeyGeneratorTests {
2929

30-
private final JobKeyGenerator<JobParameters> jobKeyGenerator = new DefaultJobKeyGenerator();
30+
private final JobKeyGenerator jobKeyGenerator = new DefaultJobKeyGenerator();
3131

3232
@Test
3333
void testNullParameters() {

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/BatchRegistrarTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
3131
import org.springframework.batch.core.converter.JobParametersConverter;
3232
import org.springframework.batch.core.converter.JsonJobParametersConverter;
33+
import org.springframework.batch.core.job.parameters.JobParameters;
3334
import org.springframework.batch.core.launch.JobLauncher;
3435
import org.springframework.batch.core.launch.JobOperator;
3536
import org.springframework.batch.core.repository.JobRepository;
@@ -291,14 +292,14 @@ public JdbcTransactionManager transactionManager(DataSource dataSource) {
291292
}
292293

293294
@Bean
294-
public JobKeyGenerator<Object> jobKeyGenerator() {
295+
public JobKeyGenerator jobKeyGenerator() {
295296
return new TestCustomJobKeyGenerator();
296297
}
297298

298-
private static class TestCustomJobKeyGenerator implements JobKeyGenerator<Object> {
299+
private static class TestCustomJobKeyGenerator implements JobKeyGenerator {
299300

300301
@Override
301-
public String generateKey(Object source) {
302+
public String generateKey(JobParameters source) {
302303
return "1";
303304
}
304305

spring-batch-core/src/test/java/org/springframework/batch/core/explore/support/JobExplorerFactoryBeanTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717

1818
import javax.sql.DataSource;
1919

20+
import org.jetbrains.annotations.NotNull;
2021
import org.junit.jupiter.api.Assertions;
2122
import org.junit.jupiter.api.BeforeEach;
2223
import org.junit.jupiter.api.Test;
@@ -26,6 +27,7 @@
2627
import org.springframework.aop.framework.Advised;
2728
import org.springframework.batch.core.job.DefaultJobKeyGenerator;
2829
import org.springframework.batch.core.job.JobKeyGenerator;
30+
import org.springframework.batch.core.job.parameters.JobParameters;
2931
import org.springframework.batch.core.repository.explore.JobExplorer;
3032
import org.springframework.batch.core.repository.explore.support.JobExplorerFactoryBean;
3133
import org.springframework.jdbc.core.JdbcOperations;
@@ -148,10 +150,10 @@ public void testCustomJobKeyGenerator() throws Exception {
148150
Assertions.assertEquals(CustomJobKeyGenerator.class, jobKeyGenerator.getClass());
149151
}
150152

151-
static class CustomJobKeyGenerator implements JobKeyGenerator<String> {
153+
static class CustomJobKeyGenerator implements JobKeyGenerator {
152154

153155
@Override
154-
public String generateKey(String source) {
156+
public @NotNull String generateKey(@NotNull JobParameters source) {
155157
return "1";
156158
}
157159

spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/jdbc/CustomJobKeyGenerator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717

1818
import org.jetbrains.annotations.NotNull;
1919
import org.springframework.batch.core.job.JobKeyGenerator;
20+
import org.springframework.batch.core.job.parameters.JobParameters;
2021

21-
public class CustomJobKeyGenerator implements JobKeyGenerator<String> {
22+
public class CustomJobKeyGenerator implements JobKeyGenerator {
2223

2324
@Override
24-
public @NotNull String generateKey(@NotNull String source) {
25+
public @NotNull String generateKey(@NotNull JobParameters source) {
2526
return "1";
2627
}
2728

spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBeanTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,24 +334,22 @@ void testCustomLobType() throws Exception {
334334
@Test
335335
public void testDefaultJobKeyGenerator() throws Exception {
336336
testCreateRepository();
337-
@SuppressWarnings("rawtypes")
338-
JobKeyGenerator<?> jobKeyGenerator = (JobKeyGenerator) ReflectionTestUtils.getField(factory, "jobKeyGenerator");
337+
JobKeyGenerator jobKeyGenerator = (JobKeyGenerator) ReflectionTestUtils.getField(factory, "jobKeyGenerator");
339338
assertEquals(DefaultJobKeyGenerator.class, jobKeyGenerator.getClass());
340339
}
341340

342341
@Test
343342
public void testCustomJobKeyGenerator() throws Exception {
344343
factory.setJobKeyGenerator(new CustomJobKeyGenerator());
345344
testCreateRepository();
346-
@SuppressWarnings("rawtypes")
347-
JobKeyGenerator<?> jobKeyGenerator = (JobKeyGenerator) ReflectionTestUtils.getField(factory, "jobKeyGenerator");
345+
JobKeyGenerator jobKeyGenerator = (JobKeyGenerator) ReflectionTestUtils.getField(factory, "jobKeyGenerator");
348346
assertEquals(CustomJobKeyGenerator.class, jobKeyGenerator.getClass());
349347
}
350348

351-
static class CustomJobKeyGenerator implements JobKeyGenerator<String> {
349+
static class CustomJobKeyGenerator implements JobKeyGenerator {
352350

353351
@Override
354-
public String generateKey(String source) {
352+
public String generateKey(JobParameters source) {
355353
return "1";
356354
}
357355

0 commit comments

Comments
 (0)