Skip to content

Commit

Permalink
Remove datasource dependency in JobRepositoryTestUtils
Browse files Browse the repository at this point in the history
Before this commit, the `JobRepositoryTestUtils` was
tied to the JDBC implementation of the `JobRepository`
as it was requiring a datasource. This makes it unusable
with implementations that do not rely on a datasource
to store batch meta-data (A MongoDB job repository for
instance where no datasource is used).

This commit decouples the `JobRepositoryTestUtils` from
the implementation details of the `JobRepository` by making
it working against the `JobRepository` interface.

This commit also introduces the necessary methods in the
`JobRepository` interface as well as various DAOs to
implement the utilities without having to deal with the
details of the underlying repository implementation.

Resolves #4070
  • Loading branch information
fmbenhassine committed Aug 23, 2022
1 parent f104baa commit c425137
Show file tree
Hide file tree
Showing 19 changed files with 354 additions and 96 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,8 @@
import org.springframework.transaction.annotation.Isolation;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
* <p>
Expand All @@ -47,6 +49,41 @@
*/
public interface JobRepository {

/**
* Retrieve the names of all job instances sorted alphabetically - i.e. jobs that have
* ever been executed.
* @return the names of all job instances
* @since 5.0
*/
default List<String> getJobNames() {
return Collections.emptyList();
}

/**
* Fetch the last job instances with the provided name, sorted backwards by primary
* key, using a 'like' criteria
* @param jobName {@link String} containing the name of the job.
* @param start int containing the offset of where list of job instances results
* should begin.
* @param count int containing the number of job instances to return.
* @return a list of {@link JobInstance} for the job name requested.
* @since 5.0
*/
default List<JobInstance> findJobInstancesByName(String jobName, int start, int count) {
return Collections.emptyList();
}

/**
* Return all {@link JobExecution}s for given {@link JobInstance}, sorted backwards by
* creation order (so the first element is the most recent).
* @param jobInstance parent {@link JobInstance} of the {@link JobExecution}s to find.
* @return {@link List} containing JobExecutions for the jobInstance.
* @since 5.0
*/
default List<JobExecution> findJobExecutions(JobInstance jobInstance) {
return Collections.emptyList();
}

/**
* Check if an instance of this job already exists with the parameters provided.
* @param jobName the name of the job
Expand Down Expand Up @@ -172,4 +209,33 @@ JobExecution createJobExecution(String jobName, JobParameters jobParameters)
@Nullable
JobExecution getLastJobExecution(String jobName, JobParameters jobParameters);

/**
* Delete the step execution along with its execution context.
* @param stepExecution the step execution to delete
* @since 5.0
*/
default void deleteStepExecution(StepExecution stepExecution) {
throw new UnsupportedOperationException();
}

/**
* Delete the job execution object graph (ie the job execution with its execution
* context, all related step executions and their executions contexts, as well as
* associated job parameters)
* @param jobExecution the job execution to delete
* @since 5.0
*/
default void deleteJobExecution(JobExecution jobExecution) {
throw new UnsupportedOperationException();
}

/**
* Delete the job instance.
* @param jobInstance the job instance to delete
* @since 5.0
*/
default void deleteJobInstance(JobInstance jobInstance) {
throw new UnsupportedOperationException();
}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@
*
* @author Robert Kasanicky
* @author David Turanski
* @author Mahmoud Ben Hassine
*/
public interface ExecutionContextDao {

Expand Down Expand Up @@ -78,4 +79,22 @@ public interface ExecutionContextDao {
*/
void updateExecutionContext(final StepExecution stepExecution);

/**
* Delete the execution context of the given {@link JobExecution}.
* @param jobExecution {@link JobExecution} that contains the context to delete.
* @since 5.0
*/
default void deleteExecutionContext(JobExecution jobExecution) {
throw new UnsupportedOperationException();
}

/**
* Delete the execution context of the given {@link StepExecution}.
* @param stepExecution {@link StepExecution} that contains the context to delete.
* @since 5.0
*/
default void deleteExecutionContext(StepExecution stepExecution) {
throw new UnsupportedOperationException();
}

}
Expand Up @@ -77,6 +77,12 @@ public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implem
private static final String UPDATE_STEP_EXECUTION_CONTEXT = "UPDATE %PREFIX%STEP_EXECUTION_CONTEXT "
+ "SET SHORT_CONTEXT = ?, SERIALIZED_CONTEXT = ? " + "WHERE STEP_EXECUTION_ID = ?";

private static final String DELETE_STEP_EXECUTION_CONTEXT = "DELETE FROM %PREFIX%STEP_EXECUTION_CONTEXT "
+ "WHERE STEP_EXECUTION_ID = ?";

private static final String DELETE_JOB_EXECUTION_CONTEXT = "DELETE FROM %PREFIX%JOB_EXECUTION_CONTEXT "
+ "WHERE JOB_EXECUTION_ID = ?";

private Charset charset = StandardCharsets.UTF_8;

private static final int DEFAULT_MAX_VARCHAR_LENGTH = 2500;
Expand Down Expand Up @@ -217,6 +223,22 @@ public void saveExecutionContexts(Collection<StepExecution> stepExecutions) {
persistSerializedContexts(serializedContexts, INSERT_STEP_EXECUTION_CONTEXT);
}

/**
* Delete the execution context of the given {@link JobExecution}.
* @param jobExecution {@link JobExecution} that contains the context to delete.
*/
public void deleteExecutionContext(JobExecution jobExecution) {
getJdbcTemplate().update(getQuery(DELETE_JOB_EXECUTION_CONTEXT), jobExecution.getId());
}

/**
* Delete the execution context of the given {@link StepExecution}.
* @param stepExecution {@link StepExecution} that contains the context to delete.
*/
public void deleteExecutionContext(StepExecution stepExecution) {
getJdbcTemplate().update(getQuery(DELETE_STEP_EXECUTION_CONTEXT), stepExecution.getId());
}

public void setLobHandler(LobHandler lobHandler) {
this.lobHandler = lobHandler;
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -94,6 +94,10 @@ public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements
private static final String CREATE_JOB_PARAMETERS = "INSERT into %PREFIX%JOB_EXECUTION_PARAMS(JOB_EXECUTION_ID, KEY_NAME, TYPE_CD, "
+ "STRING_VAL, DATE_VAL, LONG_VAL, DOUBLE_VAL, IDENTIFYING) values (?, ?, ?, ?, ?, ?, ?, ?)";

private static final String DELETE_JOB_EXECUTION = "DELETE FROM %PREFIX%JOB_EXECUTION WHERE JOB_EXECUTION_ID = ?";

private static final String DELETE_JOB_EXECUTION_PARAMETERS = "DELETE FROM %PREFIX%JOB_EXECUTION_PARAMS WHERE JOB_EXECUTION_ID = ?";

private int exitMessageLength = DEFAULT_EXIT_MESSAGE_LENGTH;

private DataFieldMaxValueIncrementer jobExecutionIncrementer;
Expand Down Expand Up @@ -303,6 +307,22 @@ public void synchronizeStatus(JobExecution jobExecution) {
}
}

/**
* Delete the given job execution.
* @param jobExecution the job execution to delete
*/
public void deleteJobExecution(JobExecution jobExecution) {
getJdbcTemplate().update(getQuery(DELETE_JOB_EXECUTION), jobExecution.getId());
}

/**
* Delete the parameters associated with the given job execution.
* @param jobExecution the job execution for which job parameters should be deleted
*/
public void deleteJobExecutionParameters(JobExecution jobExecution) {
getJdbcTemplate().update(getQuery(DELETE_JOB_EXECUTION_PARAMETERS), jobExecution.getId());
}

/**
* Convenience method that inserts all parameters from the provided JobParameters.
*
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -85,6 +85,8 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements

private static final String FIND_LAST_JOBS_LIKE_NAME = "SELECT JOB_INSTANCE_ID, JOB_NAME from %PREFIX%JOB_INSTANCE where JOB_NAME like ? order by JOB_INSTANCE_ID desc";

private static final String DELETE_JOB_INSTANCE = "DELETE FROM %PREFIX%JOB_INSTANCE WHERE JOB_INSTANCE_ID = ?";

private DataFieldMaxValueIncrementer jobInstanceIncrementer;

private JobKeyGenerator<JobParameters> jobKeyGenerator = new DefaultJobKeyGenerator();
Expand Down Expand Up @@ -276,6 +278,14 @@ public int getJobInstanceCount(@Nullable String jobName) throws NoSuchJobExcepti
}
}

/**
* Delete the job instance.
* @param jobInstance the job instance to delete
*/
public void deleteJobInstance(JobInstance jobInstance) {
getJdbcTemplate().update(getQuery(DELETE_JOB_INSTANCE), jobInstance.getId());
}

/**
* Setter for {@link DataFieldMaxValueIncrementer} to be used when generating primary
* keys for {@link JobInstance} instances.
Expand Down
Expand Up @@ -105,6 +105,9 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
+ " on SE.JOB_EXECUTION_ID = JE.JOB_EXECUTION_ID " + "where JE.JOB_INSTANCE_ID = ?"
+ " and SE.STEP_NAME = ?";

private static final String DELETE_STEP_EXECUTION = "DELETE FROM %PREFIX%STEP_EXECUTION "
+ "WHERE STEP_EXECUTION_ID = ?";

private int exitMessageLength = DEFAULT_EXIT_MESSAGE_LENGTH;

private DataFieldMaxValueIncrementer stepExecutionIncrementer;
Expand Down Expand Up @@ -353,6 +356,14 @@ public int countStepExecutions(JobInstance jobInstance, String stepName) {
jobInstance.getInstanceId(), stepName);
}

/**
* Delete the given step execution.
* @param stepExecution the step execution to delete
*/
public void deleteStepExecution(StepExecution stepExecution) {
getJdbcTemplate().update(getQuery(DELETE_STEP_EXECUTION), stepExecution.getId());
}

private static class StepExecutionRowMapper implements RowMapper<StepExecution> {

private final JobExecution jobExecution;
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2018 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.StepExecution;
import org.springframework.lang.Nullable;

/**
Expand Down Expand Up @@ -88,4 +89,22 @@ public interface JobExecutionDao {
*/
void synchronizeStatus(JobExecution jobExecution);

/**
* Delete the given job execution.
* @param jobExecution the job execution to delete
* @since 5.0
*/
default void deleteJobExecution(JobExecution jobExecution) {
throw new UnsupportedOperationException();
}

/**
* Delete the parameters associated with the given job execution.
* @param jobExecution the job execution for which job parameters should be deleted
* @since 5.0
*/
default void deleteJobExecutionParameters(JobExecution jobExecution) {
throw new UnsupportedOperationException();
}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2019 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -130,4 +130,13 @@ default JobInstance getLastJobInstance(String jobName) {
*/
int getJobInstanceCount(@Nullable String jobName) throws NoSuchJobException;

/**
* Delete the job instance.
* @param jobInstance the job instance to delete
* @since 5.0
*/
default void deleteJobInstance(JobInstance jobInstance) {
throw new UnsupportedOperationException();
}

}
Expand Up @@ -91,4 +91,13 @@ default int countStepExecutions(JobInstance jobInstance, String stepName) {
throw new UnsupportedOperationException();
}

/**
* Delete the given step execution.
* @param stepExecution the step execution to delete
* @since 5.0
*/
default void deleteStepExecution(StepExecution stepExecution) {
throw new UnsupportedOperationException();
}

}
Expand Up @@ -279,6 +279,24 @@ public JobExecution getLastJobExecution(String jobName, JobParameters jobParamet

}

@Override
public void deleteStepExecution(StepExecution stepExecution) {
this.ecDao.deleteExecutionContext(stepExecution);
this.stepExecutionDao.deleteStepExecution(stepExecution);
}

@Override
public void deleteJobExecution(JobExecution jobExecution) {
this.ecDao.deleteExecutionContext(jobExecution);
this.jobExecutionDao.deleteJobExecutionParameters(jobExecution);
this.jobExecutionDao.deleteJobExecution(jobExecution);
}

@Override
public void deleteJobInstance(JobInstance jobInstance) {
this.jobInstanceDao.deleteJobInstance(jobInstance);
}

@Override
public JobInstance createJobInstance(String jobName, JobParameters jobParameters) {
Assert.notNull(jobName, "A job name is required to create a JobInstance");
Expand Down

0 comments on commit c425137

Please sign in to comment.