Skip to content

Commit

Permalink
spring-projectsGH-8692 Add createIndexes to MongoDbMessageStore
Browse files Browse the repository at this point in the history
Fixes spring-projects#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`**
  • Loading branch information
AdamaSorho authored and artembilan committed Aug 30, 2023
1 parent 3f5f32b commit 32eba4e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -60,6 +60,7 @@
* for implementations of this class.
*
* @author Artem Bilan
* @author Adama Sorho
*
* @since 4.0
*/
Expand All @@ -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");
Expand All @@ -107,6 +110,15 @@ public AbstractConfigurableMongoDbMessageStore(MongoDatabaseFactory mongoDbFacto
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;
Expand Down Expand Up @@ -146,6 +158,12 @@ public void afterPropertiesSet() {

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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -43,6 +43,7 @@
* {@code priorityEnabled = true} option.
*
* @author Artem Bilan
* @author Adama Sorho
*
* @since 4.0
*/
Expand Down Expand Up @@ -94,8 +95,8 @@ public boolean isPriorityEnabled() {
}

@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
protected void createIndexes() {
super.createIndexes();
getMongoTemplate()
.indexOps(this.collectionName)
.ensureIndex(new Index(MessageDocumentFields.GROUP_ID, Sort.Direction.ASC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.integration.mongodb.store;

import java.util.List;
import java.util.Map;

import org.bson.Document;
Expand All @@ -25,6 +26,8 @@
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;
Expand All @@ -42,6 +45,7 @@
* @author Amol Nayak
* @author Artem Bilan
* @author Artem Vozhdayenko
* @author Adama Sorho
*
*/
class ConfigurableMongoDbMessageGroupStoreTests extends AbstractMongoDbMessageGroupStoreTests {
Expand Down Expand Up @@ -134,6 +138,12 @@ void testWithCustomConverter() {
.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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
29 changes: 29 additions & 0 deletions src/reference/antora/modules/ROOT/pages/mongodb.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/reference/antora/modules/ROOT/pages/whats-new.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

0 comments on commit 32eba4e

Please sign in to comment.