Skip to content

Improve error message when using ResourcelessJobRepository with partitioned steps #4952

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 @@ -33,6 +33,7 @@
import org.springframework.batch.core.partition.Partitioner;
import org.springframework.batch.core.partition.StepExecutionSplitter;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.ResourcelessJobRepository;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -148,6 +149,12 @@ public String getStepName() {
@Override
public Set<StepExecution> split(StepExecution stepExecution, int gridSize) throws JobExecutionException {

if (jobRepository instanceof ResourcelessJobRepository) {
throw new JobExecutionException("ResourcelessJobRepository cannot be used with partitioned steps "
+ "as it does not support execution context. Please use a different JobRepository implementation "
+ "that supports batch meta-data storage for partitioned steps.");
}

JobExecution jobExecution = stepExecution.getJobExecution();

Map<String, ExecutionContext> contexts = getContexts(stepExecution, gridSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.batch.core.partition.Partitioner;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JdbcJobRepositoryFactoryBean;
import org.springframework.batch.core.repository.support.ResourcelessJobRepository;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.jdbc.support.JdbcTransactionManager;
Expand All @@ -44,6 +45,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class SimpleStepExecutionSplitterTests {
Expand Down Expand Up @@ -219,6 +221,26 @@ void testAbandonedStatus() throws Exception {
}
}

@Test
void testResourcelessJobRepositoryThrowsException() throws Exception {
// Create a ResourcelessJobRepository for testing
ResourcelessJobRepository resourcelessRepo = new ResourcelessJobRepository();
JobExecution jobExecution = resourcelessRepo.createJobExecution("job", new JobParameters());
StepExecution stepExecution = jobExecution.createStepExecution("bar");

// Create splitter with ResourcelessJobRepository
SimpleStepExecutionSplitter splitter = new SimpleStepExecutionSplitter(resourcelessRepo, true, step.getName(),
new SimplePartitioner());

// Verify that attempting to split with ResourcelessJobRepository throws
// appropriate exception
JobExecutionException exception = assertThrows(JobExecutionException.class,
() -> splitter.split(stepExecution, 2));

assertTrue(exception.getMessage().contains("ResourcelessJobRepository cannot be used with partitioned steps"));
assertTrue(exception.getMessage().contains("does not support execution context"));
}

private StepExecution update(Set<StepExecution> split, StepExecution stepExecution, BatchStatus status)
throws Exception {
return update(split, stepExecution, status, true);
Expand Down