All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.craftercms.commons.mongo.MongoClientOptionsFactory Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
/*
 * Copyright (C) 2007-2020 Crafter Software Corporation. All Rights Reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package org.craftercms.commons.mongo;

import com.mongodb.MongoClientOptions;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;

import org.springframework.beans.factory.config.AbstractFactoryBean;

/**
 * Spring helper for Create a MongoClient
 */
public class MongoClientOptionsFactory extends AbstractFactoryBean {

    private static final int PRIMARY_READ_PREFERENCE = 1;
    private static final int NEAREST_READ_PREFERENCE = 2;
    private static final int SECONDARY_READ_PREFERENCE = 3;
    private int readPreference;
    private boolean alwaysUseMBeans;
    private int connectionsPerHost;
    private boolean cursorFinalizerEnabled;
    private int connectTimeout;
    private int maxWaitTime;
    private String writeConcern;
    private int threadsAllowedToBlockForConnectionMultiplier;

    public void setReadPreference(final int readPreference) {
        this.readPreference = readPreference;
    }

    public void setAlwaysUseMBeans(final boolean alwaysUseMBeans) {
        this.alwaysUseMBeans = alwaysUseMBeans;
    }

    public void setConnectionsPerHost(final int connectionsPerHost) {
        this.connectionsPerHost = connectionsPerHost;
    }

    public void setCursorFinalizerEnabled(final boolean cursorFinalizerEnabled) {
        this.cursorFinalizerEnabled = cursorFinalizerEnabled;
    }

    public void setConnectTimeout(final int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public void setMaxWaitTime(final int maxWaitTime) {
        this.maxWaitTime = maxWaitTime;
    }

    public void setWriteConcern(final String writeConcern) {
        this.writeConcern = writeConcern;
    }

    public void setThreadsAllowedToBlockForConnectionMultiplier(final int
                                                                    threadsAllowedToBlockForConnectionMultiplier) {
        this.threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier;
    }

    @Override
    public Class getObjectType() {
        return MongoClientOptions.class;
    }

    @Override
    protected MongoClientOptions createInstance() throws Exception {
        MongoClientOptions.Builder builder = MongoClientOptions.builder();
        builder.alwaysUseMBeans(this.alwaysUseMBeans);
        builder.connectionsPerHost(this.connectionsPerHost);
        builder.cursorFinalizerEnabled(this.cursorFinalizerEnabled);
        builder.connectTimeout(this.connectTimeout);
        builder.maxWaitTime(this.maxWaitTime);


        switch (this.readPreference) {
            case PRIMARY_READ_PREFERENCE:
                builder.readPreference(ReadPreference.primary());
                break;
            case NEAREST_READ_PREFERENCE:
                builder.readPreference(ReadPreference.nearest());
                break;
            case SECONDARY_READ_PREFERENCE:
                builder.readPreference(ReadPreference.secondary());
                break;
            default:
                builder.readPreference(ReadPreference.primary());
                break;
        }
        builder.writeConcern(WriteConcern.valueOf(this.writeConcern));
        builder.threadsAllowedToBlockForConnectionMultiplier(this.threadsAllowedToBlockForConnectionMultiplier);
        return builder.build();

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy