Skip to content

Commit 2d9d5df

Browse files
committed
Add MongoDB support for Spring Cloud Task with configuration and repository initialization
Signed-off-by: belljun3395 <[email protected]>
1 parent 28e4dfd commit 2d9d5df

File tree

14 files changed

+3229
-7
lines changed

14 files changed

+3229
-7
lines changed

spring-cloud-task-core/pom.xml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@
8585
<artifactId>spring-integration-jdbc</artifactId>
8686
<optional>true</optional>
8787
</dependency>
88+
<dependency>
89+
<groupId>org.mongodb</groupId>
90+
<artifactId>mongodb-driver-sync</artifactId>
91+
<optional>true</optional>
92+
</dependency>
93+
<dependency>
94+
<groupId>org.springframework.data</groupId>
95+
<artifactId>spring-data-mongodb</artifactId>
96+
<optional>true</optional>
97+
</dependency>
8898
<dependency>
8999
<groupId>jakarta.platform</groupId>
90100
<artifactId>jakarta.jakartaee-api</artifactId>
@@ -135,6 +145,16 @@
135145
<artifactId>micrometer-test</artifactId>
136146
<scope>test</scope>
137147
</dependency>
148+
<dependency>
149+
<groupId>org.testcontainers</groupId>
150+
<artifactId>mongodb</artifactId>
151+
<scope>test</scope>
152+
</dependency>
153+
<dependency>
154+
<groupId>org.testcontainers</groupId>
155+
<artifactId>junit-jupiter</artifactId>
156+
<scope>test</scope>
157+
</dependency>
138158
<dependency>
139159
<groupId>io.micrometer</groupId>
140160
<artifactId>micrometer-observation-test</artifactId>
@@ -180,11 +200,6 @@
180200
<artifactId>testcontainers</artifactId>
181201
<scope>test</scope>
182202
</dependency>
183-
<dependency>
184-
<groupId>org.testcontainers</groupId>
185-
<artifactId>junit-jupiter</artifactId>
186-
<scope>test</scope>
187-
</dependency>
188203
<dependency>
189204
<groupId>org.testcontainers</groupId>
190205
<artifactId>mariadb</artifactId>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2015-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.task.configuration;
18+
19+
import org.springframework.boot.autoconfigure.AutoConfiguration;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
24+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
25+
import org.springframework.cloud.task.repository.dao.MongoLockRepository;
26+
import org.springframework.cloud.task.repository.support.MongoTaskRepositoryInitializer;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.data.mongodb.core.MongoOperations;
30+
import org.springframework.integration.support.locks.LockRegistry;
31+
import org.springframework.transaction.PlatformTransactionManager;
32+
33+
/**
34+
* Auto-configuration for MongoDB-based Task repository.
35+
* This configuration is activated when MongoDB is available and configured,
36+
* and when the appropriate properties are set to enable MongoDB task storage.
37+
*
38+
* @author JongJun Kim
39+
*/
40+
@AutoConfiguration
41+
@ConditionalOnClass({ MongoOperations.class })
42+
@ConditionalOnBean(MongoOperations.class)
43+
@ConditionalOnProperty(prefix = "spring.cloud.task", name = "repository-type", havingValue = "mongodb", matchIfMissing = false)
44+
@EnableConfigurationProperties(TaskProperties.class)
45+
public class MongoTaskAutoConfiguration {
46+
47+
/**
48+
* MongoDB Repository Initializer configuration.
49+
*/
50+
@Bean
51+
@ConditionalOnMissingBean(MongoTaskRepositoryInitializer.class)
52+
public MongoTaskRepositoryInitializer mongoTaskRepositoryInitializer(MongoOperations mongoOperations,
53+
TaskProperties taskProperties) {
54+
return new MongoTaskRepositoryInitializer(mongoOperations, taskProperties);
55+
}
56+
57+
/**
58+
* MongoDB Lock Registry configuration for single instance task execution.
59+
*/
60+
@Bean
61+
@ConditionalOnMissingBean(LockRegistry.class)
62+
@ConditionalOnProperty(prefix = "spring.cloud.task", name = "single-instance-enabled", havingValue = "true")
63+
public LockRegistry mongoLockRegistry(MongoOperations mongoOperations, TaskProperties taskProperties) {
64+
return new MongoLockRepository(mongoOperations, taskProperties);
65+
}
66+
67+
/**
68+
* Configuration for MongoDB Task components when no custom {@link TaskConfigurer} is provided.
69+
*/
70+
@Configuration
71+
@ConditionalOnMissingBean(TaskConfigurer.class)
72+
public static class MongoTaskConfigurerConfiguration {
73+
74+
/**
75+
* Creates a {@link MongoTaskConfigurer} with transaction manager when MongoDB is
76+
* the configured repository type, no custom {@link TaskConfigurer} bean exists,
77+
* and a {@link PlatformTransactionManager} is available.
78+
* @param mongoOperations the {@link MongoOperations} instance
79+
* @param taskProperties the {@link TaskProperties} configuration
80+
* @param transactionManager the {@link PlatformTransactionManager} for transaction support
81+
* @return a configured {@link MongoTaskConfigurer} with transaction support
82+
*/
83+
@Bean
84+
@ConditionalOnMissingBean
85+
@ConditionalOnBean(PlatformTransactionManager.class)
86+
public TaskConfigurer taskConfigurer(MongoOperations mongoOperations,
87+
TaskProperties taskProperties, PlatformTransactionManager transactionManager) {
88+
return new MongoTaskConfigurer(mongoOperations, taskProperties, transactionManager);
89+
}
90+
91+
/**
92+
* Creates a {@link MongoTaskConfigurer} when MongoDB is the configured repository type
93+
* and no custom {@link TaskConfigurer} bean exists.
94+
* @param mongoOperations the {@link MongoOperations} instance
95+
* @param taskProperties the {@link TaskProperties} configuration
96+
* @return a configured {@link MongoTaskConfigurer}
97+
*/
98+
@Bean
99+
@ConditionalOnMissingBean
100+
public TaskConfigurer taskConfigurerWithoutTransactionManager(MongoOperations mongoOperations, TaskProperties taskProperties) {
101+
return new MongoTaskConfigurer(mongoOperations, taskProperties);
102+
}
103+
}
104+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2015-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.task.configuration;
18+
19+
import javax.sql.DataSource;
20+
21+
import org.springframework.cloud.task.repository.TaskExplorer;
22+
import org.springframework.cloud.task.repository.TaskNameResolver;
23+
import org.springframework.cloud.task.repository.TaskRepository;
24+
import org.springframework.cloud.task.repository.support.SimpleTaskExplorer;
25+
import org.springframework.cloud.task.repository.support.SimpleTaskNameResolver;
26+
import org.springframework.cloud.task.repository.support.SimpleTaskRepository;
27+
import org.springframework.cloud.task.repository.support.TaskExecutionDaoFactoryBean;
28+
import org.springframework.data.mongodb.core.MongoOperations;
29+
import org.springframework.transaction.PlatformTransactionManager;
30+
import org.springframework.util.Assert;
31+
32+
/**
33+
* A {@link TaskConfigurer} implementation that uses MongoDB for storing Task metadata.
34+
* This configurer uses MongoDB collections to store task execution information instead
35+
* of relational database tables.
36+
*
37+
* @author JongJun Kim
38+
*/
39+
public class MongoTaskConfigurer implements TaskConfigurer {
40+
41+
private final MongoOperations mongoOperations;
42+
43+
private final TaskProperties taskProperties;
44+
45+
private TaskRepository taskRepository;
46+
47+
private TaskExplorer taskExplorer;
48+
49+
private PlatformTransactionManager transactionManager;
50+
51+
/**
52+
* Create a new {@link MongoTaskConfigurer} with the provided {@link MongoOperations}.
53+
* @param mongoOperations the {@link MongoOperations} to use for task metadata storage
54+
* @param taskProperties the {@link TaskProperties} for configuration
55+
*/
56+
public MongoTaskConfigurer(MongoOperations mongoOperations, TaskProperties taskProperties) {
57+
Assert.notNull(mongoOperations, "mongoOperations must not be null");
58+
Assert.notNull(taskProperties, "taskProperties must not be null");
59+
this.mongoOperations = mongoOperations;
60+
this.taskProperties = taskProperties;
61+
}
62+
63+
/**
64+
* Create a new {@link MongoTaskConfigurer} with the provided {@link MongoOperations}
65+
* and {@link PlatformTransactionManager}.
66+
* @param mongoOperations the {@link MongoOperations} to use for task metadata storage
67+
* @param taskProperties the {@link TaskProperties} for configuration
68+
* @param transactionManager the {@link PlatformTransactionManager} for transaction management
69+
*/
70+
public MongoTaskConfigurer(MongoOperations mongoOperations, TaskProperties taskProperties,
71+
PlatformTransactionManager transactionManager) {
72+
this(mongoOperations, taskProperties);
73+
this.transactionManager = transactionManager;
74+
}
75+
76+
@Override
77+
public TaskRepository getTaskRepository() {
78+
if (this.taskRepository == null) {
79+
this.taskRepository = new SimpleTaskRepository(new TaskExecutionDaoFactoryBean(this.mongoOperations, this.taskProperties));
80+
}
81+
return this.taskRepository;
82+
}
83+
84+
@Override
85+
public TaskExplorer getTaskExplorer() {
86+
if (this.taskExplorer == null) {
87+
this.taskExplorer = new SimpleTaskExplorer(new TaskExecutionDaoFactoryBean(this.mongoOperations, this.taskProperties));
88+
}
89+
return this.taskExplorer;
90+
}
91+
92+
@Override
93+
public PlatformTransactionManager getTransactionManager() {
94+
return this.transactionManager;
95+
}
96+
97+
@Override
98+
public TaskNameResolver getTaskNameResolver() {
99+
return new SimpleTaskNameResolver();
100+
}
101+
102+
@Override
103+
public DataSource getTaskDataSource() {
104+
return null;
105+
}
106+
}

0 commit comments

Comments
 (0)