Example usage for com.mongodb MongoClientOptions getReadPreference

List of usage examples for com.mongodb MongoClientOptions getReadPreference

Introduction

In this page you can find the example usage for com.mongodb MongoClientOptions getReadPreference.

Prototype

public ReadPreference getReadPreference() 

Source Link

Document

The read preference to use for queries, map-reduce, aggregation, and count.

Default is ReadPreference.primary() .

Usage

From source file:com.bnrc.sdn.properties.MongoProperties.java

License:Apache License

private Builder builder(MongoClientOptions options) {
    Builder builder = MongoClientOptions.builder();
    if (options != null) {
        builder.alwaysUseMBeans(options.isAlwaysUseMBeans());
        builder.connectionsPerHost(options.getConnectionsPerHost());
        builder.connectTimeout(options.getConnectTimeout());
        builder.cursorFinalizerEnabled(options.isCursorFinalizerEnabled());
        builder.dbDecoderFactory(options.getDbDecoderFactory());
        builder.dbEncoderFactory(options.getDbEncoderFactory());
        builder.description(options.getDescription());
        builder.maxWaitTime(options.getMaxWaitTime());
        builder.readPreference(options.getReadPreference());
        builder.socketFactory(options.getSocketFactory());
        builder.socketKeepAlive(options.isSocketKeepAlive());
        builder.socketTimeout(options.getSocketTimeout());
        builder.threadsAllowedToBlockForConnectionMultiplier(
                options.getThreadsAllowedToBlockForConnectionMultiplier());
        builder.writeConcern(options.getWriteConcern());
    }/*from  w  ww. j ava  2s  .c om*/
    return builder;
}

From source file:com.navercorp.pinpoint.plugin.mongo.interceptor.MongoDriverConnectInterceptor3_0.java

License:Apache License

private String getReadPreference(Object arg) {
    if (!(arg instanceof MongoClientOptions)) {
        return null;
    }// w ww  .  j  a  v a2  s  .  c  om

    final MongoClientOptions mongoClientOptions = (MongoClientOptions) arg;

    return mongoClientOptions.getReadPreference().getName();
}

From source file:com.ultratechnica.mongodb.Main.java

License:Apache License

public static void main(String[] args) {

    log.info("=============================================================================================\n"
            + "                       _   _     _                 \n"
            + " |\\/|  _  ._   _   _  | \\ |_)   |_)  _  ._   _ |_  \n"
            + " |  | (_) | | (_| (_) |_/ |_)   |_) (/_ | | (_ | | \n"
            + "               _|                                  \n"
            + "Copyright 2013 Keith Bishop, Ultratechnica Ltd, http://ultratechnica.com\n"
            + "Licensed to Apache "
            + "=============================================================================================\n");
    Options options = getOptions();/*from   w ww  .  j ava2 s . co m*/

    CommandLine commandLine = parseArgs(args, options);

    MongoClient client = null;

    if (commandLine.hasOption("host") && commandLine.hasOption("port")) {

        String host = commandLine.getOptionValue("host");
        String port = commandLine.getOptionValue("port");

        try {

            client = new MongoClient(host, Integer.parseInt(port));
        } catch (UnknownHostException e) {
            log.error("Unable to connect to host [{}] on port [{}]", host, port);
        }
    } else if (options.hasOption("host")) {

        String host = commandLine.getOptionValue("host");

        try {
            client = new MongoClient(host);
        } catch (UnknownHostException e) {
            log.error("Unable to connect to host [{}] on default port ()", host);
        }
    } else {

        try {
            client = new MongoClient();
        } catch (UnknownHostException e) {
            log.error("Unable to connect default host ({}) on default port ()", DEFAULT_HOST, DEFAULT_PORT);
        }
    }

    if (client == null) {
        System.out.println("Exiting, due to previous connection errors");
        System.exit(1);
    }

    ServerAddress clientAddress = client.getAddress();
    log.info("Connected to MongoDB [{}]", clientAddress);

    MongoClientOptions mongoClientOptions = client.getMongoClientOptions();

    log.info("=============================================================================================\n"
            + "Using Mongo Client options:\n" + "\tConnections per host: "
            + mongoClientOptions.getConnectionsPerHost() + "\n" + "\tConect timeout: "
            + mongoClientOptions.getConnectTimeout() + " \n" + "\tSocket timeout: "
            + mongoClientOptions.getSocketTimeout() + "\n" + "\tMax Wait time: "
            + mongoClientOptions.getMaxWaitTime() + "\n" + "\tMax Auto connect retry time: "
            + mongoClientOptions.getMaxAutoConnectRetryTime() + "\n"
            + "\tMax threads allowed to block for conneciton multipler: "
            + mongoClientOptions.getThreadsAllowedToBlockForConnectionMultiplier() + "\n" + "\tWrite concern: "
            + mongoClientOptions.getWriteConcern() + "\n" + "\tRead Preference: "
            + mongoClientOptions.getReadPreference() + "\n"
            + "=============================================================================================\n");

    String items = commandLine.getOptionValue("n");

    int numberOfItems = 0;
    try {
        numberOfItems = Integer.parseInt(items);
    } catch (NumberFormatException e) {
        log.error("The parameter provided for -n was not an integer [{}]", items);
        System.exit(1);
    }

    DB local = client.getDB("local");

    DBCollection collection = local.getCollection(DEFAULT_COLLECTION_NAME);

    log.info("Starting benchmark, inserting [{}] items into collection [{}]", numberOfItems,
            DEFAULT_COLLECTION_NAME);

    long startTime = System.currentTimeMillis();

    for (int i = 0; i < numberOfItems; i++) {

        BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().add("timestamp", new Date())
                .add("field1", "123456").add("field2", "2345678")
                .add("field3", "123123231313131232131231231231123132123123123").add("field4", true);

        WriteResult result = collection.insert(builder.get());
        if (!result.getLastError().ok()) {
            log.error("An error occurred [{}]", result.getLastError());
        }
    }

    long endTime = System.currentTimeMillis();

    log.info("Finished benchmarking.");
    long timeTakenMillis = endTime - startTime;

    float timeTaken = (float) timeTakenMillis / 1000;

    log.info("Results:\n\n" + String.format("%-25s %d", "Number of Items inserted:", numberOfItems) + "\n"
            + String.format("%-25s %.2f", "Time elapsed:", timeTaken) + " seconds\n"
            + String.format("%-25s %.2f", "Throughput:", numberOfItems / timeTaken) + " items/sec\n");

    log.info("Removing test data...");
    collection.remove(new BasicDBObject());
    log.info("Cleared collection [test]");
}

From source file:org.apache.jackrabbit.oak.plugins.document.util.MongoConnection.java

License:Apache License

public static String toString(MongoClientOptions opts) {
    return Objects.toStringHelper(opts).add("connectionsPerHost", opts.getConnectionsPerHost())
            .add("connectTimeout", opts.getConnectTimeout()).add("socketTimeout", opts.getSocketTimeout())
            .add("socketKeepAlive", opts.isSocketKeepAlive()).add("maxWaitTime", opts.getMaxWaitTime())
            .add("threadsAllowedToBlockForConnectionMultiplier",
                    opts.getThreadsAllowedToBlockForConnectionMultiplier())
            .add("readPreference", opts.getReadPreference().getName())
            .add("writeConcern", opts.getWriteConcern()).toString();
}