Skip to content

Commit fe26d00

Browse files
hpoettkerfmbenhassine
authored andcommitted
Add JobRegistrySmartInitializingSingleton
1 parent c61670b commit fe26d00

File tree

4 files changed

+371
-1
lines changed

4 files changed

+371
-1
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/JobRegistryBeanPostProcessor.java

Lines changed: 5 additions & 1 deletion
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-2024 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.
@@ -40,6 +40,10 @@
4040
* {@link JobRegistry}. Include a bean of this type along with your job configuration and
4141
* use the same {@link JobRegistry} as a {@link JobLocator} when you need to locate a
4242
* {@link Job} to launch.
43+
* <p>
44+
* An alternative to this class is {@link JobRegistrySmartInitializingSingleton}, which is
45+
* recommended in cases where this class may cause early bean initializations. You must
46+
* include at most one of either of them as a bean.
4347
*
4448
* @author Dave Syer
4549
* @author Mahmoud Ben Hassine
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.configuration.support;
17+
18+
import java.util.Collection;
19+
import java.util.HashSet;
20+
import java.util.Map;
21+
22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
import org.springframework.batch.core.Job;
25+
import org.springframework.batch.core.configuration.DuplicateJobException;
26+
import org.springframework.batch.core.configuration.JobLocator;
27+
import org.springframework.batch.core.configuration.JobRegistry;
28+
import org.springframework.beans.BeansException;
29+
import org.springframework.beans.FatalBeanException;
30+
import org.springframework.beans.factory.BeanFactory;
31+
import org.springframework.beans.factory.BeanFactoryAware;
32+
import org.springframework.beans.factory.DisposableBean;
33+
import org.springframework.beans.factory.InitializingBean;
34+
import org.springframework.beans.factory.ListableBeanFactory;
35+
import org.springframework.beans.factory.SmartInitializingSingleton;
36+
import org.springframework.beans.factory.config.BeanDefinition;
37+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
38+
import org.springframework.util.Assert;
39+
40+
/**
41+
* A {@link SmartInitializingSingleton} that registers {@link Job} beans with a
42+
* {@link JobRegistry}. Include a bean of this type along with your job configuration and
43+
* use the same {@link JobRegistry} as a {@link JobLocator} when you need to locate a
44+
* {@link Job} to launch.
45+
* <p>
46+
* This class is an alternative to {@link JobRegistryBeanPostProcessor} and prevents early
47+
* bean initializations. You must include at most one of either of them as a bean.
48+
*
49+
* @author Henning Pöttker
50+
* @since 5.1.1
51+
*/
52+
public class JobRegistrySmartInitializingSingleton
53+
implements SmartInitializingSingleton, BeanFactoryAware, InitializingBean, DisposableBean {
54+
55+
private static final Log logger = LogFactory.getLog(JobRegistrySmartInitializingSingleton.class);
56+
57+
// It doesn't make sense for this to have a default value...
58+
private JobRegistry jobRegistry = null;
59+
60+
private final Collection<String> jobNames = new HashSet<>();
61+
62+
private String groupName = null;
63+
64+
private ListableBeanFactory beanFactory;
65+
66+
/**
67+
* Default constructor.
68+
*/
69+
public JobRegistrySmartInitializingSingleton() {
70+
}
71+
72+
/**
73+
* Convenience constructor for setting the {@link JobRegistry}.
74+
* @param jobRegistry the {@link JobRegistry} to register the {@link Job}s with
75+
*/
76+
public JobRegistrySmartInitializingSingleton(JobRegistry jobRegistry) {
77+
this.jobRegistry = jobRegistry;
78+
}
79+
80+
/**
81+
* The group name for jobs registered by this component. Optional (defaults to null,
82+
* which means that jobs are registered with their bean names). Useful where there is
83+
* a hierarchy of application contexts all contributing to the same
84+
* {@link JobRegistry}: child contexts can then define an instance with a unique group
85+
* name to avoid clashes between job names.
86+
* @param groupName the groupName to set
87+
*/
88+
public void setGroupName(String groupName) {
89+
this.groupName = groupName;
90+
}
91+
92+
/**
93+
* Injection setter for {@link JobRegistry}.
94+
* @param jobRegistry the {@link JobRegistry} to register the {@link Job}s with
95+
*/
96+
public void setJobRegistry(JobRegistry jobRegistry) {
97+
this.jobRegistry = jobRegistry;
98+
}
99+
100+
@Override
101+
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
102+
if (beanFactory instanceof ListableBeanFactory listableBeanFactory) {
103+
this.beanFactory = listableBeanFactory;
104+
}
105+
}
106+
107+
/**
108+
* Make sure the registry is set before use.
109+
*/
110+
@Override
111+
public void afterPropertiesSet() throws Exception {
112+
Assert.state(jobRegistry != null, "JobRegistry must not be null");
113+
}
114+
115+
/**
116+
* Unregister all the {@link Job} instances that were registered by this post
117+
* processor.
118+
*/
119+
@Override
120+
public void destroy() throws Exception {
121+
for (String name : jobNames) {
122+
if (logger.isDebugEnabled()) {
123+
logger.debug("Unregistering job: " + name);
124+
}
125+
jobRegistry.unregister(name);
126+
}
127+
jobNames.clear();
128+
}
129+
130+
@Override
131+
public void afterSingletonsInstantiated() {
132+
if (beanFactory == null) {
133+
return;
134+
}
135+
Map<String, Job> jobs = beanFactory.getBeansOfType(Job.class, false, false);
136+
for (var entry : jobs.entrySet()) {
137+
postProcessAfterInitialization(entry.getValue(), entry.getKey());
138+
}
139+
}
140+
141+
private void postProcessAfterInitialization(Job job, String beanName) {
142+
try {
143+
String groupName = this.groupName;
144+
if (beanFactory instanceof DefaultListableBeanFactory defaultListableBeanFactory
145+
&& beanFactory.containsBean(beanName)) {
146+
groupName = getGroupName(defaultListableBeanFactory.getBeanDefinition(beanName), job);
147+
}
148+
job = groupName == null ? job : new GroupAwareJob(groupName, job);
149+
ReferenceJobFactory jobFactory = new ReferenceJobFactory(job);
150+
String name = jobFactory.getJobName();
151+
if (logger.isDebugEnabled()) {
152+
logger.debug("Registering job: " + name);
153+
}
154+
jobRegistry.register(jobFactory);
155+
jobNames.add(name);
156+
}
157+
catch (DuplicateJobException e) {
158+
throw new FatalBeanException("Cannot register job configuration", e);
159+
}
160+
}
161+
162+
/**
163+
* Determine a group name for the job to be registered. The default implementation
164+
* returns the {@link #setGroupName(String) groupName} configured. Provides an
165+
* extension point for specialised subclasses.
166+
* @param beanDefinition the bean definition for the job
167+
* @param job the job
168+
* @return a group name for the job (or null if not needed)
169+
*/
170+
protected String getGroupName(BeanDefinition beanDefinition, Job job) {
171+
return groupName;
172+
}
173+
174+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.configuration.support;
17+
18+
import java.util.Collection;
19+
import java.util.Map;
20+
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.batch.core.Job;
24+
import org.springframework.batch.core.configuration.DuplicateJobException;
25+
import org.springframework.batch.core.configuration.JobRegistry;
26+
import org.springframework.batch.core.job.JobSupport;
27+
import org.springframework.beans.FatalBeanException;
28+
import org.springframework.beans.factory.ListableBeanFactory;
29+
import org.springframework.context.support.ClassPathXmlApplicationContext;
30+
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertNotNull;
33+
import static org.junit.jupiter.api.Assertions.assertThrows;
34+
import static org.junit.jupiter.api.Assertions.assertTrue;
35+
import static org.mockito.Mockito.lenient;
36+
import static org.mockito.Mockito.mock;
37+
38+
/**
39+
* @author Henning Pöttker
40+
*/
41+
class JobRegistrySmartInitializingSingletonTests {
42+
43+
private final JobRegistry jobRegistry = new MapJobRegistry();
44+
45+
private final JobRegistrySmartInitializingSingleton singleton = new JobRegistrySmartInitializingSingleton(
46+
jobRegistry);
47+
48+
private final ListableBeanFactory beanFactory = mock(ListableBeanFactory.class);
49+
50+
@BeforeEach
51+
void setUp() {
52+
var job = new JobSupport();
53+
job.setName("foo");
54+
lenient().when(beanFactory.getBeansOfType(Job.class, false, false)).thenReturn(Map.of("bar", job));
55+
singleton.setBeanFactory(beanFactory);
56+
}
57+
58+
@Test
59+
void testInitializationFails() {
60+
singleton.setJobRegistry(null);
61+
var exception = assertThrows(IllegalStateException.class, singleton::afterPropertiesSet);
62+
assertTrue(exception.getMessage().contains("JobRegistry"));
63+
}
64+
65+
@Test
66+
void testAfterSingletonsInstantiated() {
67+
singleton.afterSingletonsInstantiated();
68+
assertEquals("[foo]", jobRegistry.getJobNames().toString());
69+
}
70+
71+
@Test
72+
void testAfterSingletonsInstantiatedWithGroupName() {
73+
singleton.setGroupName("jobs");
74+
singleton.afterSingletonsInstantiated();
75+
assertEquals("[jobs.foo]", jobRegistry.getJobNames().toString());
76+
}
77+
78+
@Test
79+
void testAfterSingletonsInstantiatedWithDuplicate() {
80+
singleton.afterSingletonsInstantiated();
81+
var exception = assertThrows(FatalBeanException.class, singleton::afterSingletonsInstantiated);
82+
assertTrue(exception.getCause() instanceof DuplicateJobException);
83+
}
84+
85+
@Test
86+
void testUnregisterOnDestroy() throws Exception {
87+
singleton.afterSingletonsInstantiated();
88+
singleton.destroy();
89+
assertEquals("[]", jobRegistry.getJobNames().toString());
90+
}
91+
92+
@Test
93+
void testExecutionWithApplicationContext() throws Exception {
94+
var context = new ClassPathXmlApplicationContext("test-context-with-smart-initializing-singleton.xml",
95+
getClass());
96+
var registry = context.getBean("registry", JobRegistry.class);
97+
Collection<String> jobNames = registry.getJobNames();
98+
String[] names = context.getBeanNamesForType(JobSupport.class);
99+
int count = names.length;
100+
// Each concrete bean of type JobConfiguration is registered...
101+
assertEquals(count, jobNames.size());
102+
// N.B. there is a failure / wonky mode where a parent bean is given an
103+
// explicit name or beanName (using property setter): in this case then
104+
// child beans will have the same name and will be re-registered (and
105+
// override, if the registry supports that).
106+
assertNotNull(registry.getJob("test-job"));
107+
assertEquals(context.getBean("test-job-with-name"), registry.getJob("foo"));
108+
assertEquals(context.getBean("test-job-with-bean-name"), registry.getJob("bar"));
109+
assertEquals(context.getBean("test-job-with-parent-and-name"), registry.getJob("spam"));
110+
assertEquals(context.getBean("test-job-with-parent-and-bean-name"), registry.getJob("bucket"));
111+
assertEquals(context.getBean("test-job-with-concrete-parent"), registry.getJob("maps"));
112+
assertEquals(context.getBean("test-job-with-concrete-parent-and-name"), registry.getJob("oof"));
113+
assertEquals(context.getBean("test-job-with-concrete-parent-and-bean-name"), registry.getJob("rab"));
114+
}
115+
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:p="http://www.springframework.org/schema/p"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="
6+
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
7+
8+
<bean
9+
class="org.springframework.batch.core.configuration.support.JobRegistrySmartInitializingSingleton">
10+
<property name="jobRegistry" ref="registry" />
11+
</bean>
12+
13+
<bean id="registry"
14+
class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
15+
16+
<bean id="test-job"
17+
class="org.springframework.batch.core.job.JobSupport">
18+
<property name="steps">
19+
<bean id="step1" class="org.springframework.batch.core.step.factory.SimpleStepFactoryBean">
20+
<property name="itemReader" ref="itemReader" />
21+
<property name="itemWriter" ref="itemWriter" />
22+
<property name="jobRepository" ref="jobRepository" />
23+
<property name="transactionManager" ref="transactionManager"/>
24+
</bean>
25+
</property>
26+
</bean>
27+
28+
<bean id="itemReader"
29+
class="org.springframework.batch.item.support.ListItemReader">
30+
<constructor-arg value="foo,bar,spam" />
31+
</bean>
32+
33+
<bean id="itemWriter"
34+
class="org.springframework.batch.core.launch.EmptyItemWriter" />
35+
36+
<bean id="jobRepository"
37+
class="org.springframework.batch.core.step.JobRepositorySupport" />
38+
39+
<bean id="transactionManager"
40+
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
41+
42+
<bean id="test-job-with-name"
43+
class="org.springframework.batch.core.job.JobSupport">
44+
<property name="name" value="foo" />
45+
</bean>
46+
47+
<bean id="test-job-with-bean-name"
48+
class="org.springframework.batch.core.job.JobSupport">
49+
<property name="beanName" value="bar" />
50+
</bean>
51+
52+
<bean id="abstract-job"
53+
class="org.springframework.batch.core.job.JobSupport"
54+
abstract="true" />
55+
56+
<bean id="test-job-with-parent" parent="abstract-job" />
57+
58+
<bean id="test-job-with-parent-and-name" parent="abstract-job"
59+
p:name="spam" />
60+
61+
<bean id="test-job-with-parent-and-bean-name" parent="abstract-job"
62+
p:name="bucket" />
63+
64+
<bean id="parent-job"
65+
class="org.springframework.batch.core.job.JobSupport" />
66+
67+
<bean id="test-job-with-concrete-parent" parent="parent-job"
68+
p:name="maps" />
69+
70+
<bean id="test-job-with-concrete-parent-and-name"
71+
parent="parent-job" p:name="oof" />
72+
73+
<bean id="test-job-with-concrete-parent-and-bean-name"
74+
parent="parent-job" p:beanName="rab" />
75+
76+
</beans>

0 commit comments

Comments
 (0)