User can establish taskNameReslover via configurer vs SimpleTaskAutoConfiguration.

resolves #801
This commit is contained in:
Glenn Renfro
2023-05-15 17:59:16 -04:00
parent 1f39989831
commit dbbae91056
6 changed files with 48 additions and 9 deletions

View File

@@ -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.apache.commons.logging.LogFactory;
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 class DefaultTaskConfigurer implements TaskConfigurer {
return this.dataSource;
}
@Override
public TaskNameResolver getTaskNameResolver() {
return new SimpleTaskNameResolver();
}
@Override
public PlatformTransactionManager getTransactionManager() {
if (this.transactionManager == null) {

View File

@@ -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.boot.sql.init.dependency.DatabaseInitializationDepend
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 class SimpleTaskAutoConfiguration {
@Bean
public TaskNameResolver taskNameResolver() {
return new SimpleTaskNameResolver();
return taskNameResolver;
}
@Bean
@@ -138,6 +139,7 @@ public class SimpleTaskAutoConfiguration {
this.taskRepository = taskConfigurer.getTaskRepository();
this.platformTransactionManager = taskConfigurer.getTransactionManager();
this.taskExplorer = taskConfigurer.getTaskExplorer();
this.taskNameResolver = taskConfigurer.getTaskNameResolver();
this.initialized = true;
}

View File

@@ -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 @@ package org.springframework.cloud.task.configuration;
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 <code>TaskNameResolver</code>
*/
TaskNameResolver getTaskNameResolver();
}

View File

@@ -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 @@ import org.springframework.util.StringUtils;
/**
* Simple implementation of the {@link TaskNameResolver} interface. Names the task based
* on the following order of precidence:
* on the following order of precedence:
* <ol>
* <li>A configured property <code>spring.cloud.task.name</code></li>
* <li>The {@link ApplicationContext}'s id.</li>

View File

@@ -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.SimpleTaskAutoConfiguration;
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 class SimpleTaskAutoConfigurationTests {
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()

View File

@@ -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 class DefaultTaskConfigurerTests {
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);