Example usage for com.mongodb ReadPreference primaryPreferred

List of usage examples for com.mongodb ReadPreference primaryPreferred

Introduction

In this page you can find the example usage for com.mongodb ReadPreference primaryPreferred.

Prototype

public static ReadPreference primaryPreferred() 

Source Link

Document

Gets a read preference that forces reads to the primary if available, otherwise to a secondary.

Usage

From source file:com.ott.bookings.auth.form.impl.MongoTockenStore.java

License:Apache License

/**
 * Create the {@link MongoClient}./*w  w  w. jav  a2  s.c  om*/
 * 
 */
private void getConnection() {
    try {

        /* create the client using the Mongo options */
        ReadPreference readPreference = ReadPreference.primaryPreferred();
        if (this.useSlaves) {
            readPreference = ReadPreference.secondaryPreferred();
        }
        MongoClientOptions options = MongoClientOptions.builder().connectTimeout(connectionTimeoutMs)
                .maxWaitTime(connectionWaitTimeoutMs).connectionsPerHost(maxPoolSize).writeConcern(writeConcern)
                .readPreference(readPreference).build();

        log.info("[Mongo Token Store]: Connecting to MongoDB [" + this.hostsString + "]");

        /* connect */
        this.mongoClient = new MongoClient(getServerAddresses(), options);

        /* get a connection to our db */
        log.info("[Mongo Token Store]: Using Database [" + this.dbName + "]");
        this.db = this.mongoClient.getDB(this.dbName);
        /* get a reference to the collection */
        this.collection = this.db.getCollection(this.collectionName);

        log.info("[Mongo Token Store]: Store ready.");
    } catch (UnknownHostException uhe) {
        log.error("Unable to Connect to MongoDB", uhe);
    } catch (MongoException me) {
        log.error("Unable to Connect to MongoDB", me);
    }
}

From source file:com.redhat.lightblue.mongo.config.MongoReadPreference.java

License:Open Source License

public static ReadPreference parse(String value) {
    value = value.trim();//from  w ww .  j  a v  a 2s  .c  o m
    int paren = value.indexOf('(');
    String pref;
    List<TagSet> tags;
    if (paren != -1) {
        pref = value.substring(0, paren).trim();
        String argsStr = value.substring(paren + 1).trim();
        if (!argsStr.endsWith(")")) {
            throw new InvalidReadPreference(value);
        }
        tags = parseArgs(argsStr.substring(0, argsStr.length() - 1));
    } else {
        pref = value;
        tags = null;
    }
    switch (pref) {
    case READ_PREFERENCE_NEAREST:
        if (tags == null) {
            return ReadPreference.nearest();
        } else {
            return ReadPreference.nearest(tags);
        }
    case READ_PREFERENCE_PRIMARY:
        return ReadPreference.primary();
    case READ_PREFERENCE_PRIMARY_PREFERRED:
        if (tags == null) {
            return ReadPreference.primaryPreferred();
        } else {
            return ReadPreference.primaryPreferred(tags);
        }
    case READ_PREFERENCE_SECONDARY:
        if (tags == null) {
            return ReadPreference.secondary();
        } else {
            return ReadPreference.secondary(tags);
        }
    case READ_PREFERENCE_SECONDARY_PREFERRED:
        if (tags == null) {
            return ReadPreference.secondaryPreferred();
        } else {
            return ReadPreference.secondaryPreferred(tags);
        }
    default:
        throw new InvalidReadPreference(value);
    }
}

From source file:com.ricardolorenzo.identity.user.impl.UserIdentityManagerMongoDB.java

License:Open Source License

public UserIdentityManagerMongoDB(final Properties conf) throws UnknownHostException {
    this.properties = conf;

    this.databaseUsers = new Boolean(this.properties.getProperty("mongodb.databaseUsers", "true"));

    WriteConcern writeConcern = WriteConcern.MAJORITY;
    String writeConcernType = conf.getProperty("mongodb.writeConcern", "majority").toLowerCase();
    if ("majority".equals(writeConcernType)) {
        writeConcern = WriteConcern.UNACKNOWLEDGED;
    } else if ("unacknowledged".equals(writeConcernType)) {
        writeConcern = WriteConcern.UNACKNOWLEDGED;
    } else if ("acknowledged".equals(writeConcernType)) {
        writeConcern = WriteConcern.ACKNOWLEDGED;
    } else if ("journaled".equals(writeConcernType)) {
        writeConcern = WriteConcern.JOURNALED;
    } else if ("replica_acknowledged".equals(writeConcernType)) {
        writeConcern = WriteConcern.REPLICA_ACKNOWLEDGED;
    }//from  w  w w  .  j  av a2 s .  co  m

    ReadPreference readPreference = null;
    String readPreferenceType = conf.getProperty("mongodb.readPreference", "primary").toLowerCase();
    if ("primary".equals(readPreferenceType)) {
        readPreference = ReadPreference.primary();
    } else if ("primary_preferred".equals(readPreferenceType)) {
        readPreference = ReadPreference.primaryPreferred();
    } else if ("secondary".equals(readPreferenceType)) {
        readPreference = ReadPreference.secondary();
    } else if ("secondary_preferred".equals(readPreferenceType)) {
        readPreference = ReadPreference.secondaryPreferred();
    } else if ("nearest".equals(readPreferenceType)) {
        readPreference = ReadPreference.nearest();
    }

    MongoClientOptions.Builder options = MongoClientOptions.builder();
    options.writeConcern(writeConcern);
    options.readPreference(readPreference);
    try {
        options.connectionsPerHost(Integer.parseInt(conf.getProperty("mongodb.threads", "100")));
    } catch (NumberFormatException e) {
        options.connectionsPerHost(100);
    }

    MongoClientURI mongoClientURI = new MongoClientURI(
            conf.getProperty("mongodb.url", "mongodb://localhost:27017"), options);
    if (!this.properties.containsKey("mongodb.database")) {
        if (mongoClientURI.getDatabase() != null && !mongoClientURI.getDatabase().isEmpty()) {
            this.properties.setProperty("mongodb.database", mongoClientURI.getDatabase());
        } else {
            this.properties.setProperty("mongodb.database", DEFAULT_DATABASE);
        }
    }
    mongoClient = new MongoClient(mongoClientURI);
}

From source file:com.softinstigate.restheart.db.MongoDBClientSingleton.java

License:Open Source License

private void setup() throws UnknownHostException {
    if (initialized) {
        List<ServerAddress> servers = new ArrayList<>();
        List<MongoCredential> credentials = new ArrayList<>();

        for (Map<String, Object> mongoServer : mongoServers) {
            Object mongoHost = mongoServer.get(Configuration.MONGO_HOST_KEY);
            Object mongoPort = mongoServer.get(Configuration.MONGO_PORT_KEY);

            if (mongoHost != null && mongoHost instanceof String && mongoPort != null
                    && mongoPort instanceof Integer) {
                servers.add(new ServerAddress((String) mongoHost, (int) mongoPort));
            }//from  w w  w.j  ava2s .  c om
        }

        if (mongoCredentials != null) {
            for (Map<String, Object> mongoCredential : mongoCredentials) {
                Object mongoAuthDb = mongoCredential.get(Configuration.MONGO_AUTH_DB_KEY);
                Object mongoUser = mongoCredential.get(Configuration.MONGO_USER_KEY);
                Object mongoPwd = mongoCredential.get(Configuration.MONGO_PASSWORD_KEY);

                if (mongoAuthDb != null && mongoAuthDb instanceof String && mongoUser != null
                        && mongoUser instanceof String && mongoPwd != null && mongoPwd instanceof String) {
                    credentials.add(MongoCredential.createMongoCRCredential((String) mongoUser,
                            (String) mongoAuthDb, ((String) mongoPwd).toCharArray()));
                }
            }
        }

        MongoClientOptions opts = MongoClientOptions.builder().readPreference(ReadPreference.primaryPreferred())
                .writeConcern(WriteConcern.ACKNOWLEDGED).build();

        mongoClient = new MongoClient(servers, credentials, opts);
    }
}

From source file:com.stratio.connector.mongodb.core.configuration.MongoClientConfiguration.java

License:Apache License

