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`**
This commit is contained in:
Adama Sorho
2023-08-28 15:02:58 -04:00
committed by Artem Bilan
parent 3f5f32b1e0
commit 32eba4ecb9
6 changed files with 71 additions and 5 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"

View File

@@ -197,6 +197,19 @@ It builds a `MongoTemplate` from the provided `MongoDbFactory` and `MappingMongo
The default name for the collection stored by the `ConfigurableMongoDbMessageStore` is `configurableStoreMessages`.
We recommend using this implementation to create robust and flexible solutions when messages contain complex data types.
Starting with version 6.0.8, the `AbstractConfigurableMongoDbMessageStore` provides a `setCreateIndexes(boolean)` (defaults to `true`) option which can be used to disable the auto indexes creation.
The following example shows how to declare a bean and disable the auto indexes creation:
[source, java]
----
@Bean
public MongoDbChannelMessageStore mongoDbChannelMessageStore(MongoDatabaseFactory databaseFactory) {
MongoDbChannelMessageStore mongoDbChannelMessageStore = new MongoDbChannelMessageStore(databaseFactory);
mongoDbChannelMessageStore.setCreateIndexes(false);
return mongoDbChannelMessageStore;
}
----
[[mongodb-priority-channel-message-store]]
=== MongoDB Channel Message Store
@@ -231,6 +244,22 @@ To configure that scenario, you can extend one message store bean from the other
<int:priority-queue message-store="priorityStore"/>
</int:channel>
----
[[abstract-configurable-mongodb-message-store-with-auto-index-creation-disable]]
=== Using AbstractConfigurableMongoDbMessageStore with auto index creation disable
Starting with version 6.0.8, the `AbstractConfigurableMongoDbMessageStore` implements a `setCreateIndex(boolean)` which can be use to desable or enable (default) the auto index creation.
The following example shows how to declare a bean and disable the auto index creation:
[source, java]
----
@Bean
public AbstractConfigurableMongoDbMessageStore mongoDbChannelMessageStore(MongoDatabaseFactory databaseFactory)
{
AbstractConfigurableMongoDbMessageStore mongoDbChannelMessageStore = new MongoDbChannelMessageStore(databaseFactory);
mongoDbChannelMessageStore.setCreateIndex(false);
return mongoDbChannelMessageStore;
}
----
[[mongodb-metadata-store]]
=== MongoDB Metadata Store

View File

@@ -37,7 +37,7 @@ See, for example, `transformWith()`, `splitWith()` in xref:dsl.adoc#java-dsl[ Ja
See xref:configuration/global-properties.adoc[Global Properties] for more information.
- The `@MessagingGateway` and `GatewayEndpointSpec` provided by the Java DSL now expose the `errorOnTimeout` property of the internal `MethodInvocationGateway` extension of the `MessagingGatewaySupport`.
See xref:gateway.adoc#gateway-no-response[Gateway Behavior When No response Arrives] for more information.
See xref:gateway.adoc#gateway-no-response[ Gateway Behavior When No response Arrives] for more information.
[[x6.2-websockets]]
=== WebSockets Changes
@@ -57,3 +57,9 @@ See xref:kafka.adoc#kafka-inbound-pollable[Kafka Inbound Channel Adapter] for mo
The `JdbcMessageStore`, `JdbcChannelMessageStore`, `JdbcMetadataStore`, and `DefaultLockRepository` implement `SmartLifecycle` and perform a`SELECT COUNT` query, on their respective tables, in the `start()` method to ensure that the required table (according to the provided prefix) is present in the target database.
See xref:jdbc/message-store.adoc#jdbc-db-init[Initializing the Database] for more information.
[[x6.2-mongodb]]
=== MongoDB support change
A new option `setCreateIndexes(boolean)` has been introduced in `AbstractConfigurableMongoDbMessageStore` to disable the auto indexes creation.
See xref:mongodb.adoc#mongodb-message-store[MongoDB Message Store] for an example.