Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions spring-cloud-task-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@
<artifactId>spring-integration-jdbc</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
Expand Down Expand Up @@ -135,6 +145,16 @@
<artifactId>micrometer-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-observation-test</artifactId>
Expand Down Expand Up @@ -180,11 +200,6 @@
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mariadb</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2015-present 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.task.configuration;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.task.repository.dao.MongoLockRepository;
import org.springframework.cloud.task.repository.support.MongoTaskRepositoryInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.integration.support.locks.LockRegistry;
import org.springframework.transaction.PlatformTransactionManager;

/**
* Auto-configuration for MongoDB-based Task repository.
* This configuration is activated when MongoDB is available and configured,
* and when the appropriate properties are set to enable MongoDB task storage.
*
* @author JongJun Kim
*/
@AutoConfiguration
@ConditionalOnClass({ MongoOperations.class })
@ConditionalOnBean(MongoOperations.class)
@ConditionalOnProperty(prefix = "spring.cloud.task", name = "repository-type", havingValue = "mongodb", matchIfMissing = false)
@EnableConfigurationProperties(TaskProperties.class)
public class MongoTaskAutoConfiguration {

/**
* MongoDB Repository Initializer configuration.
*/
@Bean
@ConditionalOnMissingBean(MongoTaskRepositoryInitializer.class)
public MongoTaskRepositoryInitializer mongoTaskRepositoryInitializer(MongoOperations mongoOperations,
TaskProperties taskProperties) {
return new MongoTaskRepositoryInitializer(mongoOperations, taskProperties);
}

/**
* MongoDB Lock Registry configuration for single instance task execution.
*/
@Bean
@ConditionalOnMissingBean(LockRegistry.class)
@ConditionalOnProperty(prefix = "spring.cloud.task", name = "single-instance-enabled", havingValue = "true")
public LockRegistry mongoLockRegistry(MongoOperations mongoOperations, TaskProperties taskProperties) {
return new MongoLockRepository(mongoOperations, taskProperties);
}

/**
* Configuration for MongoDB Task components when no custom {@link TaskConfigurer} is provided.
*/
@Configuration
@ConditionalOnMissingBean(TaskConfigurer.class)
public static class MongoTaskConfigurerConfiguration {

/**
* Creates a {@link MongoTaskConfigurer} with transaction manager when MongoDB is
* the configured repository type, no custom {@link TaskConfigurer} bean exists,
* and a {@link PlatformTransactionManager} is available.
* @param mongoOperations the {@link MongoOperations} instance
* @param taskProperties the {@link TaskProperties} configuration
* @param transactionManager the {@link PlatformTransactionManager} for transaction support
* @return a configured {@link MongoTaskConfigurer} with transaction support
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(PlatformTransactionManager.class)
public TaskConfigurer taskConfigurer(MongoOperations mongoOperations,
TaskProperties taskProperties, PlatformTransactionManager transactionManager) {
return new MongoTaskConfigurer(mongoOperations, taskProperties, transactionManager);
}

/**
* Creates a {@link MongoTaskConfigurer} when MongoDB is the configured repository type
* and no custom {@link TaskConfigurer} bean exists.
* @param mongoOperations the {@link MongoOperations} instance
* @param taskProperties the {@link TaskProperties} configuration
* @return a configured {@link MongoTaskConfigurer}
*/
@Bean
@ConditionalOnMissingBean
public TaskConfigurer taskConfigurerWithoutTransactionManager(MongoOperations mongoOperations, TaskProperties taskProperties) {
return new MongoTaskConfigurer(mongoOperations, taskProperties);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2015-present 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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.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.data.mongodb.core.MongoOperations;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.Assert;

/**
* A {@link TaskConfigurer} implementation that uses MongoDB for storing Task metadata.
* This configurer uses MongoDB collections to store task execution information instead
* of relational database tables.
*
* @author JongJun Kim
*/
public class MongoTaskConfigurer implements TaskConfigurer {

private final MongoOperations mongoOperations;

private final TaskProperties taskProperties;

private TaskRepository taskRepository;

private TaskExplorer taskExplorer;

private PlatformTransactionManager transactionManager;

/**
* Create a new {@link MongoTaskConfigurer} with the provided {@link MongoOperations}.
* @param mongoOperations the {@link MongoOperations} to use for task metadata storage
* @param taskProperties the {@link TaskProperties} for configuration
*/
public MongoTaskConfigurer(MongoOperations mongoOperations, TaskProperties taskProperties) {
Assert.notNull(mongoOperations, "mongoOperations must not be null");
Assert.notNull(taskProperties, "taskProperties must not be null");
this.mongoOperations = mongoOperations;
this.taskProperties = taskProperties;
}

/**
* Create a new {@link MongoTaskConfigurer} with the provided {@link MongoOperations}
* and {@link PlatformTransactionManager}.
* @param mongoOperations the {@link MongoOperations} to use for task metadata storage
* @param taskProperties the {@link TaskProperties} for configuration
* @param transactionManager the {@link PlatformTransactionManager} for transaction management
*/
public MongoTaskConfigurer(MongoOperations mongoOperations, TaskProperties taskProperties,
PlatformTransactionManager transactionManager) {
this(mongoOperations, taskProperties);
this.transactionManager = transactionManager;
}

@Override
public TaskRepository getTaskRepository() {
if (this.taskRepository == null) {
this.taskRepository = new SimpleTaskRepository(new TaskExecutionDaoFactoryBean(this.mongoOperations, this.taskProperties));
}
return this.taskRepository;
}

@Override
public TaskExplorer getTaskExplorer() {
if (this.taskExplorer == null) {
this.taskExplorer = new SimpleTaskExplorer(new TaskExecutionDaoFactoryBean(this.mongoOperations, this.taskProperties));
}
return this.taskExplorer;
}

@Override
public PlatformTransactionManager getTransactionManager() {
return this.transactionManager;
}

@Override
public TaskNameResolver getTaskNameResolver() {
return new SimpleTaskNameResolver();
}

@Override
public DataSource getTaskDataSource() {
return null;
}
}
Loading