/**
 * Convert the Mongo connector string option to the appropriate Read Preference.
 *
 * @param readSetting//  w  w w .jav  a  2 s.c om
 *            the read preference string setting
 * @return the read preference.
 * @throws MongoValidationException
 *             if the value cannot be parsed to a ReadPreference
 */
private ReadPreference settingToReadPreference(String readSetting) throws MongoValidationException {
    ReadPreference readPreference = null;
    switch (readSetting.trim().toLowerCase()) {
    case "primary":
        readPreference = ReadPreference.primary();
        break;
    case "primarypreferred":
        readPreference = ReadPreference.primaryPreferred();
        break;
    case "secondary":
        readPreference = ReadPreference.secondary();
        break;
    case "secondarypreferred":
        readPreference = ReadPreference.secondaryPreferred();
        break;
    case "nearest":
        readPreference = ReadPreference.nearest();
        break;
    default:
        throw new MongoValidationException("Read preference " + readSetting + " is not a legal value");
    }
    return readPreference;

}

From source file:fr.gouv.vitam.mdbes.MainErazeData.java

License:Open Source License

/**
 * Will save to a File and to ElasticSearch, then to MongoDB
 *
 * @param args/*from   w  w w  . ja v a 2s .  c o  m*/
 *            logfile eraze/noeraze host database escluster unicast start nbload file fileout limitdepth nbThread mongoimport
 *            stopindex/xx
 *
 *            <ol>
 *            <li>logfile = Log4J configuration log file</li>
 *            <li>eraze/noeraze = eraze will delete all data in DB (!), else nothing is done</li>
 *            <li>host = MongoDB host</li>
 *            <li>database = MongoDB database name as VitamLinks</li>
 *            <li>escluster = ElasticSearch cluster name</li>
 *            <li>unicast = ElasticSearch unicast servers list (as in "mdb001, mdb002, mdb003")</li>
 *            </ol>
 */
public static void main(final String[] args) throws Exception {
    if (args.length < 6) {
        System.err.println("need: logfile eraze/noeraze host database escluster unicast");
        return;
    }
    final String log4j = args[0];
    PropertyConfigurator.configure(log4j);
    VitamLoggerFactory.setDefaultFactory(new LogbackLoggerFactory(VitamLogLevel.WARN));
    LOGGER = VitamLoggerFactory.getInstance(MainErazeData.class);
    MongoDbAccess dbvitam = null;
    try {
        if (args.length > 1) {
            eraze = args[1].equals("eraze");
        }
        if (simulate) {
            eraze = false;
        }
        final String networkHost = "192.168.56.102";
        GlobalDatas.localNetworkAddress = networkHost;
        // connect to the local database server
        if (args.length > 2) {
            host = args[2];
        }
        if (args.length > 3) {
            database = args[3];
        }
        if (args.length > 4) {
            esbase = args[4];
        }
        if (args.length > 5) {
            unicast = args[5];
        }
        LOGGER.debug("Start with " + eraze + ":" + host + ":" + database + ":" + esbase + ":" + unicast);
        MAXTHREAD += nbThread;
        final MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(MAXTHREAD)
                .build();
        mongoClient = new MongoClient(host, options);
        mongoClient.setReadPreference(ReadPreference.primaryPreferred());
        dbvitam = new MongoDbAccess(mongoClient, database, esbase, unicast, eraze);
        // get a list of the collections in this database and print them out
        LOGGER.debug(dbvitam.toString());
        if (eraze) {
            reinit(dbvitam);
            LOGGER.warn(dbvitam.toString());
            return;
        }
        return;
    } catch (Exception e) {
        LOGGER.error(e);
    } finally {
        // release resources
        final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
        final ToClean toclean = new ToClean(dbvitam);
        scheduler.schedule(toclean, 1, TimeUnit.MILLISECONDS);
        final ToShutdown toShutdown = new ToShutdown();
        scheduler.schedule(toShutdown, 5000, TimeUnit.MILLISECONDS);
        scheduler.awaitTermination(7000, TimeUnit.MILLISECONDS);
        System.exit(0);
    }

}

From source file:fr.gouv.vitam.mdbes.MainIngestESFromFile.java

License:Open Source License

