Update Task to BOOT 2.1.M1

Migrating to use ApplicationContextRunner or ImportAutoConfiguration with SpringApp.run, instead of SpringApplicationBuilder, because builder does not handle AutoConfiguration properly

SimpleTaskAutoConfiguration now has an annotation AutoConfigureBefore the BatchTaskAutoConfig so that it is processed prior.  THis is so that that BatchTaskAutoConfig can create the appropriate beans

SimpleTaskAutoConfiguration has new annotations so that it is AutoConfigured after BindingServiceConfiguration and after SimpleTaskAutoConfiguration.  This is so that it does not attempt to start emitting messages before stream is ready and it can create the appropriate beans after SimpleTaskAutoConfiguration has run.

Renamed SimpleTaskConfiguration to SimpleTaskAutoConfiguration.

Task version updated to  2.1.0

Added missing headers

Updated documentation.

Deprecated EnableTask

Added ability to disable Task autoconfiguration.

Removed @EnableTask from tests

Resolves #439
Resolves #440
Resolves #448
Resolves #466
This commit is contained in:
Glenn Renfro
2018-08-09 17:54:56 -04:00
committed by Michael Minella
parent d2c90c5256
commit d2bc2530cc
70 changed files with 1381 additions and 801 deletions

View File

@@ -1,6 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-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.
@@ -54,11 +53,15 @@ import org.springframework.context.annotation.Import;
* </ul>
*
* @author Glenn Renfro
*
* @deprecated The EnableTask annotation is no longer be required to initialize
* Spring Cloud Task. This will be handled by AutoConfiguration provided by Spring Cloud Task.
*/
@Deprecated
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({ SimpleTaskConfiguration.class, SingleTaskConfiguration.class })
@Import({ })
public @interface EnableTask {
}

View File

