GH-8692 Add createIndexes to MongoDbMessageStore

Fixes https://github.com/spring-projects/spring-integration/issues/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
This commit is contained in:
Adama Sorho
2023-08-28 15:02:58 -04:00
committed by Artem Bilan
parent bdfbf7b03d
commit 26fae85fa1
4 changed files with 35 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-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.
@@ -60,6 +60,7 @@ import org.springframework.util.Assert;
* for implementations of this class.
*
* @author Artem Bilan
* @author Adama Sorho
*
* @since 4.0
*/
@@ -86,6 +87,8 @@ public abstract class AbstractConfigurableMongoDbMessageStore extends AbstractMe
private MessageBuilderFactory messageBuilderFactory = new DefaultMessageBuilderFactory();
private boolean createIndexes = true;
public AbstractConfigurableMongoDbMessageStore(MongoTemplate mongoTemplate, String collectionName) {
Assert.notNull(mongoTemplate, "'mongoTemplate' must not be null");
Assert.hasText(collectionName, "'collectionName' must not be empty");
@@ -107,6 +110,15 @@ public abstract class AbstractConfigurableMongoDbMessageStore extends AbstractMe
this.mappingMongoConverter = mappingMongoConverter;
}
/**
* Define the option to auto create indexes or not.
* @param createIndexes a boolean.
* @since 6.0.8.
*/
public void setCreateIndexes(boolean createIndexes) {
this.createIndexes = createIndexes;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
@@ -146,6 +158,12 @@ public abstract class AbstractConfigurableMongoDbMessageStore extends AbstractMe
this.messageBuilderFactory = IntegrationUtils.getMessageBuilderFactory(this.applicationContext);
if (this.createIndexes) {
createIndexes();
}
}
protected void createIndexes() {
IndexOperations indexOperations = this.mongoTemplate.indexOps(this.collectionName);
indexOperations.ensureIndex(new Index(MessageDocumentFields.MESSAGE_ID, Sort.Direction.ASC));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2020 the original author or authors.
* Copyright 2014-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.
@@ -43,6 +43,7 @@ import org.springframework.util.Assert;
* {@code priorityEnabled = true} option.
*
* @author Artem Bilan
* @author Adama Sorho
*
* @since 4.0
*/
@@ -94,8 +95,8 @@ public class MongoDbChannelMessageStore extends AbstractConfigurableMongoDbMessa
}
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
protected void createIndexes() {
super.createIndexes();
getMongoTemplate()
.indexOps(this.collectionName)
.ensureIndex(new Index(MessageDocumentFields.GROUP_ID, Sort.Direction.ASC)

View File

@@ -16,6 +16,7 @@
package org.springframework.integration.mongodb.store;
import java.util.List;
import java.util.Map;
import org.bson.Document;
@@ -25,6 +26,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.index.IndexInfo;
import org.springframework.integration.IntegrationMessageHeaderAccessor;
import org.springframework.integration.channel.PriorityChannel;
import org.springframework.integration.channel.QueueChannel;
@@ -42,6 +45,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Amol Nayak
* @author Artem Bilan
* @author Artem Vozhdayenko
* @author Adama Sorho
*
*/
class ConfigurableMongoDbMessageGroupStoreTests extends AbstractMongoDbMessageGroupStoreTests {
@@ -134,6 +138,12 @@ class ConfigurableMongoDbMessageGroupStoreTests extends AbstractMongoDbMessageGr
.getClass());
context.refresh();
List<IndexInfo> indexesInfo =
new MongoTemplate(MONGO_DATABASE_FACTORY)
.indexOps("customConverterCollection")
.getIndexInfo();
assertThat(indexesInfo).hasSize(0);
TestGateway gateway = context.getBean(TestGateway.class);
String result = gateway.service("foo");
assertThat(result).isEqualTo("FOO");

View File

@@ -29,6 +29,8 @@
<beans:bean id="messageStore" parent="abstractMessageStore">
<beans:constructor-arg name="mappingMongoConverter" ref="customConverter"/>
<beans:constructor-arg name="collectionName" value="customConverterCollection"/>
<beans:property name="createIndexes" value="false" />
</beans:bean>
<gateway id="gateway"