Skip to content

Commit dbbae91

Browse files
committed
User can establish taskNameReslover via configurer vs SimpleTaskAutoConfiguration.
resolves #801
1 parent 1f39989 commit dbbae91

6 files changed

Lines changed: 48 additions & 9 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,10 +24,12 @@
2424

2525
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
2626
import org.springframework.cloud.task.repository.TaskExplorer;
27+
import org.springframework.cloud.task.repository.TaskNameResolver;
2728
import org.springframework.cloud.task.repository.TaskRepository;
2829
import org.springframework.cloud.task.repository.dao.JdbcTaskExecutionDao;
2930
import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
3031
import org.springframework.cloud.task.repository.support.SimpleTaskExplorer;
32+
import org.springframework.cloud.task.repository.support.SimpleTaskNameResolver;
3133
import org.springframework.cloud.task.repository.support.SimpleTaskRepository;
3234
import org.springframework.cloud.task.repository.support.TaskExecutionDaoFactoryBean;
3335
import org.springframework.context.ApplicationContext;
@@ -129,6 +131,11 @@ public DataSource getTaskDataSource() {
129131
return this.dataSource;
130132
}
131133

134+
@Override
135+
public TaskNameResolver getTaskNameResolver() {
136+
return new SimpleTaskNameResolver();
137+
}
138+
132139
@Override
133140
public PlatformTransactionManager getTransactionManager() {
134141
if (this.transactionManager == null) {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,7 +34,6 @@
3434
import org.springframework.cloud.task.repository.TaskExplorer;
3535
import org.springframework.cloud.task.repository.TaskNameResolver;
3636
import org.springframework.cloud.task.repository.TaskRepository;
37-
import org.springframework.cloud.task.repository.support.SimpleTaskNameResolver;
3837
import org.springframework.cloud.task.repository.support.SimpleTaskRepository;
3938
import org.springframework.cloud.task.repository.support.TaskRepositoryInitializer;
4039
import org.springframework.context.ConfigurableApplicationContext;
@@ -84,6 +83,8 @@ public class SimpleTaskAutoConfiguration {
8483

8584
private TaskExplorer taskExplorer;
8685

86+
private TaskNameResolver taskNameResolver;
87+
8788
@Bean
8889
public SimpleTaskRepository taskRepository() {
8990
return (SimpleTaskRepository) this.taskRepository;
@@ -102,7 +103,7 @@ public TaskExplorer taskExplorer() {
102103

103104
@Bean
104105
public TaskNameResolver taskNameResolver() {
105-
return new SimpleTaskNameResolver();
106+
return taskNameResolver;
106107
}
107108

108109
@Bean
@@ -138,6 +139,7 @@ protected void initialize() {
138139
this.taskRepository = taskConfigurer.getTaskRepository();
139140
this.platformTransactionManager = taskConfigurer.getTransactionManager();
140141
this.taskExplorer = taskConfigurer.getTaskExplorer();
142+
this.taskNameResolver = taskConfigurer.getTaskNameResolver();
141143
this.initialized = true;
142144
}
143145

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import javax.sql.DataSource;
2020

2121
import org.springframework.cloud.task.repository.TaskExplorer;
22+
import org.springframework.cloud.task.repository.TaskNameResolver;
2223
import org.springframework.cloud.task.repository.TaskRepository;
2324
import org.springframework.transaction.PlatformTransactionManager;
2425

@@ -58,4 +59,10 @@ public interface TaskConfigurer {
5859
*/
5960
DataSource getTaskDataSource();
6061

62+
/**
63+
* Create a {@link TaskNameResolver} for use with the task application.
64+
* @return A <code>TaskNameResolver</code>
65+
*/
66+
TaskNameResolver getTaskNameResolver();
67+
6168
}

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/support/SimpleTaskNameResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@
2525

2626
/**
2727
* Simple implementation of the {@link TaskNameResolver} interface. Names the task based
28-
* on the following order of precidence:
28+
* on the following order of precedence:
2929
* <ol>
3030
* <li>A configured property <code>spring.cloud.task.name</code></li>
3131
* <li>The {@link ApplicationContext}'s id.</li>

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,7 @@
4040
import org.springframework.cloud.task.configuration.SingleTaskConfiguration;
4141
import org.springframework.cloud.task.configuration.TaskConfigurer;
4242
import org.springframework.cloud.task.repository.TaskExplorer;
43+
import org.springframework.cloud.task.repository.TaskNameResolver;
4344
import org.springframework.cloud.task.repository.TaskRepository;
4445
import org.springframework.cloud.task.repository.support.SimpleTaskRepository;
4546
import org.springframework.context.ApplicationContextException;
@@ -131,6 +132,20 @@ public void testRepositoryNotInitialized() {
131132
verifyExceptionThrownDefaultExecutable(ApplicationContextException.class, applicationContextRunner);
132133
}
133134

135+
@Test
136+
public void testTaskNameResolver() {
137+
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
138+
.withConfiguration(AutoConfigurations.of(EmbeddedDataSourceConfiguration.class,
139+
PropertyPlaceholderAutoConfiguration.class, SimpleTaskAutoConfiguration.class,
140+
SingleTaskConfiguration.class))
141+
.withUserConfiguration(TaskLifecycleListenerConfiguration.class)
142+
.withPropertyValues("spring.cloud.task.name=myTestName");
143+
applicationContextRunner.run((context) -> {
144+
TaskNameResolver taskNameResolver = context.getBean(TaskNameResolver.class);
145+
assertThat(taskNameResolver.getTaskName()).isEqualTo("myTestName");
146+
});
147+
}
148+
134149
@Test
135150
public void testMultipleConfigurers() {
136151
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()

spring-cloud-task-core/src/test/java/org/springframework/cloud/task/configuration/DefaultTaskConfigurerTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2022 the original author or authors.
2+
* Copyright 2017-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -90,6 +90,14 @@ public void taskExplorerTest() {
9090
assertThat(defaultTaskConfigurer.getTaskExplorer()).isNotNull();
9191
}
9292

93+
@Test
94+
public void taskNameResolverTest() {
95+
DefaultTaskConfigurer defaultTaskConfigurer = new DefaultTaskConfigurer(this.dataSource);
96+
assertThat(defaultTaskConfigurer.getTaskNameResolver()).isNotNull();
97+
defaultTaskConfigurer = new DefaultTaskConfigurer();
98+
assertThat(defaultTaskConfigurer.getTaskNameResolver()).isNotNull();
99+
}
100+
93101
@Test
94102
public void taskRepositoryTest() {
95103
DefaultTaskConfigurer defaultTaskConfigurer = new DefaultTaskConfigurer(this.dataSource);

0 commit comments

Comments
 (0)