Update TaskLifecycleListener to use SmartLifecycle

This commit changes the starting point of a task from the point when the
ApplicationContext issues the ContextRefreshedEvent to
SmartLifecycle#start.  This is a more accurate point of start for a task
in that all beans should now be available.  It also allows us to clean
up many ApplicationContext hacks that were present to get around the
fact that many beans were not ready when a Task was attempting to begin.

Resolves spring-cloud/spring-cloud-task#107
This commit is contained in:
Michael Minella
2016-03-11 14:17:02 -06:00
committed by Glenn Renfro
parent 7c8fc5f50e
commit f35f8ef52d
17 changed files with 384 additions and 310 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 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.
@@ -16,38 +16,76 @@
package org.springframework.cloud.task;
import static junit.framework.TestCase.assertNotNull;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertThat;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.framework.Advised;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.cloud.task.configuration.DefaultTaskConfigurer;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.cloud.task.configuration.SimpleTaskConfiguration;
import org.springframework.cloud.task.configuration.TaskConfigurer;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.cloud.task.repository.support.SimpleTaskRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* Verifies that the beans created by the SimpleTaskConfiguration.
*
* @author Glenn Renfro
* @author Michael Minella
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SimpleTaskConfiguration.class, PropertyPlaceholderAutoConfiguration.class})
public class SimpleTaskConfigurationTests {
@Autowired
private TaskRepository taskRepository;
private ConfigurableApplicationContext context;
@After
public void tearDown() {
if(this.context != null) {
this.context.close();
}
}
@Test
public void testRepository() throws Exception {
assertNotNull("testRepository should not be null", taskRepository);
TaskRepository clazz = (TaskRepository) ((Advised)taskRepository).getTargetSource().getTarget();
assertThat(clazz, instanceOf(SimpleTaskRepository.class));
}
}
this.context = new AnnotationConfigApplicationContext(SimpleTaskConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
TaskRepository taskRepository = this.context.getBean(TaskRepository.class);
assertNotNull("testRepository should not be null", taskRepository);
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(taskRepository);
assertEquals(targetClass, SimpleTaskRepository.class);
}
@Test(expected = BeanCreationException.class)
public void testMultipleConfigurers() {
this.context = new AnnotationConfigApplicationContext(MultipleConfigurers.class,
PropertyPlaceholderAutoConfiguration.class);
}
@Configuration
@EnableTask
public static class MultipleConfigurers {
@Bean
public TaskConfigurer taskConfigurer1() {
return new DefaultTaskConfigurer(null);
}
@Bean
public TaskConfigurer taskConfigurer2() {
return new DefaultTaskConfigurer(null);
}
}
}

View File

@@ -26,7 +26,6 @@ import org.springframework.cloud.task.repository.support.SimpleTaskExplorer;
import org.springframework.cloud.task.repository.support.SimpleTaskRepository;
import org.springframework.cloud.task.repository.support.TaskExecutionDaoFactoryBean;
import org.springframework.cloud.task.repository.support.TaskRepositoryInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
@@ -46,9 +45,6 @@ public class TestConfiguration implements InitializingBean {
@Autowired(required = false)
private ResourceLoader resourceLoader;
@Autowired
private ConfigurableApplicationContext applicationContext;
private TaskExecutionDaoFactoryBean taskExecutionDaoFactoryBean;
@Bean
@@ -83,6 +79,11 @@ public class TestConfiguration implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
this.taskExecutionDaoFactoryBean = new TaskExecutionDaoFactoryBean(this.applicationContext);
if(this.dataSource != null) {
this.taskExecutionDaoFactoryBean = new TaskExecutionDaoFactoryBean(this.dataSource);
}
else {
this.taskExecutionDaoFactoryBean = new TaskExecutionDaoFactoryBean();
}
}
}

View File