/**
 * @param args//  ww  w.j a  v a  2s . c  om
 */
public static void main(final String[] args) throws Exception {
    if (args.length < 6) {
        System.err.println("need: logfile host escluster unicast indextype files");
        return;
    }
    final String log4j = args[0];
    PropertyConfigurator.configure(log4j);
    final String networkHost = "192.168.56.102";
    GlobalDatas.localNetworkAddress = networkHost;
    // connect to the local database server
    if (args.length > 1) {
        host = args[1];
    }
    if (args.length > 2) {
        esbase = args[2];
    }
    if (args.length > 3) {
        unicast = args[3];
    }
    if (args.length > 4) {
        model = args[4];
    }
    MongoDbAccess dbvitam = null;
    try {
        final MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(4).build();
        mongoClient = new MongoClient(host, options);
        mongoClient.setReadPreference(ReadPreference.primaryPreferred());
        dbvitam = new MongoDbAccess(mongoClient, "VitamLinks", esbase, unicast, false);
        dbvitam.updateEsIndex(model);
        MainIngestESFromFile.loadt = new AtomicLong(0);
        MainIngestFile.cptMaip.set(0);
        for (int i = 5; i < args.length - 1; i++) {
            System.out.println("Load " + args[i]);
            runOnce(dbvitam, args[i], true);
        }
        int i = args.length - 1;
        System.out.println("Load " + args[i]);
        runOnce(dbvitam, args[i], false);
    } catch (final Exception e) {
        System.err.println("ERROR: " + e.getMessage());
        e.printStackTrace();

    } finally {
        // release resources
        final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
        final ToClean toclean = new ToClean(dbvitam);
        scheduler.schedule(toclean, 1, TimeUnit.MILLISECONDS);
        final ToShutdown toShutdown = new ToShutdown();
        scheduler.schedule(toShutdown, 5000, TimeUnit.MILLISECONDS);
        scheduler.awaitTermination(7000, TimeUnit.MILLISECONDS);
        System.exit(0);
    }

}

From source file:fr.gouv.vitam.mdbes.MainIngestFile.java

License:Open Source License

/**
 * Will save to a File and to ElasticSearch, then to MongoDB
 *
 * @param args/*from  w w  w . ja  v  a2 s.  c  om*/
 *            logfile eraze/noeraze host database escluster unicast start nbload file fileout limitdepth nbThread mongoimport
 *            stopindex/xx
 *
 *            <ol>
 *            <li>logfile = Log4J configuration log file</li>
 *            <li>noeraze/index = index will (re)create index, else nothing is done</li>
 *            <li>host = MongoDB host</li>
 *            <li>database = MongoDB database name as VitamLinks</li>
 *            <li>escluster = ElasticSearch cluster name</li>
 *            <li>unicast = ElasticSearch unicast servers list (as in "mdb001, mdb002, mdb003")</li>
 *            <li>start = start index in the bench (will be for instance between 1-1000 start from 100)</li>
 *            <li>nbload = number of iteration from start</li>
 *            <li>file = ingest file</li>
 *            <li>fileout = output saved</li>
 *            <li>limitdepth = from which level the output is saved to the file and not to MongoDB</li>
 *            <li>nbThread = number of thread (default 1)</li>
 *            <li>mongoimport = optional command for import</li>
 *            <li>stopindex/xx = shall we stop index during import in MongoDB</li>
 *            </ol>
 */
