Refine contribution #4521

- Update reference documentation
- Minor test updates

(cherry picked from commit d6b6361e59)
This commit is contained in:
Mahmoud Ben Hassine
2024-02-06 15:21:47 +01:00
parent 4247561073
commit 9ae45ae52e
3 changed files with 50 additions and 10 deletions

View File

@@ -113,8 +113,8 @@ public class JobRegistrySmartInitializingSingleton
}
/**
* 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 {

View File

@@ -29,6 +29,7 @@ import org.springframework.beans.factory.ListableBeanFactory;
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 @@ import static org.mockito.Mockito.mock;
/**
* @author Henning Pöttker
* @author Mahmoud Ben Hassine
*/
class JobRegistrySmartInitializingSingletonTests {
@@ -59,34 +61,38 @@ class JobRegistrySmartInitializingSingletonTests {
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<String> 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<String> 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

View File

@@ -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]
----
<bean class="org.springframework.batch.core.configuration.support.JobRegistrySmartInitializingSingleton">
<property name="jobRegistry" ref="jobRegistry" />
</bean>
----
====
[[automaticjobregistrar]]
=== AutomaticJobRegistrar