diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/JobRegistrySmartInitializingSingleton.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/JobRegistrySmartInitializingSingleton.java index 9e4bbb3a4f..ede418cf23 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/JobRegistrySmartInitializingSingleton.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/JobRegistrySmartInitializingSingleton.java @@ -113,8 +113,8 @@ public void afterPropertiesSet() throws Exception { } /** - * Unregister all the {@link Job} instances that were registered by this post - * processor. + * Unregister all the {@link Job} instances that were registered by this smart + * initializing singleton. */ @Override public void destroy() throws Exception { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/JobRegistrySmartInitializingSingletonTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/JobRegistrySmartInitializingSingletonTests.java index 7738ee2d4f..f6db1e0187 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/JobRegistrySmartInitializingSingletonTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/JobRegistrySmartInitializingSingletonTests.java @@ -29,6 +29,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -37,6 +38,7 @@ /** * @author Henning Pƶttker + * @author Mahmoud Ben Hassine */ class JobRegistrySmartInitializingSingletonTests { @@ -59,34 +61,38 @@ void setUp() { void testInitializationFails() { singleton.setJobRegistry(null); var exception = assertThrows(IllegalStateException.class, singleton::afterPropertiesSet); - assertTrue(exception.getMessage().contains("JobRegistry")); + assertEquals("JobRegistry must not be null", exception.getMessage()); } @Test void testAfterSingletonsInstantiated() { singleton.afterSingletonsInstantiated(); - assertEquals("[foo]", jobRegistry.getJobNames().toString()); + Collection jobNames = jobRegistry.getJobNames(); + assertEquals(1, jobNames.size()); + assertEquals("foo", jobNames.iterator().next()); } @Test void testAfterSingletonsInstantiatedWithGroupName() { singleton.setGroupName("jobs"); singleton.afterSingletonsInstantiated(); - assertEquals("[jobs.foo]", jobRegistry.getJobNames().toString()); + Collection jobNames = jobRegistry.getJobNames(); + assertEquals(1, jobNames.size()); + assertEquals("jobs.foo", jobNames.iterator().next()); } @Test void testAfterSingletonsInstantiatedWithDuplicate() { singleton.afterSingletonsInstantiated(); var exception = assertThrows(FatalBeanException.class, singleton::afterSingletonsInstantiated); - assertTrue(exception.getCause() instanceof DuplicateJobException); + assertInstanceOf(DuplicateJobException.class, exception.getCause()); } @Test void testUnregisterOnDestroy() throws Exception { singleton.afterSingletonsInstantiated(); singleton.destroy(); - assertEquals("[]", jobRegistry.getJobNames().toString()); + assertTrue(jobRegistry.getJobNames().isEmpty()); } @Test diff --git a/spring-batch-docs/modules/ROOT/pages/job/advanced-meta-data.adoc b/spring-batch-docs/modules/ROOT/pages/job/advanced-meta-data.adoc index 94fc236f5c..bd41a5d941 100644 --- a/spring-batch-docs/modules/ROOT/pages/job/advanced-meta-data.adoc +++ b/spring-batch-docs/modules/ROOT/pages/job/advanced-meta-data.adoc @@ -173,9 +173,9 @@ The following example shows how to include a `JobRegistry` for a job defined in ==== -You can populate a `JobRegistry` in either of two ways: by using -a bean post processor or by using a registrar lifecycle component. The coming -sections describe these two mechanisms. +You can populate a `JobRegistry` in one of the following ways: by using +a bean post processor, or by using a smart initializing singleton or by using +a registrar lifecycle component. The coming sections describe these mechanisms. [[jobregistrybeanpostprocessor]] === JobRegistryBeanPostProcessor @@ -224,6 +224,40 @@ there to also be registered automatically. As of version 5.1, the `@EnableBatchProcessing` annotation automatically registers a `jobRegistryBeanPostProcessor` bean in the application context. +[[jobregistrysmartinitializingsingleton]] +=== JobRegistrySmartInitializingSingleton + +This is a `SmartInitializingSingleton` that registers all singleton jobs within the job registry. + +[tabs] +==== +Java:: ++ +The following example shows how to define a `JobRegistrySmartInitializingSingleton` in Java: ++ +.Java Configuration +[source, java] +---- +@Bean +public JobRegistrySmartInitializingSingleton jobRegistrySmartInitializingSingleton(JobRegistry jobRegistry) { + return new JobRegistrySmartInitializingSingleton(jobRegistry); +} +---- + +XML:: ++ +The following example shows how to define a `JobRegistrySmartInitializingSingleton` in XML: ++ +.XML Configuration +[source, xml] +---- + + + +---- + +==== + [[automaticjobregistrar]] === AutomaticJobRegistrar