Skip to content

Commit ba82efd

Browse files
AdamaSorhoartembilan
authored andcommitted
GH-8692 Add createIndexes to MongoDbMessageStore
Fixes #8692 * Added `createIndexes` in `AbstractConfigurableMongoDbMessageStore` * Added Javadoc for `setCreateIndex()` method * Removed `afterPropertiesSet()` in `MongoDbChannelMessageStore` and update `whats-new.adoc` and `mongodb.adoc` files **Cherry-pick to `6.1.x` & `6.0.x`** # Conflicts: # src/reference/antora/modules/ROOT/pages/whats-new.adoc # src/reference/asciidoc/mongodb.adoc
1 parent 5a93607 commit ba82efd

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

spring-integration-mongodb/src/main/java/org/springframework/integration/mongodb/store/AbstractConfigurableMongoDbMessageStore.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-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.
@@ -60,6 +60,7 @@
6060
* for implementations of this class.
6161
*
6262
* @author Artem Bilan
63+
* @author Adama Sorho
6364
*
6465
* @since 4.0
6566
*/
@@ -86,6 +87,8 @@ public abstract class AbstractConfigurableMongoDbMessageStore extends AbstractMe
8687

8788
private MessageBuilderFactory messageBuilderFactory = new DefaultMessageBuilderFactory();
8889

90+
private boolean createIndexes = true;
91+
8992
public AbstractConfigurableMongoDbMessageStore(MongoTemplate mongoTemplate, String collectionName) {
9093
Assert.notNull(mongoTemplate, "'mongoTemplate' must not be null");
9194
Assert.hasText(collectionName, "'collectionName' must not be empty");
@@ -107,6 +110,15 @@ public AbstractConfigurableMongoDbMessageStore(MongoDatabaseFactory mongoDbFacto
107110
this.mappingMongoConverter = mappingMongoConverter;
108111
}
109112

113+
/**
114+
* Define the option to auto create indexes or not.
115+
* @param createIndexes a boolean.
116+
* @since 6.0.8.
117+
*/
118+
public void setCreateIndexes(boolean createIndexes) {
119+
this.createIndexes = createIndexes;
120+
}
121+
110122
@Override
111123
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
112124
this.applicationContext = applicationContext;
@@ -146,6 +158,12 @@ public void afterPropertiesSet() {
146158

147159
this.messageBuilderFactory = IntegrationUtils.getMessageBuilderFactory(this.applicationContext);
148160

161+
if (this.createIndexes) {
162+
createIndexes();
163+
}
164+
}
165+
166+
protected void createIndexes() {
149167
IndexOperations indexOperations = this.mongoTemplate.indexOps(this.collectionName);
150168

151169
indexOperations.ensureIndex(new Index(MessageDocumentFields.MESSAGE_ID, Sort.Direction.ASC));

spring-integration-mongodb/src/main/java/org/springframework/integration/mongodb/store/MongoDbChannelMessageStore.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2020 the original author or authors.
2+
* Copyright 2014-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.
@@ -43,6 +43,7 @@
4343
* {@code priorityEnabled = true} option.
4444
*
4545
* @author Artem Bilan
46+
* @author Adama Sorho
4647
*
4748
* @since 4.0
4849
*/
@@ -94,8 +95,8 @@ public boolean isPriorityEnabled() {
9495
}
9596

9697
@Override
97-
public void afterPropertiesSet() {
98-
super.afterPropertiesSet();
98+
protected void createIndexes() {
99+
super.createIndexes();
99100
getMongoTemplate()
100101
.indexOps(this.collectionName)
101102
.ensureIndex(new Index(MessageDocumentFields.GROUP_ID, Sort.Direction.ASC)

spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/store/ConfigurableMongoDbMessageGroupStoreTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.integration.mongodb.store;
1818

19+
import java.util.List;
1920
import java.util.Map;
2021

2122
import org.bson.Document;
@@ -25,6 +26,8 @@
2526
import org.springframework.context.support.ClassPathXmlApplicationContext;
2627
import org.springframework.core.convert.converter.Converter;
2728
import org.springframework.data.convert.ReadingConverter;
29+
import org.springframework.data.mongodb.core.MongoTemplate;
30+
import org.springframework.data.mongodb.core.index.IndexInfo;
2831
import org.springframework.integration.IntegrationMessageHeaderAccessor;
2932
import org.springframework.integration.channel.PriorityChannel;
3033
import org.springframework.integration.channel.QueueChannel;
@@ -42,6 +45,7 @@
4245
* @author Amol Nayak
4346
* @author Artem Bilan
4447
* @author Artem Vozhdayenko
48+
* @author Adama Sorho
4549
*
4650
*/
4751
class ConfigurableMongoDbMessageGroupStoreTests extends AbstractMongoDbMessageGroupStoreTests {
@@ -134,6 +138,12 @@ void testWithCustomConverter() {
134138
.getClass());
135139
context.refresh();
136140

141+
List<IndexInfo> indexesInfo =
142+
new MongoTemplate(MONGO_DATABASE_FACTORY)
143+
.indexOps("customConverterCollection")
144+
.getIndexInfo();
145+
assertThat(indexesInfo).hasSize(0);
146+
137147
TestGateway gateway = context.getBean(TestGateway.class);
138148
String result = gateway.service("foo");
139149
assertThat(result).isEqualTo("FOO");

spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/store/ConfigurableMongoDbMessageStore-CustomConverter.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
<beans:bean id="messageStore" parent="abstractMessageStore">
3131
<beans:constructor-arg name="mappingMongoConverter" ref="customConverter"/>
32+
<beans:constructor-arg name="collectionName" value="customConverterCollection"/>
33+
<beans:property name="createIndexes" value="false" />
3234
</beans:bean>
3335

3436
<gateway id="gateway"

0 commit comments

Comments
 (0)