BATCH-2564: make AutomaticJobRegistrar implement SmartLifeCycle
Before this commit, the AutomaticJobRegistrar was started on ContextRefreshedEvent event. This is sometimes too late to register jobs especially when other life cycle components that use the jobs are started before this registrar. This commit changes the AutomaticJobRegistrar to implement SmartLifeCycle and makes its autoStartup and phase properties configurable. It should be noted that the "onApplicationEvent" method has been removed even if it is a public API. This method is not intended to be used by client code and even if it was, its usage is considered wrong anyway. Resolves BATCH-2564
This commit is contained in:
committed by
Michael Minella
parent
b02367d6ce
commit
4dc406ef60
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2014 the original author or authors.
|
||||
* Copyright 2010-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -25,11 +25,10 @@ import java.util.Collection;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.Ordered;
|
||||
@@ -40,6 +39,7 @@ import org.springframework.core.io.Resource;
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Lucas Ward
|
||||
* @author Mahmoud Ben Hassine
|
||||
*
|
||||
*/
|
||||
public class AutomaticJobRegistrarTests {
|
||||
@@ -66,6 +66,20 @@ public class AutomaticJobRegistrarTests {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultAutoStartup() throws Exception {
|
||||
|
||||
assertTrue(registrar.isAutoStartup());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultPhase() throws Exception {
|
||||
|
||||
assertEquals(Integer.MIN_VALUE + 1000, registrar.getPhase());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocateJob() throws Exception {
|
||||
|
||||
@@ -176,36 +190,21 @@ public class AutomaticJobRegistrarTests {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
@Test
|
||||
public void testInitCalledOnContextRefreshed() throws Exception {
|
||||
public void testStartStopRunningWithCallback() throws Exception {
|
||||
|
||||
Runnable callback = Mockito.mock(Runnable.class);
|
||||
Resource[] jobPaths = new Resource[] { new ClassPathResource(
|
||||
"org/springframework/batch/core/launch/support/2jobs.xml") };
|
||||
registrar.setApplicationContext(new ClassPathXmlApplicationContext(
|
||||
"/org/springframework/batch/core/launch/support/test-environment-with-registry-and-auto-register.xml"));
|
||||
GenericApplicationContext applicationContext = new GenericApplicationContext();
|
||||
applicationContext.refresh();
|
||||
setUpApplicationContextFactories(jobPaths, applicationContext);
|
||||
registrar.setApplicationContext(applicationContext);
|
||||
registrar.onApplicationEvent(new ContextRefreshedEvent(applicationContext));
|
||||
assertEquals(2, registry.getJobNames().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClearCalledOnContextClosed() throws Exception {
|
||||
|
||||
Resource[] jobPaths = new Resource[] { new ClassPathResource(
|
||||
"org/springframework/batch/core/launch/support/2jobs.xml") };
|
||||
@SuppressWarnings("resource")
|
||||
GenericApplicationContext applicationContext = new GenericApplicationContext();
|
||||
applicationContext.refresh();
|
||||
setUpApplicationContextFactories(jobPaths, applicationContext);
|
||||
registrar.setApplicationContext(applicationContext);
|
||||
setUpApplicationContextFactories(jobPaths, null);
|
||||
registrar.start();
|
||||
assertTrue(registrar.isRunning());
|
||||
registrar.start();
|
||||
assertEquals(2, registry.getJobNames().size());
|
||||
registrar.onApplicationEvent(new ContextClosedEvent(applicationContext));
|
||||
registrar.stop(callback);
|
||||
assertFalse(registrar.isRunning());
|
||||
assertEquals(0, registry.getJobNames().size());
|
||||
Mockito.verify(callback, Mockito.times(1)).run();
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user