|
| 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 | +} |
0 commit comments