Skip to content

Commit

Permalink
Refine contribution #4521
Browse files Browse the repository at this point in the history
- Update reference documentation
- Minor test updates
  • Loading branch information
fmbenhassine committed Feb 6, 2024
1 parent fe26d00 commit d6b6361
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,6 +38,7 @@

/**
* @author Henning Pöttker
* @author Mahmoud Ben Hassine
*/
class JobRegistrySmartInitializingSingletonTests {

Expand All @@ -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<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
Expand Down
40 changes: 37 additions & 3 deletions spring-batch-docs/modules/ROOT/pages/job/advanced-meta-data.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit d6b6361

Please sign in to comment.