Skip to content
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
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2025 the original author or authors.
* Copyright 2012-2026 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 @@ -74,6 +74,7 @@
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Taeik Lim
* @author Yanming Zhou
* @since 5.0
*/
@Configuration(proxyBeanMethods = false)
Expand Down Expand Up @@ -113,16 +114,18 @@ public JobOperator jobOperator(JobRepository jobRepository) throws BatchConfigur

// FIXME getter with side effect, see JobOperatorFactoryBean.populateJobRegistry
protected JobRegistry getJobRegistry() {
MapJobRegistry jobRegistry = new MapJobRegistry();
this.applicationContext.getBeansOfType(Job.class).values().forEach(job -> {
try {
jobRegistry.register(job);
}
catch (DuplicateJobException e) {
throw new BatchConfigurationException(e);
}
return this.applicationContext.getBeanProvider(JobRegistry.class).getIfAvailable(() -> {
MapJobRegistry jobRegistry = new MapJobRegistry();
this.applicationContext.getBeansOfType(Job.class).values().forEach(job -> {
try {
jobRegistry.register(job);
}
catch (DuplicateJobException e) {
throw new BatchConfigurationException(e);
}
});
return jobRegistry;
});
return jobRegistry;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2025 the original author or authors.
* Copyright 2022-2026 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 @@ -23,9 +23,11 @@
import org.junit.jupiter.api.Test;

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.job.Job;
import org.springframework.batch.core.job.JobExecution;
import org.springframework.batch.core.job.parameters.JobParameters;
import org.springframework.batch.core.launch.JobRestartException;
import org.springframework.batch.core.repository.support.ResourcelessJobRepository;
import org.springframework.batch.core.step.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
Expand All @@ -42,8 +44,12 @@
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

/**
* @author Mahmoud Ben Hassine
* @author Yanming Zhou
*/
class DefaultBatchConfigurationTests {

Expand Down Expand Up @@ -85,6 +91,25 @@ void testDefaultInfrastructureBeansRegistration() {
Assertions.assertNotNull(jobOperator);
}

@Test
void testConfigurationWithCustomJobRegistry() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
CustomJobRegistryJobConfiguration.class);
Job job = context.getBean(Job.class);
JobOperator jobOperator = context.getBean(JobOperator.class);
JobExecution jobExecution = jobOperator.start(job, new JobParameters());

try {
jobOperator.restart(jobExecution);
}
catch (JobRestartException ignored) {
// ignore
}

JobRegistry jobRegistry = context.getBean(JobRegistry.class);
verify(jobRegistry).getJob("job");
}

@Configuration
static class MyJobConfiguration extends JdbcDefaultBatchConfiguration {

Expand Down Expand Up @@ -125,4 +150,14 @@ public JobRepository jobRepository() {

}

@Configuration
static class CustomJobRegistryJobConfiguration extends MyJobConfiguration {

@Bean
public JobRegistry jobRegistry() {
return spy(new MapJobRegistry());
}

}

}