Example usage for com.mongodb Mongo getConnectPoint

List of usage examples for com.mongodb Mongo getConnectPoint

Introduction

In this page you can find the example usage for com.mongodb Mongo getConnectPoint.

Prototype

@Deprecated
@Nullable
public String getConnectPoint() 

Source Link

Document

Gets a String representation of current connection point, i.e.

Usage

From source file:com.tomtom.speedtools.tracer.mongo.MongoDBTraceHandler.java

License:Apache License

@Nonnull
static DBCollection getDBCollection(@Nonnull final String servers, @Nonnull final String database,
        @Nonnull final String userName, @Nonnull final String password, final int sizeMB,
        final int connectTimeoutMsecs) throws UnknownHostException {
    assert servers != null;
    assert database != null;
    assert userName != null;
    assert password != null;
    assert connectTimeoutMsecs >= 0;

    LOG.info("getDBCollection: Creating MongoDB connection for traces: {}", servers);
    final Mongo mongo = MongoConnectionCache.getMongoDB(servers, connectTimeoutMsecs, userName, database,
            password);/* w w  w.j  a v  a 2 s .  c  o  m*/

    // If this is a replica set, set read preference to secondary for traces.
    final List<ServerAddress> serverAddressList = mongo.getServerAddressList();
    if (serverAddressList.size() > 1) {
        mongo.setReadPreference(ReadPreference.secondary());
    }

    // Should writes fail, then don't throw exceptions, just ignore.
    // We care more about not disturbing the primary system then our own (traces) integrity.
    mongo.setWriteConcern(WriteConcern.UNACKNOWLEDGED);

    // The connection point may actually be null... Not an error.
    final String connectPoint = mongo.getConnectPoint();
    LOG.info("getDBCollection: MongoDB connection for traces established: '{}' at {}", database, connectPoint);

    final DB db = mongo.getDB(database);
    final DBObject options = new BasicDBObject();
    options.put("capped", true);
    options.put("size", sizeMB * MEGABYTE);

    final DBCollection collection;
    if (!db.getCollectionNames().contains(Constants.COLLECTION_NAME)) {
        collection = db.createCollection(Constants.COLLECTION_NAME, options);
    } else {
        collection = db.getCollection(Constants.COLLECTION_NAME);
    }

    return collection;
}