public static void main(final String[] args) throws Exception {
    if (args.length < 6) {
        System.err.println(
                "need: logfile noeraze/index host database escluster unicast start nbload file fileout limitdepth mongoimport stopindex/xx nbThread");
        // System.err.println("before was need: logfile nbload files eraze/noeraze start host escluster unicast fileout limitdepth mongoimport 0/1 (1=stop index)");
        return;
    }
    final String log4j = args[0];
    PropertyConfigurator.configure(log4j);
    VitamLoggerFactory.setDefaultFactory(new LogbackLoggerFactory(VitamLogLevel.WARN));
    LOGGER = VitamLoggerFactory.getInstance(MainIngestFile.class);
    boolean reindex = false;
    if (args.length > 1) {
        reindex = args[1].equals("index");
    }
    if (simulate) {
        reindex = false;
    }
    final String networkHost = "192.168.56.102";
    GlobalDatas.localNetworkAddress = networkHost;
    // connect to the local database server
    if (args.length > 2) {
        host = args[2];
    }
    if (args.length > 3) {
        database = args[3];
    }
    if (args.length > 4) {
        esbase = args[4];
    }
    if (args.length > 5) {
        unicast = args[5];
    }
    int realnb = -1;
    if (args.length > 6) {
        startFrom = Integer.parseInt(args[6]);
    }
    if (args.length > 7) {
        realnb = Integer.parseInt(args[7]);
    }
    if (args.length > 8) {
        ingest = FileUtil.readFile(args[8]);
    }
    if (args.length > 9) {
        fileout = args[9];
    }
    if (args.length > 10) {
        final int stoplevel = Integer.parseInt(args[10]);
        minleveltofile = stoplevel;
    }
    if (args.length > 11) {
        nbThread = Integer.parseInt(args[11]);
    }
    if (args.length > 12) {
        commandMongo = args[12];
    }
    boolean stopindex = false;
    if (args.length > 13) {
        stopindex = args[13].equalsIgnoreCase("stopindex");
    }
    LOGGER.debug("Start with " + reindex + ":" + host + ":" + database + ":" + esbase + ":" + unicast);
    if (args.length > 6) {
        LOGGER.debug("and " + startFrom + ":" + realnb + ":" + ingest + ":" + fileout + ":" + minleveltofile
                + ":" + nbThread + ":" + commandMongo + ":" + stopindex);
    }
    MongoDbAccess dbvitam = null;
    try {
        MAXTHREAD += nbThread;
        final MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(MAXTHREAD)
                .build();
        mongoClient = new MongoClient(host, options);
        mongoClient.setReadPreference(ReadPreference.primaryPreferred());
        dbvitam = new MongoDbAccess(mongoClient, database, esbase, unicast, reindex);
        // get a list of the collections in this database and print them out
        LOGGER.debug(dbvitam.toString());
        if (realnb < 0) {
            return;
        }
        // drop all the data in it
        final ParserIngest parser = new ParserIngest(true);
        parser.parse(ingest);
        model = parser.getModel();
        if (reindex) {
            LOGGER.debug("ensureIndex");
            dbvitam.ensureIndex();
            if (model != null) {
                LOGGER.debug("updateEsIndex");
                dbvitam.updateEsIndex(model);
            }
            LOGGER.debug("end Index");
        }
        LOGGER.warn(dbvitam.toString());

        final int stepnb = realnb;
        nb = stepnb;
        loadt = new AtomicLong(0);
        cptMaip.set(0);
        runOnce(dbvitam);
        // Continue with MongoDB Loading if setup
        if (commandMongo != null) {
            System.out.println("Launch MongoImport");
            runOnceMongo(dbvitam, stopindex);
        }
    } catch (Exception e) {
        LOGGER.error(e);
    } finally {
        // release resources
        final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
        final ToClean toclean = new ToClean(dbvitam);
        scheduler.schedule(toclean, 1, TimeUnit.MILLISECONDS);
        final ToShutdown toShutdown = new ToShutdown();
        scheduler.schedule(toShutdown, 5000, TimeUnit.MILLISECONDS);
        scheduler.awaitTermination(7000, TimeUnit.MILLISECONDS);
        System.exit(0);
    }

}

From source file:fr.gouv.vitam.mdbes.MainIngestMDBESFromFile.java

License:Open Source License

/**
 * @param args/*from   ww w . j  a va  2 s. com*/
 */
