Established @EnableTask to create instance of TaskLifecycle Bean

resolves #494
This commit is contained in:
Glenn Renfro
2018-12-10 14:41:27 -05:00
parent b4b50e8182
commit 30399dbb57
20 changed files with 151 additions and 33 deletions

View File

@@ -24,10 +24,12 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.context.annotation.Import;
/**
* <p>
* Enable Spring Task features.
* Enables the {@link org.springframework.cloud.task.listener.TaskLifecycleListener}
* so that the features of Spring Cloud Task will be applied.
*
* <pre class="code">
* &#064;Configuration
@@ -43,23 +45,15 @@ import org.springframework.cloud.task.repository.TaskRepository;
*
* Note that only one of your configuration classes needs to have the <code>&#064;EnableTask</code>
* annotation. Once you have an <code>&#064;EnableTask</code> class in your configuration
* you will have an instance of {@link TaskConfigurer}. If one is not specified then the
* {@link DefaultTaskConfigurer} will be used.
* You will also be able to <code>&#064;Autowired</code> some useful stuff into your context:
*
* <ul>
* <li>a {@link TaskRepository} (bean name "taskRepository").
* </ul>
* the task will have the Spring Cloud Task features available.
*
* @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(TaskLifecycleConfiguration.class)
public @interface EnableTask {
}

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.task.configuration;
import java.util.Arrays;
import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
@@ -30,8 +31,6 @@ 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.TaskListenerExecutorObjectFactory;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.cloud.task.repository.TaskNameResolver;
import org.springframework.cloud.task.repository.TaskRepository;
@@ -76,8 +75,6 @@ public class SimpleTaskAutoConfiguration {
private TaskRepository taskRepository;
private TaskLifecycleListener taskLifecycleListener;
private PlatformTransactionManager platformTransactionManager;
private TaskExplorer taskExplorer;
@@ -87,11 +84,6 @@ public class SimpleTaskAutoConfiguration {
return this.taskRepository;
}
@Bean
public TaskLifecycleListener taskLifecycleListener() {
return this.taskLifecycleListener;
}
@Bean
@ConditionalOnMissingBean
public PlatformTransactionManager transactionManager() {
@@ -136,10 +128,6 @@ public class SimpleTaskAutoConfiguration {
this.taskRepository = taskConfigurer.getTaskRepository();
this.platformTransactionManager = taskConfigurer.getTransactionManager();
this.taskExplorer = taskConfigurer.getTaskExplorer();
this.taskLifecycleListener = new TaskLifecycleListener(this.taskRepository, taskNameResolver(),
this.applicationArguments, taskExplorer, taskProperties, new TaskListenerExecutorObjectFactory(context));
initialized = true;
}

View File

@@ -18,7 +18,7 @@ package org.springframework.cloud.task.configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.task.repository.support.SimpleTaskNameResolver;
import org.springframework.cloud.task.repository.TaskNameResolver;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -41,9 +41,6 @@ public class SingleTaskConfiguration {
@Autowired
private TaskProperties taskProperties;
@Autowired
private SimpleTaskNameResolver taskNameResolver;
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
@@ -52,14 +49,14 @@ public class SingleTaskConfiguration {
@Bean
public SingleInstanceTaskListener taskListener() {
public SingleInstanceTaskListener taskListener(TaskNameResolver resolver) {
if (taskConfigurer.getTaskDataSource() == null) {
return new SingleInstanceTaskListener(new PassThruLockRegistry(),
this.taskNameResolver, this.taskProperties, this.applicationEventPublisher);
resolver, this.taskProperties, this.applicationEventPublisher);
}
return new SingleInstanceTaskListener(taskConfigurer.getTaskDataSource(),
this.taskNameResolver,
resolver,
this.taskProperties,
this.applicationEventPublisher);
}

View File

@@ -0,0 +1,88 @@
/*
* 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
*
* 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.
*/
package org.springframework.cloud.task.configuration;
import javax.annotation.PostConstruct;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.cloud.task.listener.TaskLifecycleListener;
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;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Configuration for a {@link TaskLifecycleListener}.
*
* @author Glenn Renfro
* @since 2.1
*/
@Configuration
public class TaskLifecycleConfiguration {
protected static final Log logger = LogFactory.getLog(TaskLifecycleConfiguration.class);
@Autowired
private TaskProperties taskProperties;
@Autowired
private ConfigurableApplicationContext context;
@Autowired(required = false)
private ApplicationArguments applicationArguments;
@Autowired
private TaskRepository taskRepository;
@Autowired
private TaskExplorer taskExplorer;
@Autowired
private TaskNameResolver taskNameResolver;
private TaskLifecycleListener taskLifecycleListener;
private boolean initialized = false;
@Bean
public TaskLifecycleListener taskLifecycleListener() {
return this.taskLifecycleListener;
}
/**
* Initializes the {@link TaskLifecycleListener} for the task app.
*/
@PostConstruct
protected void initialize() {
if (this.initialized) {
return;
}
this.taskLifecycleListener = new TaskLifecycleListener(this.taskRepository, this.taskNameResolver,
this.applicationArguments, this.taskExplorer, this.taskProperties, new TaskListenerExecutorObjectFactory(context));
this.initialized = true;
}
}

View File

@@ -35,6 +35,7 @@ import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoCon
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.task.configuration.DefaultTaskConfigurer;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration;
import org.springframework.cloud.task.configuration.SingleTaskConfiguration;
import org.springframework.cloud.task.configuration.TaskConfigurer;
@@ -107,7 +108,8 @@ public class SimpleTaskAutoConfigurationTests {
.withConfiguration(AutoConfigurations.of(EmbeddedDataSourceConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
SimpleTaskAutoConfiguration.class,
SingleTaskConfiguration.class));
SingleTaskConfiguration.class))
.withUserConfiguration(TaskLifecycleListenerConfiguration.class);
applicationContextRunner.run((context) -> {
TaskExplorer taskExplorer = context.getBean(TaskExplorer.class);
assertThat(taskExplorer.getTaskExecutionCount()).isEqualTo(1l);
@@ -121,6 +123,7 @@ public class SimpleTaskAutoConfigurationTests {
PropertyPlaceholderAutoConfiguration.class,
SimpleTaskAutoConfiguration.class,
SingleTaskConfiguration.class))
.withUserConfiguration(TaskLifecycleListenerConfiguration.class)
.withPropertyValues("spring.cloud.task.tablePrefix=foobarless");
verifyExceptionThrownDefaultExecutable(ApplicationContextException.class, "Failed to start " +
@@ -248,4 +251,10 @@ public class SimpleTaskAutoConfigurationTests {
}
@EnableTask
@Configuration
public static class TaskLifecycleListenerConfiguration {
}
}

View File

@@ -145,6 +145,7 @@ public class TaskCoreTests {
output.contains(EXCEPTION_INVALID_TASK_EXECUTION_ID));
}
@EnableTask
@ImportAutoConfiguration({SimpleTaskAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class})
public static class TaskConfiguration {
@@ -172,6 +173,7 @@ public class TaskCoreTests {
}
}
@EnableTask
@ImportAutoConfiguration({SimpleTaskAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class})
public static class TaskExceptionConfiguration {