Skip to content

Commit 9ae45ae

Browse files
committed
Refine contribution #4521
- Update reference documentation - Minor test updates (cherry picked from commit d6b6361)
1 parent 4247561 commit 9ae45ae

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ public void afterPropertiesSet() throws Exception {
113113
}
114114

115115
/**
116-
* Unregister all the {@link Job} instances that were registered by this post
117-
* processor.
116+
* Unregister all the {@link Job} instances that were registered by this smart
117+
* initializing singleton.
118118
*/
119119
@Override
120120
public void destroy() throws Exception {

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/JobRegistrySmartInitializingSingletonTests.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.context.support.ClassPathXmlApplicationContext;
3030

3131
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
3233
import static org.junit.jupiter.api.Assertions.assertNotNull;
3334
import static org.junit.jupiter.api.Assertions.assertThrows;
3435
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -37,6 +38,7 @@
3738

3839
/**
3940
* @author Henning Pöttker
41+
* @author Mahmoud Ben Hassine
4042
*/
4143
class JobRegistrySmartInitializingSingletonTests {
4244

@@ -59,34 +61,38 @@ void setUp() {
5961
void testInitializationFails() {
6062
singleton.setJobRegistry(null);
6163
var exception = assertThrows(IllegalStateException.class, singleton::afterPropertiesSet);
62-
assertTrue(exception.getMessage().contains("JobRegistry"));
64+
assertEquals("JobRegistry must not be null", exception.getMessage());
6365
}
6466

6567
@Test
6668
void testAfterSingletonsInstantiated() {
6769
singleton.afterSingletonsInstantiated();
68-
assertEquals("[foo]", jobRegistry.getJobNames().toString());
70+
Collection<String> jobNames = jobRegistry.getJobNames();
71+
assertEquals(1, jobNames.size());
72+
assertEquals("foo", jobNames.iterator().next());
6973
}
7074

7175
@Test
7276
void testAfterSingletonsInstantiatedWithGroupName() {
7377
singleton.setGroupName("jobs");
7478
singleton.afterSingletonsInstantiated();
75-
assertEquals("[jobs.foo]", jobRegistry.getJobNames().toString());
79+
Collection<String> jobNames = jobRegistry.getJobNames();
80+
assertEquals(1, jobNames.size());
81+
assertEquals("jobs.foo", jobNames.iterator().next());
7682
}
7783

7884
@Test
7985
void testAfterSingletonsInstantiatedWithDuplicate() {
8086
singleton.afterSingletonsInstantiated();
8187
var exception = assertThrows(FatalBeanException.class, singleton::afterSingletonsInstantiated);
82-
assertTrue(exception.getCause() instanceof DuplicateJobException);
88+
assertInstanceOf(DuplicateJobException.class, exception.getCause());
8389
}
8490

8591
@Test
8692
void testUnregisterOnDestroy() throws Exception {
8793
singleton.afterSingletonsInstantiated();
8894
singleton.destroy();
89-
assertEquals("[]", jobRegistry.getJobNames().toString());
95+
assertTrue(jobRegistry.getJobNames().isEmpty());
9096
}
9197

9298
@Test

spring-batch-docs/modules/ROOT/pages/job/advanced-meta-data.adoc

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ The following example shows how to include a `JobRegistry` for a job defined in
173173
174174
====
175175

176-
You can populate a `JobRegistry` in either of two ways: by using
177-
a bean post processor or by using a registrar lifecycle component. The coming
178-
sections describe these two mechanisms.
176+
You can populate a `JobRegistry` in one of the following ways: by using
177+
a bean post processor, or by using a smart initializing singleton or by using
178+
a registrar lifecycle component. The coming sections describe these mechanisms.
179179

180180
[[jobregistrybeanpostprocessor]]
181181
=== JobRegistryBeanPostProcessor
@@ -224,6 +224,40 @@ there to also be registered automatically.
224224

225225
As of version 5.1, the `@EnableBatchProcessing` annotation automatically registers a `jobRegistryBeanPostProcessor` bean in the application context.
226226

227+
[[jobregistrysmartinitializingsingleton]]
228+
=== JobRegistrySmartInitializingSingleton
229+
230+
This is a `SmartInitializingSingleton` that registers all singleton jobs within the job registry.
231+
232+
[tabs]
233+
====
234+
Java::
235+
+
236+
The following example shows how to define a `JobRegistrySmartInitializingSingleton` in Java:
237+
+
238+
.Java Configuration
239+
[source, java]
240+
----
241+
@Bean
242+
public JobRegistrySmartInitializingSingleton jobRegistrySmartInitializingSingleton(JobRegistry jobRegistry) {
243+
return new JobRegistrySmartInitializingSingleton(jobRegistry);
244+
}
245+
----
246+
247+
XML::
248+
+
249+
The following example shows how to define a `JobRegistrySmartInitializingSingleton` in XML:
250+
+
251+
.XML Configuration
252+
[source, xml]
253+
----
254+
<bean class="org.springframework.batch.core.configuration.support.JobRegistrySmartInitializingSingleton">
255+
<property name="jobRegistry" ref="jobRegistry" />
256+
</bean>
257+
----
258+
259+
====
260+
227261
[[automaticjobregistrar]]
228262
=== AutomaticJobRegistrar
229263

0 commit comments

Comments
 (0)