public static void main(final String[] args) throws Exception {
    if (args.length < 6) {
        System.err.println("need: logfile host database escluster unicast files");
        return;
    }
    final String networkHost = "192.168.56.102";
    GlobalDatas.localNetworkAddress = networkHost;
    final String log4j = args[0];
    PropertyConfigurator.configure(log4j);
    VitamLoggerFactory.setDefaultFactory(new LogbackLoggerFactory(VitamLogLevel.WARN));
    LOGGER = VitamLoggerFactory.getInstance(MainIngestMDBESFromFile.class);
    // connect to the local database server
    if (args.length > 1) {
        host = args[1];
    }
    if (args.length > 2) {
        database = args[2];
    }
    if (args.length > 3) {
        esbase = args[3];
    }
    if (args.length > 4) {
        unicast = args[4];
    }
    if (args.length > 5) {
        model = args[5];
    }
    if (args.length > 6) {
        ingest = new String[args.length - 6];
        for (int i = 0; i < ingest.length; i++) {
            ingest[i] = args[6 + i];
        }
    }
    LOGGER.warn("Start with " + ingest + ":" + host + ":" + database + ":" + esbase + ":" + unicast);

    MongoDbAccess dbvitam = null;
    try {
        final MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(10).build();
        mongoClient = new MongoClient(host, options);
        mongoClient.setReadPreference(ReadPreference.primaryPreferred());
        dbvitam = new MongoDbAccess(mongoClient, database, esbase, unicast, true);
        dbvitam.ensureIndex();
        LOGGER.warn(dbvitam.toString());

        MainIngestMDBESFromFile.loadt = new AtomicLong(0);
        MainIngestFile.cptMaip.set(0);

        runOnce(dbvitam);

    } catch (final Exception e) {
        System.err.println("ERROR: " + e.getMessage());
        e.printStackTrace();

    } finally {
        // release resources
        final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
        final ToClean toclean = new ToClean(dbvitam);
        scheduler.schedule(toclean, 1, TimeUnit.MILLISECONDS);
        final ToShutdown toShutdown = new ToShutdown();
        scheduler.schedule(toShutdown, 5000, TimeUnit.MILLISECONDS);
        scheduler.awaitTermination(7000, TimeUnit.MILLISECONDS);
        System.exit(0);
    }

}

From source file:fr.gouv.vitam.mdbes.MainIngestMDBFromFile.java

License:Open Source License

/**
 * @param args//from  w w  w  .j  a  v a  2  s .c  om
 */
public static void main(final String[] args) throws Exception {
    if (args.length < 6) {
        System.err.println("need: logfile host database escluster unicast files");
        return;
    }
    final String networkHost = "192.168.56.102";
    GlobalDatas.localNetworkAddress = networkHost;
    final String log4j = args[0];
    PropertyConfigurator.configure(log4j);
    VitamLoggerFactory.setDefaultFactory(new LogbackLoggerFactory(VitamLogLevel.WARN));
    LOGGER = VitamLoggerFactory.getInstance(MainIngestMDBFromFile.class);
    // connect to the local database server
    if (args.length > 1) {
        host = args[1];
    }
    if (args.length > 2) {
        database = args[2];
    }
    if (args.length > 3) {
        esbase = args[3];
    }
    if (args.length > 4) {
        unicast = args[4];
    }
    if (args.length > 5) {
        ingest = new String[args.length - 5];
        for (int i = 0; i < ingest.length; i++) {
            ingest[i] = args[5 + i];
        }
    }
    LOGGER.warn("Start with " + ingest + ":" + host + ":" + database + ":" + esbase + ":" + unicast);

    MongoDbAccess dbvitam = null;
    try {
        final MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(4).build();
        mongoClient = new MongoClient(host, options);
        mongoClient.setReadPreference(ReadPreference.primaryPreferred());
        dbvitam = new MongoDbAccess(mongoClient, database, esbase, unicast, true);
        dbvitam.ensureIndex();
        LOGGER.warn(dbvitam.toString());

        MainIngestMDBFromFile.loadt = new AtomicLong(0);
        MainIngestFile.cptMaip.set(0);

        runOnce(dbvitam);

    } catch (final Exception e) {
        System.err.println("ERROR: " + e.getMessage());
        e.printStackTrace();

    } finally {
        // release resources
        final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
        final ToClean toclean = new ToClean(dbvitam);
        scheduler.schedule(toclean, 1, TimeUnit.MILLISECONDS);
        final ToShutdown toShutdown = new ToShutdown();
        scheduler.schedule(toShutdown, 5000, TimeUnit.MILLISECONDS);
        scheduler.awaitTermination(7000, TimeUnit.MILLISECONDS);
        System.exit(0);
    }

}