GH-175 - Auto-configure transactions for MongoDB by default.
As the Event Publication registry only works in transactional environments, we should enable them for MongoDB by default. This will require a replica setup for the MongoDB instance the application interacts with.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.modulith.events.mongodb;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.mongodb.MongoDatabaseFactory;
|
||||
import org.springframework.data.mongodb.MongoTransactionManager;
|
||||
import org.springframework.modulith.events.EventPublicationRegistry;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
/**
|
||||
* Auto-configuration to enable MongoDB transaction management as that is required for the
|
||||
* {@link EventPublicationRegistry} to work properly.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@ConditionalOnProperty(//
|
||||
name = "spring.modulith.events.mongobd.transaction-management.enabled",
|
||||
havingValue = "true",
|
||||
matchIfMissing = true)
|
||||
class MongoDbTransactionAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
MongoTransactionManager transactionManager(MongoDatabaseFactory factory) {
|
||||
return new MongoTransactionManager(factory);
|
||||
}
|
||||
|
||||
@Bean
|
||||
TransactionTemplate transactionTemplate(PlatformTransactionManager txManager) {
|
||||
return new TransactionTemplate(txManager);
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
org.springframework.modulith.events.mongodb.MongoDbEventPublicationAutoConfiguration
|
||||
org.springframework.modulith.events.mongodb.MongoDbTransactionAutoConfiguration
|
||||
|
||||
@@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.mongodb.MongoTransactionManager;
|
||||
import org.springframework.modulith.events.EventPublicationRegistry;
|
||||
import org.springframework.modulith.testapp.TestApplication;
|
||||
|
||||
@@ -34,10 +35,11 @@ class MongoDbEventPublicationAutoConfigurationIntegrationTests {
|
||||
|
||||
@Autowired ApplicationContext context;
|
||||
|
||||
@Test // GH-4
|
||||
@Test // GH-4, GH-175
|
||||
void bootstrapsApplicationComponents() {
|
||||
|
||||
assertThat(context.getBean(EventPublicationRegistry.class)).isNotNull();
|
||||
assertThat(context.getBean(MongoDbEventPublicationRepository.class)).isNotNull();
|
||||
assertThat(context.getBean(MongoTransactionManager.class)).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,8 +147,11 @@ To ease that task, Spring Modulith provides starter POMs that are centered aroun
|
||||
The following starters are available:
|
||||
|
||||
* `spring-modulith-starter-jpa` -- Using JPA as persistence technology.
|
||||
* `spring-modulith-starter-jdbc` -- Using JDBC as persistence technology. Also works in JPA-based applications but bypasses your JPA provider for actual event persistence.
|
||||
* `spring-modulith-starter-jdbc` -- Using JDBC as persistence technology.
|
||||
Also works in JPA-based applications but bypasses your JPA provider for actual event persistence.
|
||||
* `spring-modulith-starter-mongodb` -- Using MongoDB behind Spring Data MongoDB.
|
||||
Also enables MongoDB transactions and requires a replica set setup of the server to interact with.
|
||||
The transaction auto-configuration can be disabled by setting the `spring.modulith.events.mongobd.transaction-management.enabled` property to `false`.
|
||||
|
||||
[[events.integration-testing]]
|
||||
== Integration Testing Application Modules Working with Events
|
||||
|
||||
Reference in New Issue
Block a user