@@ -16,24 +16,19 @@
package org.springframework.cloud.task.listener;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Date;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.cloud.task.listener.annotation.AfterTask;
import org.springframework.cloud.task.listener.annotation.BeforeTask;
import org.springframework.cloud.task.listener.annotation.FailedTask;
import org.springframework.cloud.task.listener.annotation.TaskListenerExecutor;
import org.springframework.cloud.task.listener.annotation.TaskListenerExecutorFactory;
import org.springframework.cloud.task.listener.annotation.TaskListenerExecutorFactoryBean;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.util.TestDefaultConfiguration;
import org.springframework.cloud.task.util.TestListener;
@@ -43,6 +38,11 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Verifies that the TaskExecutionListener invocations occur at the appropriate task
* lifecycle stages.
@@ -203,10 +203,9 @@ public class TaskExecutionListenerTests {
}
@Bean
public TaskListenerExecutor taskListenerExecutor(ConfigurableApplicationContext context) throws Exception
public TaskListenerExecutorFactoryBean taskListenerExecutor(ConfigurableApplicationContext context) throws Exception
{
TaskListenerExecutorFactory taskListenerExecutorFactory = new TaskListenerExecutorFactory(context);
return taskListenerExecutorFactory.getObject();
return new TaskListenerExecutorFactoryBean(context);
}
public static class AnnotatedTaskListener extends TestListener {

View File

@@ -27,9 +27,6 @@ import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
import org.springframework.cloud.task.util.TaskExecutionCreator;
import org.springframework.cloud.task.util.TestVerifierUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import static org.springframework.test.util.AssertionErrors.assertTrue;
@@ -43,8 +40,7 @@ public class SimpleTaskRepositoryMapTests {
@Before
public void setUp() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(EmptyConfiguration.class);
this.taskRepository = new SimpleTaskRepository(new TaskExecutionDaoFactoryBean(context));
this.taskRepository = new SimpleTaskRepository(new TaskExecutionDaoFactoryBean());
}
@Test
@@ -52,8 +48,7 @@ public class SimpleTaskRepositoryMapTests {
TaskExecution expectedTaskExecution =
TaskExecutionCreator.createAndStoreTaskExecutionNoParams(taskRepository);
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
getSingleTaskExecutionFromMapRepository(taskRepository,
expectedTaskExecution.getExecutionId()));
getSingleTaskExecutionFromMapRepository(expectedTaskExecution.getExecutionId()));
}
@Test
@@ -61,8 +56,7 @@ public class SimpleTaskRepositoryMapTests {
TaskExecution expectedTaskExecution =
TaskExecutionCreator.createAndStoreTaskExecutionWithParams(taskRepository);
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
getSingleTaskExecutionFromMapRepository(taskRepository,
expectedTaskExecution.getExecutionId()));
getSingleTaskExecutionFromMapRepository(expectedTaskExecution.getExecutionId()));
}
@Test
@@ -75,8 +69,7 @@ public class SimpleTaskRepositoryMapTests {
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution);
}
private TaskExecution getSingleTaskExecutionFromMapRepository(
TaskRepository repository, long taskExecutionId){
private TaskExecution getSingleTaskExecutionFromMapRepository(long taskExecutionId){
Map<Long, TaskExecution> taskMap = ((MapTaskExecutionDao)
((SimpleTaskRepository)taskRepository).getTaskExecutionDao()).getTaskExecutions();
assertTrue("taskExecutionId must be in MapTaskExecutionRepository",
@@ -91,7 +84,4 @@ public class SimpleTaskRepositoryMapTests {
expectedTaskExecution.setExitCode(-1);
TaskExecutionCreator.completeExecution(taskRepository, expectedTaskExecution);
}
@Configuration
public static class EmptyConfiguration{}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 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.
@@ -16,11 +16,6 @@
package org.springframework.cloud.task.repository.support;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import javax.sql.DataSource;
import org.junit.After;
@@ -38,6 +33,11 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
/**
* Verifies that task initialization occurs properly.
*
@@ -70,7 +70,7 @@ public class TaskDatabaseInitializerTests {
@Test
public void testNoDatabase() throws Exception {
this.context = new AnnotationConfigApplicationContext(EmptyConfiguration.class);
SimpleTaskRepository repository = new SimpleTaskRepository(new TaskExecutionDaoFactoryBean(this.context));
SimpleTaskRepository repository = new SimpleTaskRepository(new TaskExecutionDaoFactoryBean());
assertThat(repository.getTaskExecutionDao(), instanceOf(MapTaskExecutionDao.class));
MapTaskExecutionDao dao = (MapTaskExecutionDao) repository.getTaskExecutionDao();
assertEquals(0, dao.getTaskExecutions().size());

View File

@@ -15,9 +15,6 @@
*/
package org.springframework.cloud.task.repository.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import javax.sql.DataSource;
import org.junit.After;
@@ -30,11 +27,13 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Michael Minella
*/
@@ -64,20 +63,6 @@ public class TaskExecutionDaoFactoryBeanTests {
new TaskExecutionDaoFactoryBean(null);
}
@Test
public void testMapTaskExecutionDaoWithAppContext() throws Exception {
this.context = new GenericApplicationContext();
this.context.refresh();
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(this.context);
TaskExecutionDao taskExecutionDao = factoryBean.getObject();
assertTrue(taskExecutionDao instanceof MapTaskExecutionDao);
TaskExecutionDao taskExecutionDao2 = factoryBean.getObject();
assertTrue(taskExecutionDao == taskExecutionDao2);
}
@Test
public void testMapTaskExecutionDaoWithoutAppContext() throws Exception {
@@ -95,7 +80,9 @@ public class TaskExecutionDaoFactoryBeanTests {
public void testDefaultDataSourceConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext(DefaultDataSourceConfiguration.class);
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(this.context);
DataSource dataSource = this.context.getBean(DataSource.class);
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(dataSource);
TaskExecutionDao taskExecutionDao = factoryBean.getObject();
assertTrue(taskExecutionDao instanceof JdbcTaskExecutionDao);
@@ -105,66 +92,14 @@ public class TaskExecutionDaoFactoryBeanTests {
assertTrue(taskExecutionDao == taskExecutionDao2);
}
@Test
public void testNonDefaultNameDataSourceConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext(AlternativeDataSourceConfiguration.class);
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(this.context);
TaskExecutionDao taskExecutionDao = factoryBean.getObject();
assertTrue(taskExecutionDao instanceof JdbcTaskExecutionDao);
TaskExecutionDao taskExecutionDao2 = factoryBean.getObject();
assertTrue(taskExecutionDao == taskExecutionDao2);
}
@Test(expected = IllegalArgumentException.class)
public void testMissingCustomDataSourceNameConfiguration() throws Exception {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(AlternativeDataSourceConfiguration.class);
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(context);
factoryBean.setDataSourceName("wrongName");
factoryBean.getObject();
}
@Test
public void testCustomDataSourceNameConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext(AlternativeDataSourceConfiguration.class);
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(this.context);
factoryBean.setDataSourceName("notDataSource");
TaskExecutionDao taskExecutionDao = factoryBean.getObject();
assertTrue(taskExecutionDao instanceof JdbcTaskExecutionDao);
TaskExecutionDao taskExecutionDao2 = factoryBean.getObject();
assertTrue(taskExecutionDao == taskExecutionDao2);
}
@Test
public void testCustomDataSourceNameConfigurationWithMultipleDataSources() throws Exception {
this.context = new AnnotationConfigApplicationContext(MultipleDataSourceConfiguration.class);
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(this.context);
factoryBean.setDataSourceName("useThisDataSource");
JdbcTaskExecutionDao taskExecutionDao = (JdbcTaskExecutionDao) factoryBean.getObject();
Object usedDataSource = ReflectionTestUtils.getField(taskExecutionDao, "dataSource");
assertTrue(usedDataSource == this.context.getBean("useThisDataSource"));
TaskExecutionDao taskExecutionDao2 = factoryBean.getObject();
assertTrue(taskExecutionDao == taskExecutionDao2);
}
@Test
public void testSettingTablePrefix() throws Exception {
this.context = new AnnotationConfigApplicationContext(DefaultDataSourceConfiguration.class);
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(this.context);
DataSource dataSource = this.context.getBean(DataSource.class);
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(dataSource);
factoryBean.setTablePrefix("foo_");
TaskExecutionDao taskExecutionDao = factoryBean.getObject();
@@ -180,35 +115,4 @@ public class TaskExecutionDaoFactoryBeanTests {
return builder.build();
}
}
@Configuration
public static class AlternativeDataSourceConfiguration {
@Bean
public DataSource notDataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2);
return builder.build();
}
}
@Configuration
public static class MultipleDataSourceConfiguration {
@Bean
public DataSource useThisDataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.setName("useThisDataSource");
return builder.build();
}
@Bean
public DataSource dontUseThisDataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.setName("dontUseThisDataSource");
return builder.build();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 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.
@@ -16,6 +16,8 @@
package org.springframework.cloud.task.util;
import javax.sql.DataSource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
@@ -35,6 +37,7 @@ import org.springframework.context.annotation.Configuration;
* Initializes the beans needed to test default task behavior.
*
* @author Glenn Renfro
* @author Michael Minella
*/
@Configuration
public class TestDefaultConfiguration implements InitializingBean {
@@ -72,6 +75,12 @@ public class TestDefaultConfiguration implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
this.factoryBean = new TaskExecutionDaoFactoryBean(this.context);
if(this.context.getBeanNamesForType(DataSource.class).length == 1){
DataSource dataSource = this.context.getBean(DataSource.class);
this.factoryBean = new TaskExecutionDaoFactoryBean(dataSource);
}
else {
this.factoryBean = new TaskExecutionDaoFactoryBean();
}
}
}