@@ -29,9 +29,11 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.task.listener.TaskLifecycleListener;
import org.springframework.cloud.task.listener.annotation.TaskListenerExecutorFactoryBean;
import org.springframework.cloud.task.listener.TaskListenerExecutorObjectFactory;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.cloud.task.repository.TaskNameResolver;
import org.springframework.cloud.task.repository.TaskRepository;
@@ -55,9 +57,10 @@ import org.springframework.util.CollectionUtils;
@Configuration
@EnableTransactionManagement
@EnableConfigurationProperties(TaskProperties.class)
public class SimpleTaskConfiguration {
@ConditionalOnProperty(prefix = "spring.cloud.task.autoconfiguration", name = "enabled", havingValue = "true", matchIfMissing = true)
public class SimpleTaskAutoConfiguration {
protected static final Log logger = LogFactory.getLog(SimpleTaskConfiguration.class);
protected static final Log logger = LogFactory.getLog(SimpleTaskAutoConfiguration.class);
@Autowired(required = false)
private Collection<DataSource> dataSources;
@@ -77,8 +80,6 @@ public class SimpleTaskConfiguration {
private TaskLifecycleListener taskLifecycleListener;
private TaskListenerExecutorFactoryBean taskListenerExecutorFactoryBean;
private PlatformTransactionManager platformTransactionManager;
private TaskExplorer taskExplorer;
@@ -94,12 +95,7 @@ public class SimpleTaskConfiguration {
}
@Bean
public TaskListenerExecutorFactoryBean taskListenerExecutor()
throws Exception {
return this.taskListenerExecutorFactoryBean;
}
@Bean
@ConditionalOnMissingBean
public PlatformTransactionManager transactionManager() {
return this.platformTransactionManager;
}
@@ -140,12 +136,11 @@ public class SimpleTaskConfiguration {
taskConfigurer.getClass().getName()));
this.taskRepository = taskConfigurer.getTaskRepository();
this.taskListenerExecutorFactoryBean = new TaskListenerExecutorFactoryBean(context);
this.platformTransactionManager = taskConfigurer.getTransactionManager();
this.taskExplorer = taskConfigurer.getTaskExplorer();
this.taskLifecycleListener = new TaskLifecycleListener(this.taskRepository, taskNameResolver(),
this.applicationArguments, taskExplorer, taskProperties);
this.applicationArguments, taskExplorer, taskProperties, new TaskListenerExecutorObjectFactory(context));
initialized = true;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-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.
@@ -46,6 +46,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
/**
@@ -74,7 +75,9 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
private ConfigurableApplicationContext context;
@Autowired(required = false)
private Collection<TaskExecutionListener> taskExecutionListeners;
private Collection<TaskExecutionListener> taskExecutionListenersFromContext;
private List<TaskExecutionListener> taskExecutionListeners;
private final static Log logger = LogFactory.getLog(TaskLifecycleListener.class);
@@ -82,6 +85,8 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
private final TaskExplorer taskExplorer;
private final TaskListenerExecutorObjectFactory taskListenerExecutorObjectFactory;
private TaskExecution taskExecution;
private TaskProperties taskProperties;
@@ -108,17 +113,20 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
public TaskLifecycleListener(TaskRepository taskRepository,
TaskNameResolver taskNameResolver,
ApplicationArguments applicationArguments, TaskExplorer taskExplorer,
TaskProperties taskProperties) {
TaskProperties taskProperties,
TaskListenerExecutorObjectFactory taskListenerExecutorObjectFactory) {
Assert.notNull(taskRepository, "A taskRepository is required");
Assert.notNull(taskNameResolver, "A taskNameResolver is required");
Assert.notNull(taskExplorer, "A taskExplorer is required");
Assert.notNull(taskProperties, "TaskProperties is required");
Assert.notNull(taskListenerExecutorObjectFactory, "A TaskListenerExecutorObjectFactory is required");
this.taskRepository = taskRepository;
this.taskNameResolver = taskNameResolver;
this.applicationArguments = applicationArguments;
this.taskExplorer = taskExplorer;
this.taskProperties = taskProperties;
this.taskListenerExecutorObjectFactory = taskListenerExecutorObjectFactory;
}
/**
@@ -223,6 +231,13 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
private void doTaskStart() {
if(!this.started) {
this.taskExecutionListeners = new ArrayList<>();
this.taskListenerExecutorObjectFactory.getObject();
if(!CollectionUtils.isEmpty(this.taskExecutionListenersFromContext)) {
this.taskExecutionListeners.addAll(this.taskExecutionListenersFromContext);
}
this.taskExecutionListeners.add(this.taskListenerExecutorObjectFactory.getObject());
List<String> args = new ArrayList<>(0);
if(this.applicationArguments != null) {
@@ -258,11 +273,11 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
private TaskExecution invokeOnTaskStartup(TaskExecution taskExecution){
TaskExecution listenerTaskExecution = getTaskExecutionCopy(taskExecution);
if (this.taskExecutionListeners != null) {
List<TaskExecutionListener> startupListenerList = new ArrayList<>(this.taskExecutionListeners);
if (startupListenerList != null) {
try {
List<TaskExecutionListener> starterList = new ArrayList<>(taskExecutionListeners);
Collections.reverse(starterList);
for (TaskExecutionListener taskExecutionListener : starterList) {
Collections.reverse(startupListenerList);
for (TaskExecutionListener taskExecutionListener : startupListenerList) {
taskExecutionListener.onTaskStartup(listenerTaskExecution);
}
}
@@ -300,7 +315,7 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
private TaskExecution invokeOnTaskError(TaskExecution taskExecution, Throwable throwable){
TaskExecution listenerTaskExecution = getTaskExecutionCopy(taskExecution);
if (taskExecutionListeners != null) {
if (this.taskExecutionListeners != null) {
try {
for (TaskExecutionListener taskExecutionListener : this.taskExecutionListeners) {
taskExecutionListener.onTaskFailed(listenerTaskExecution, throwable);
@@ -346,7 +361,7 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
@Override
public void stop(Runnable callback) {
Assert.notNull(callback, "A callback is required");
stop();
callback.run();
}
@@ -358,6 +373,7 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
@Override
public void stop() {
this.doTaskEnd();
}
@Override
@@ -374,4 +390,5 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
public void destroy() throws Exception {
this.doTaskEnd();
}
}

View File

@@ -1,20 +1,20 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.listener.annotation;
package org.springframework.cloud.task.listener;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@@ -30,18 +30,27 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.autoproxy.AutoProxyUtils;
import org.springframework.aop.scope.ScopedObject;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.cloud.task.listener.TaskExecutionListener;
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.context.ConfigurableApplicationContext;
import org.springframework.core.MethodIntrospector;
import org.springframework.core.annotation.AnnotationUtils;
/**
* Initializes TaskListenerExecutor for a task.
*
* @author Glenn Renfro
* @since 2.1.0
*/
public class TaskListenerExecutorFactoryBean implements FactoryBean<TaskExecutionListener> {
public class TaskListenerExecutorObjectFactory implements ObjectFactory<TaskExecutionListener> {
private final static Log logger = LogFactory.getLog(TaskListenerExecutor.class);
@@ -56,29 +65,19 @@ public class TaskListenerExecutorFactoryBean implements FactoryBean<TaskExecutio
private Map<Method, Object> failedTaskInstances;
public TaskListenerExecutorFactoryBean(ConfigurableApplicationContext context){
public TaskListenerExecutorObjectFactory(ConfigurableApplicationContext context){
this.context = context;
}
@Override
public TaskListenerExecutor getObject() throws Exception {
beforeTaskInstances = new HashMap<>();
afterTaskInstances = new HashMap<>();
failedTaskInstances = new HashMap<>();
public TaskListenerExecutor getObject() {
this.beforeTaskInstances = new HashMap<>();
this.afterTaskInstances = new HashMap<>();
this.failedTaskInstances = new HashMap<>();
initializeExecutor();
return new TaskListenerExecutor(beforeTaskInstances, afterTaskInstances, failedTaskInstances);
}
@Override
public Class<?> getObjectType() {
return TaskListenerExecutor.class;
}
@Override
public boolean isSingleton() {
return false;
}
private void initializeExecutor( ) {
ConfigurableListableBeanFactory factory = context.getBeanFactory();
for( String beanName : context.getBeanDefinitionNames()) {

View File

@@ -1 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.cloud.task.configuration.ResourceLoadingAutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.cloud.task.configuration.SingleTaskConfiguration,\
org.springframework.cloud.task.configuration.ResourceLoadingAutoConfiguration,\
org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration\