Skip to content

Commit

Permalink
User can establish taskNameReslover via configurer vs SimpleTaskAutoC…
Browse files Browse the repository at this point in the history
…onfiguration.

resolves #801
  • Loading branch information
cppwfs committed Sep 7, 2023
1 parent 1f39989 commit dbbae91
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -84,6 +83,8 @@ public class SimpleTaskAutoConfiguration {

private TaskExplorer taskExplorer;

private TaskNameResolver taskNameResolver;

@Bean
public SimpleTaskRepository taskRepository() {
return (SimpleTaskRepository) this.taskRepository;
Expand All @@ -102,7 +103,7 @@ public TaskExplorer taskExplorer() {

@Bean
public TaskNameResolver taskNameResolver() {
return new SimpleTaskNameResolver();
return taskNameResolver;
}

@Bean
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;

Expand Down Expand Up @@ -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();

}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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:
* <ol>
* <li>A configured property <code>spring.cloud.task.name</code></li>
* <li>The {@link ApplicationContext}'s id.</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit dbbae91

Please sign in to comment.