From dbbae91056d5898dc512bd96d8354ee3dc958623 Mon Sep 17 00:00:00 2001 From: Glenn Renfro Date: Mon, 15 May 2023 17:59:16 -0400 Subject: [PATCH] User can establish taskNameReslover via configurer vs SimpleTaskAutoConfiguration. resolves #801 --- .../configuration/DefaultTaskConfigurer.java | 9 ++++++++- .../SimpleTaskAutoConfiguration.java | 8 +++++--- .../task/configuration/TaskConfigurer.java | 9 ++++++++- .../support/SimpleTaskNameResolver.java | 4 ++-- .../task/SimpleTaskAutoConfigurationTests.java | 17 ++++++++++++++++- .../DefaultTaskConfigurerTests.java | 10 +++++++++- 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/DefaultTaskConfigurer.java b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/DefaultTaskConfigurer.java index a0fb7c783..9496eedbc 100644 --- a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/DefaultTaskConfigurer.java +++ b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/DefaultTaskConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2022 the original author or authors. + * Copyright 2015-2023 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. @@ -24,10 +24,12 @@ import org.springframework.batch.support.transaction.ResourcelessTransactionManager; import org.springframework.cloud.task.repository.TaskExplorer; +import org.springframework.cloud.task.repository.TaskNameResolver; import org.springframework.cloud.task.repository.TaskRepository; import org.springframework.cloud.task.repository.dao.JdbcTaskExecutionDao; import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao; import org.springframework.cloud.task.repository.support.SimpleTaskExplorer; +import org.springframework.cloud.task.repository.support.SimpleTaskNameResolver; import org.springframework.cloud.task.repository.support.SimpleTaskRepository; import org.springframework.cloud.task.repository.support.TaskExecutionDaoFactoryBean; import org.springframework.context.ApplicationContext; @@ -129,6 +131,11 @@ public DataSource getTaskDataSource() { return this.dataSource; } + @Override + public TaskNameResolver getTaskNameResolver() { + return new SimpleTaskNameResolver(); + } + @Override public PlatformTransactionManager getTransactionManager() { if (this.transactionManager == null) { diff --git a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/SimpleTaskAutoConfiguration.java b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/SimpleTaskAutoConfiguration.java index 14d5b3165..668701b26 100644 --- a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/SimpleTaskAutoConfiguration.java +++ b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/SimpleTaskAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2022 the original author or authors. + * Copyright 2015-2023 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. @@ -34,7 +34,6 @@ import org.springframework.cloud.task.repository.TaskExplorer; import org.springframework.cloud.task.repository.TaskNameResolver; import org.springframework.cloud.task.repository.TaskRepository; -import org.springframework.cloud.task.repository.support.SimpleTaskNameResolver; import org.springframework.cloud.task.repository.support.SimpleTaskRepository; import org.springframework.cloud.task.repository.support.TaskRepositoryInitializer; import org.springframework.context.ConfigurableApplicationContext; @@ -84,6 +83,8 @@ public class SimpleTaskAutoConfiguration { private TaskExplorer taskExplorer; + private TaskNameResolver taskNameResolver; + @Bean public SimpleTaskRepository taskRepository() { return (SimpleTaskRepository) this.taskRepository; @@ -102,7 +103,7 @@ public TaskExplorer taskExplorer() { @Bean public TaskNameResolver taskNameResolver() { - return new SimpleTaskNameResolver(); + return taskNameResolver; } @Bean @@ -138,6 +139,7 @@ protected void initialize() { this.taskRepository = taskConfigurer.getTaskRepository(); this.platformTransactionManager = taskConfigurer.getTransactionManager(); this.taskExplorer = taskConfigurer.getTaskExplorer(); + this.taskNameResolver = taskConfigurer.getTaskNameResolver(); this.initialized = true; } diff --git a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/TaskConfigurer.java b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/TaskConfigurer.java index 51d781bfa..55b366818 100644 --- a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/TaskConfigurer.java +++ b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/TaskConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2019 the original author or authors. + * Copyright 2015-2023 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. @@ -19,6 +19,7 @@ import javax.sql.DataSource; import org.springframework.cloud.task.repository.TaskExplorer; +import org.springframework.cloud.task.repository.TaskNameResolver; import org.springframework.cloud.task.repository.TaskRepository; import org.springframework.transaction.PlatformTransactionManager; @@ -58,4 +59,10 @@ public interface TaskConfigurer { */ DataSource getTaskDataSource(); + /** + * Create a {@link TaskNameResolver} for use with the task application. + * @return A TaskNameResolver + */ + TaskNameResolver getTaskNameResolver(); + } diff --git a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/support/SimpleTaskNameResolver.java b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/support/SimpleTaskNameResolver.java index 40d549559..da8b54629 100644 --- a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/support/SimpleTaskNameResolver.java +++ b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/support/SimpleTaskNameResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2019 the original author or authors. + * Copyright 2015-2023 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,7 +25,7 @@ /** * Simple implementation of the {@link TaskNameResolver} interface. Names the task based - * on the following order of precidence: + * on the following order of precedence: *
    *
  1. A configured property spring.cloud.task.name
  2. *
  3. The {@link ApplicationContext}'s id.
  4. diff --git a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/SimpleTaskAutoConfigurationTests.java b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/SimpleTaskAutoConfigurationTests.java index 046ae9350..29019caf8 100644 --- a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/SimpleTaskAutoConfigurationTests.java +++ b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/SimpleTaskAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2022 the original author or authors. + * Copyright 2015-2023 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. @@ -40,6 +40,7 @@ import org.springframework.cloud.task.configuration.SingleTaskConfiguration; import org.springframework.cloud.task.configuration.TaskConfigurer; import org.springframework.cloud.task.repository.TaskExplorer; +import org.springframework.cloud.task.repository.TaskNameResolver; import org.springframework.cloud.task.repository.TaskRepository; import org.springframework.cloud.task.repository.support.SimpleTaskRepository; import org.springframework.context.ApplicationContextException; @@ -131,6 +132,20 @@ public void testRepositoryNotInitialized() { verifyExceptionThrownDefaultExecutable(ApplicationContextException.class, applicationContextRunner); } + @Test + public void testTaskNameResolver() { + ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(EmbeddedDataSourceConfiguration.class, + PropertyPlaceholderAutoConfiguration.class, SimpleTaskAutoConfiguration.class, + SingleTaskConfiguration.class)) + .withUserConfiguration(TaskLifecycleListenerConfiguration.class) + .withPropertyValues("spring.cloud.task.name=myTestName"); + applicationContextRunner.run((context) -> { + TaskNameResolver taskNameResolver = context.getBean(TaskNameResolver.class); + assertThat(taskNameResolver.getTaskName()).isEqualTo("myTestName"); + }); + } + @Test public void testMultipleConfigurers() { ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner() diff --git a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/configuration/DefaultTaskConfigurerTests.java b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/configuration/DefaultTaskConfigurerTests.java index f1a0fa09b..64b28a2a1 100644 --- a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/configuration/DefaultTaskConfigurerTests.java +++ b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/configuration/DefaultTaskConfigurerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2022 the original author or authors. + * Copyright 2017-2023 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. @@ -90,6 +90,14 @@ public void taskExplorerTest() { assertThat(defaultTaskConfigurer.getTaskExplorer()).isNotNull(); } + @Test + public void taskNameResolverTest() { + DefaultTaskConfigurer defaultTaskConfigurer = new DefaultTaskConfigurer(this.dataSource); + assertThat(defaultTaskConfigurer.getTaskNameResolver()).isNotNull(); + defaultTaskConfigurer = new DefaultTaskConfigurer(); + assertThat(defaultTaskConfigurer.getTaskNameResolver()).isNotNull(); + } + @Test public void taskRepositoryTest() { DefaultTaskConfigurer defaultTaskConfigurer = new DefaultTaskConfigurer(this.dataSource);