Skip to content

Refactor JobKeyGenerator to use JobParameters instead of generic type #4887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @author Mahmoud Ben Hassine
* @since 2.2
*/
public class DefaultJobKeyGenerator implements JobKeyGenerator<JobParameters> {
public class DefaultJobKeyGenerator implements JobKeyGenerator {

/**
* Generates the job key to be used based on the {@link JobParameters} instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,26 @@
*/
package org.springframework.batch.core.job;

import org.springframework.batch.core.job.parameters.JobParameters;

/**
* Strategy interface for the generation of the key used in identifying unique
* {@link JobInstance} objects.
*
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Taeik Lim
* @param <T> The type of the source data used to calculate the key.
* @since 2.2
*/
@FunctionalInterface
public interface JobKeyGenerator<T> {
public interface JobKeyGenerator {

/**
* Method to generate the unique key used to identify a job instance.
* @param source Source information used to generate the key (must not be
* {@code null}).
* @return a unique string identifying the job based on the information supplied.
*/
String generateKey(T source);
String generateKey(JobParameters source);

}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ SELECT COUNT(*)

private DataFieldMaxValueIncrementer jobInstanceIncrementer;

private JobKeyGenerator<JobParameters> jobKeyGenerator = new DefaultJobKeyGenerator();
private JobKeyGenerator jobKeyGenerator = new DefaultJobKeyGenerator();

/**
* In this JDBC implementation a job instance id is obtained by asking the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class MongoJobInstanceDao implements JobInstanceDao {

private DataFieldMaxValueIncrementer jobInstanceIncrementer;

private JobKeyGenerator<JobParameters> jobKeyGenerator = new DefaultJobKeyGenerator();
private JobKeyGenerator jobKeyGenerator = new DefaultJobKeyGenerator();

private final JobInstanceConverter jobInstanceConverter = new JobInstanceConverter();

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

public void setJobKeyGenerator(JobKeyGenerator<JobParameters> jobKeyGenerator) {
public void setJobKeyGenerator(JobKeyGenerator jobKeyGenerator) {
this.jobKeyGenerator = jobKeyGenerator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

class DefaultJobKeyGeneratorTests {

private final JobKeyGenerator<JobParameters> jobKeyGenerator = new DefaultJobKeyGenerator();
private final JobKeyGenerator jobKeyGenerator = new DefaultJobKeyGenerator();

@Test
void testNullParameters() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.core.converter.JobParametersConverter;
import org.springframework.batch.core.converter.JsonJobParametersConverter;
import org.springframework.batch.core.job.parameters.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.core.repository.JobRepository;
Expand Down Expand Up @@ -291,14 +292,14 @@ public JdbcTransactionManager transactionManager(DataSource dataSource) {
}

@Bean
public JobKeyGenerator<Object> jobKeyGenerator() {
public JobKeyGenerator jobKeyGenerator() {
return new TestCustomJobKeyGenerator();
}

private static class TestCustomJobKeyGenerator implements JobKeyGenerator<Object> {
private static class TestCustomJobKeyGenerator implements JobKeyGenerator {

@Override
public String generateKey(Object source) {
public String generateKey(JobParameters source) {
return "1";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import javax.sql.DataSource;

import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -26,6 +27,7 @@
import org.springframework.aop.framework.Advised;
import org.springframework.batch.core.job.DefaultJobKeyGenerator;
import org.springframework.batch.core.job.JobKeyGenerator;
import org.springframework.batch.core.job.parameters.JobParameters;
import org.springframework.batch.core.repository.explore.JobExplorer;
import org.springframework.batch.core.repository.explore.support.JobExplorerFactoryBean;
import org.springframework.jdbc.core.JdbcOperations;
Expand Down Expand Up @@ -148,10 +150,10 @@ public void testCustomJobKeyGenerator() throws Exception {
Assertions.assertEquals(CustomJobKeyGenerator.class, jobKeyGenerator.getClass());
}

static class CustomJobKeyGenerator implements JobKeyGenerator<String> {
static class CustomJobKeyGenerator implements JobKeyGenerator {

@Override
public String generateKey(String source) {
public @NotNull String generateKey(@NotNull JobParameters source) {
return "1";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

import org.jetbrains.annotations.NotNull;
import org.springframework.batch.core.job.JobKeyGenerator;
import org.springframework.batch.core.job.parameters.JobParameters;

public class CustomJobKeyGenerator implements JobKeyGenerator<String> {
public class CustomJobKeyGenerator implements JobKeyGenerator {

@Override
public @NotNull String generateKey(@NotNull String source) {
public @NotNull String generateKey(@NotNull JobParameters source) {
return "1";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,24 +334,22 @@ void testCustomLobType() throws Exception {
@Test
public void testDefaultJobKeyGenerator() throws Exception {
testCreateRepository();
@SuppressWarnings("rawtypes")
JobKeyGenerator<?> jobKeyGenerator = (JobKeyGenerator) ReflectionTestUtils.getField(factory, "jobKeyGenerator");
JobKeyGenerator jobKeyGenerator = (JobKeyGenerator) ReflectionTestUtils.getField(factory, "jobKeyGenerator");
assertEquals(DefaultJobKeyGenerator.class, jobKeyGenerator.getClass());
}

@Test
public void testCustomJobKeyGenerator() throws Exception {
factory.setJobKeyGenerator(new CustomJobKeyGenerator());
testCreateRepository();
@SuppressWarnings("rawtypes")
JobKeyGenerator<?> jobKeyGenerator = (JobKeyGenerator) ReflectionTestUtils.getField(factory, "jobKeyGenerator");
JobKeyGenerator jobKeyGenerator = (JobKeyGenerator) ReflectionTestUtils.getField(factory, "jobKeyGenerator");
assertEquals(CustomJobKeyGenerator.class, jobKeyGenerator.getClass());
}

static class CustomJobKeyGenerator implements JobKeyGenerator<String> {
static class CustomJobKeyGenerator implements JobKeyGenerator {

@Override
public String generateKey(String source) {
public String generateKey(JobParameters source) {
return "1";
}

Expand Down
Loading