Reactive MongoDB multiple database connections

Is there a way to make a multiple mongo database connection configuration with reactive mongo. Currently using the following to make a webflux back-end that needs to connect to 2 different databases:

  • Springboot: 2.3.1.RELEASE
  • Webflux (this is the important part!! not using MVC)
  • JAVA 11
  • Gradle 6.5

I tried to translate my already existing MongoDB configuration to Webflux but I get the following error:

Parameter 0 of constructor in nl.suitless.moduleservice.Services.Module.ModuleService required a bean named 'reactiveMongoTemplate' that could not be found.


Action:

Consider defining a bean named 'reactiveMongoTemplate' in your configuration.

Code:

@Configuration
@EnableConfigurationProperties(MultipleMongoProperties.class)
public class MultipleMongoConfig {

    private final MultipleMongoProperties mongoProperties;

    @Autowired()
    public MultipleMongoConfig(MultipleMongoProperties mongoProperties) {
        this.mongoProperties = mongoProperties;
    }

    @Primary
    @Bean(name = "primaryReactiveMongoTemplate")
    public ReactiveMongoTemplate primaryReactiveMongoTemplate() {
        return new ReactiveMongoTemplate(primaryFactory(this.mongoProperties.getPrimary()));
    }

    @Bean(name = "secondaryReactiveMongoTemplate")
    public ReactiveMongoTemplate secondaryReactiveMongoTemplate() {
        return new ReactiveMongoTemplate (secondaryFactory(this.mongoProperties.getSecondary()));
    }

    @Bean
    @Primary
    public SimpleReactiveMongoDatabaseFactory primaryFactory(final MongoProperties mongo) {
        MongoClient client = MongoClients.create(mongo.getUri());
        return new SimpleReactiveMongoDatabaseFactory(client, mongo.getDatabase());
    }

    @Bean
    public SimpleReactiveMongoDatabaseFactory secondaryFactory(final MongoProperties mongo) {
        return new SimpleReactiveMongoDatabaseFactory(MongoClients.create(mongo.getUri()), mongo.getDatabase());
    }
@Configuration
@EnableMongoRepositories(basePackages = "nl.suitless.moduleservice.Data.repositories",
        mongoTemplateRef = "primaryReactiveMongoTemplate")
public class PrimaryReactiveMongoConfig {

}

same for the secondaryReactiveMongoConfig.

and

@ConfigurationProperties(prefix = "mongodb")
@Qualifier("MultipleMongo")
public class MultipleMongoProperties {

    private MongoProperties primary;
    private MongoProperties secondary;

    public MultipleMongoProperties(){
    }

    public MongoProperties getPrimary() {
        return primary;
    }

    public void setPrimary(MongoProperties primary) {
        this.primary = primary;
    }

    public MongoProperties getSecondary() {
        return secondary;
    }

    public void setSecondary(MongoProperties secondary) {
        this.secondary = secondary;
    }
}


Read more here: https://stackoverflow.com/questions/65709283/reactive-mongodb-multiple-database-connections

Content Attribution

This content was originally published by spoilerd do at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: