Skip to content

Commit 73a91b6

Browse files
committed
TaskLifecycleListener triggers early, and possibly cyclical, bean initialisation
Replaced TaskListenerExecutorFactory with TaskListenerExecutorObjectProviderTests. This delays the need to acquire a bean till it is needed vs at application event time. resolves spring-cloud#448
1 parent 43a717e commit 73a91b6

File tree

6 files changed

+180
-230
lines changed

6 files changed

+180
-230
lines changed

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/SimpleTaskAutoConfiguration.java

-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3434
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3535
import org.springframework.cloud.task.listener.TaskLifecycleListener;
36-
import org.springframework.cloud.task.listener.annotation.TaskListenerExecutorFactoryBean;
3736
import org.springframework.cloud.task.listener.annotation.TaskListenerExecutorObjectProvider;
3837
import org.springframework.cloud.task.repository.TaskExplorer;
3938
import org.springframework.cloud.task.repository.TaskNameResolver;
@@ -81,8 +80,6 @@ public class SimpleTaskAutoConfiguration {
8180

8281
private TaskLifecycleListener taskLifecycleListener;
8382

84-
private TaskListenerExecutorFactoryBean taskListenerExecutorFactoryBean;
85-
8683
private PlatformTransactionManager platformTransactionManager;
8784

8885
private TaskExplorer taskExplorer;
@@ -97,12 +94,6 @@ public TaskLifecycleListener taskLifecycleListener() {
9794
return this.taskLifecycleListener;
9895
}
9996

100-
@Bean
101-
public TaskListenerExecutorFactoryBean taskListenerExecutor()
102-
throws Exception {
103-
return this.taskListenerExecutorFactoryBean;
104-
}
105-
10697
@Bean
10798
@ConditionalOnMissingBean
10899
public PlatformTransactionManager transactionManager() {
@@ -145,7 +136,6 @@ protected void initialize() throws Exception {
145136
taskConfigurer.getClass().getName()));
146137

147138
this.taskRepository = taskConfigurer.getTaskRepository();
148-
this.taskListenerExecutorFactoryBean = new TaskListenerExecutorFactoryBean(context);
149139
this.platformTransactionManager = taskConfigurer.getTransactionManager();
150140
this.taskExplorer = taskConfigurer.getTaskExplorer();
151141

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/TaskLifecycleListener.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
7575
private ConfigurableApplicationContext context;
7676

7777
@Autowired(required = false)
78-
private Collection<TaskExecutionListener> taskExecutionListeners;
78+
private Collection<TaskExecutionListener> taskExecutionListenersFromContext;
79+
80+
private List<TaskExecutionListener> taskExecutionListeners;
7981

8082
private boolean isTaskExecutionListenersInitialized;
8183

@@ -231,6 +233,7 @@ else if (this.listenerFailed || this.applicationFailedEvent != null) {
231233
private void doTaskStart() {
232234

233235
if(!this.started) {
236+
getTaskExecutionListeners();
234237
List<String> args = new ArrayList<>(0);
235238

236239
if(this.applicationArguments != null) {
@@ -266,11 +269,11 @@ private void doTaskStart() {
266269

267270
private TaskExecution invokeOnTaskStartup(TaskExecution taskExecution){
268271
TaskExecution listenerTaskExecution = getTaskExecutionCopy(taskExecution);
269-
List<TaskExecutionListener> starterList = getTaskExecutionListeners();
270-
if (starterList != null) {
272+
List<TaskExecutionListener> startupListenerList = new ArrayList<>(this.taskExecutionListeners);
273+
if (startupListenerList != null) {
271274
try {
272-
Collections.reverse(starterList);
273-
for (TaskExecutionListener taskExecutionListener : starterList) {
275+
Collections.reverse(startupListenerList);
276+
for (TaskExecutionListener taskExecutionListener : startupListenerList) {
274277
taskExecutionListener.onTaskStartup(listenerTaskExecution);
275278
}
276279
}
@@ -287,10 +290,9 @@ private TaskExecution invokeOnTaskStartup(TaskExecution taskExecution){
287290

288291
private TaskExecution invokeOnTaskEnd(TaskExecution taskExecution){
289292
TaskExecution listenerTaskExecution = getTaskExecutionCopy(taskExecution);
290-
List<TaskExecutionListener> enderList = getTaskExecutionListeners();
291-
if (enderList != null) {
293+
if (this.taskExecutionListeners != null) {
292294
try {
293-
for (TaskExecutionListener taskExecutionListener : enderList) {
295+
for (TaskExecutionListener taskExecutionListener : this.taskExecutionListeners) {
294296
taskExecutionListener.onTaskEnd(listenerTaskExecution);
295297
}
296298
}
@@ -309,10 +311,9 @@ private TaskExecution invokeOnTaskEnd(TaskExecution taskExecution){
309311

310312
private TaskExecution invokeOnTaskError(TaskExecution taskExecution, Throwable throwable){
311313
TaskExecution listenerTaskExecution = getTaskExecutionCopy(taskExecution);
312-
List<TaskExecutionListener> errorListenerList = getTaskExecutionListeners();
313-
if (errorListenerList != null) {
314+
if (this.taskExecutionListeners != null) {
314315
try {
315-
for (TaskExecutionListener taskExecutionListener : errorListenerList) {
316+
for (TaskExecutionListener taskExecutionListener : this.taskExecutionListeners) {
316317
taskExecutionListener.onTaskFailed(listenerTaskExecution, throwable);
317318
}
318319
}
@@ -385,13 +386,12 @@ public void destroy() throws Exception {
385386
this.doTaskEnd();
386387
}
387388

388-
private List<TaskExecutionListener> getTaskExecutionListeners() {
389-
ArrayList<TaskExecutionListener> result = new ArrayList<>();
389+
private void getTaskExecutionListeners() {
390+
this.taskExecutionListeners = new ArrayList<>();
390391
this.taskListenerExecutorObjectProvider.getObject();
391-
if(this.taskExecutionListeners != null) {
392-
result.addAll(this.taskExecutionListeners);
392+
if(this.taskExecutionListenersFromContext != null) {
393+
this.taskExecutionListeners.addAll(this.taskExecutionListenersFromContext);
393394
}
394-
result.add(this.taskListenerExecutorObjectProvider.getObject());
395-
return result;
395+
this.taskExecutionListeners.add(this.taskListenerExecutorObjectProvider.getObject());
396396
}
397397
}

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/annotation/TaskListenerExecutorFactoryBean.java

-165
This file was deleted.

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/annotation/TaskListenerExecutorObjectProvider.java

+6-13
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@
4040
import org.springframework.core.annotation.AnnotationUtils;
4141

4242
/**
43+
* Initializes TaskListenerExecutor for a task.
44+
*
4345
* @author Glenn Renfro
46+
* @since 2.1.0
4447
*/
4548
public class TaskListenerExecutorObjectProvider implements ObjectProvider<TaskExecutionListener> {
4649

@@ -70,16 +73,6 @@ public TaskListenerExecutor getObject() {
7073
return new TaskListenerExecutor(beforeTaskInstances, afterTaskInstances, failedTaskInstances);
7174
}
7275

73-
// @Override
74-
// public Class<?> getObjectType() {
75-
// return TaskListenerExecutor.class;
76-
// }
77-
//
78-
// @Override
79-
// public boolean isSingleton() {
80-
// return false;
81-
// }
82-
8376
private void initializeExecutor( ) {
8477
ConfigurableListableBeanFactory factory = context.getBeanFactory();
8578
for( String beanName : context.getBeanDefinitionNames()) {
@@ -154,17 +147,17 @@ private void processBean(String beanName, final Class<?> type){
154147

155148
@Override
156149
public TaskExecutionListener getObject(Object... args) throws BeansException {
157-
return null;
150+
throw new UnsupportedOperationException("the getObject(Object... args) method is not supported.");
158151
}
159152

160153
@Override
161154
public TaskExecutionListener getIfAvailable() throws BeansException {
162-
return null;
155+
throw new UnsupportedOperationException("the getIfAvailable() method is not supported.");
163156
}
164157

165158
@Override
166159
public TaskExecutionListener getIfUnique() throws BeansException {
167-
return null;
160+
throw new UnsupportedOperationException("the getIfUnique() method is not supported.");
168161
}
169162

170163
private static class MethodGetter<T extends Annotation> {

spring-cloud-task-core/src/test/java/org/springframework/cloud/task/listener/TaskExecutionListenerTests.java

-25
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@
2929
import org.springframework.cloud.task.listener.annotation.AfterTask;
3030
import org.springframework.cloud.task.listener.annotation.BeforeTask;
3131
import org.springframework.cloud.task.listener.annotation.FailedTask;
32-
import org.springframework.cloud.task.listener.annotation.TaskListenerExecutorFactoryBean;
3332
import org.springframework.cloud.task.repository.TaskExecution;
3433
import org.springframework.cloud.task.util.TestDefaultConfiguration;
3534
import org.springframework.cloud.task.util.TestListener;
36-
import org.springframework.context.ConfigurableApplicationContext;
3735
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3836
import org.springframework.context.annotation.Bean;
3937
import org.springframework.context.annotation.Configuration;
@@ -290,12 +288,6 @@ public AnnotatedTaskListener annotatedTaskListener() {
290288
return new AnnotatedTaskListener();
291289
}
292290

293-
@Bean
294-
public TaskListenerExecutorFactoryBean taskListenerExecutor(ConfigurableApplicationContext context) throws Exception
295-
{
296-
return new TaskListenerExecutorFactoryBean(context);
297-
}
298-
299291
public static class AnnotatedTaskListener extends TestListener {
300292

301293
@BeforeTask
@@ -331,12 +323,6 @@ public AnnotatedTaskListener annotatedTaskListener() {
331323
return new AnnotatedTaskListener();
332324
}
333325

334-
@Bean
335-
public TaskListenerExecutorFactoryBean taskListenerExecutor(ConfigurableApplicationContext context) throws Exception
336-
{
337-
return new TaskListenerExecutorFactoryBean(context);
338-
}
339-
340326
public static class AnnotatedTaskListener {
341327

342328
@BeforeTask
@@ -365,11 +351,6 @@ public AnnotatedTaskListener annotatedTaskListener() {
365351
return new AnnotatedTaskListener();
366352
}
367353

368-
@Bean
369-
public TaskListenerExecutorFactoryBean taskListenerExecutor(ConfigurableApplicationContext context) throws Exception
370-
{
371-
return new TaskListenerExecutorFactoryBean(context);
372-
}
373354

374355
public static class AnnotatedTaskListener {
375356

@@ -400,12 +381,6 @@ public AnnotatedTaskListener annotatedTaskListener() {
400381
return new AnnotatedTaskListener();
401382
}
402383

403-
@Bean
404-
public TaskListenerExecutorFactoryBean taskListenerExecutor(ConfigurableApplicationContext context) throws Exception
405-
{
406-
return new TaskListenerExecutorFactoryBean(context);
407-
}
408-
409384
public static class AnnotatedTaskListener extends TestListener{
410385

411386
@BeforeTask

0 commit comments

Comments
 (0)