Example usage for com.mongodb MongoClient close

List of usage examples for com.mongodb MongoClient close

Introduction

In this page you can find the example usage for com.mongodb MongoClient close.

Prototype

public void close() 

Source Link

Document

Closes all resources associated with this instance, in particular any open network connections.

Usage

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

License:Apache License

private ServerAddress getServerAddress() {
    MongoClient client = client();
    try {/*from  w  ww. j ava2 s.c o  m*/
        return client.getServerAddressList().get(0);
    } finally {
        client.close();
    }
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

License:Apache License

private Optional<CommandResult> runDBCommand(String database, DBObject command) {
    MongoClient client = client();
    try {//  w  ww  .  j  ava  2  s. c om
        DB db = client.getDB(database);
        CommandResult status;
        try {
            status = db.command(command);
        } catch (MongoException e) {
            LOG.warn("Command " + command + " on " + getServerAddress() + " failed", e);
            return Optional.absent();
        }
        if (!status.ok()) {
            LOG.debug("Unexpected result of {} on {}: {}",
                    new Object[] { command, getServerAddress(), status.getErrorMessage() });
        }
        return Optional.of(status);
    } finally {
        client.close();
    }
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

License:Apache License

public long getShardCount() {
    MongoClient client = client();
    try {/*from  www. j a  v  a  2  s .co m*/
        return client.getDB("config").getCollection("shards").getCount();
    } finally {
        client.close();
    }
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

License:Apache License

/**
 * Java equivalent of calling rs.conf() in the console.
 *///  w w w.j a  v  a  2  s . c  om
private BSONObject getReplicaSetConfig() {
    MongoClient client = client();
    try {
        return client.getDB("local").getCollection("system.replset").findOne();
    } catch (MongoException e) {
        LOG.error("Failed to get replica set config on " + client, e);
        return null;
    } finally {
        client.close();
    }
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBTestHelper.java

License:Apache License

/**
 * Inserts a new object with { key: value } at given server.
 * @return The new document's id//from www  .j  av a2  s. c o  m
 */
public static String insert(AbstractMongoDBServer entity, String key, Object value) {
    LOG.info("Inserting {}:{} at {}", new Object[] { key, value, entity });
    MongoClient mongoClient = clientForServer(entity);
    try {
        DB db = mongoClient.getDB(TEST_DB);
        DBCollection testCollection = db.getCollection(TEST_COLLECTION);
        BasicDBObject doc = new BasicDBObject(key, value);
        testCollection.insert(doc);
        ObjectId id = (ObjectId) doc.get("_id");
        return id.toString();
    } finally {
        mongoClient.close();
    }
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBTestHelper.java

License:Apache License

/** @return The {@link DBObject} representing the object with the given id */
public static DBObject getById(AbstractMongoDBServer entity, String id) {
    LOG.info("Getting {} from {}", new Object[] { id, entity });
    MongoClient mongoClient = clientForServer(entity);
    // Secondary preferred means the driver will let us read from secondaries too.
    mongoClient.setReadPreference(ReadPreference.secondaryPreferred());
    try {// w  w  w  . ja  v  a  2s .  c  o m
        DB db = mongoClient.getDB(TEST_DB);
        DBCollection testCollection = db.getCollection(TEST_COLLECTION);
        return testCollection.findOne(new BasicDBObject("_id", new ObjectId(id)));
    } finally {
        mongoClient.close();
    }
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBTestHelper.java

License:Apache License

public static List<String> getDatabaseNames(AbstractMongoDBServer entity) {
    LOG.info("Getting database names from {}", entity);
    MongoClient mongoClient = clientForServer(entity);
    try {//from   ww w .j a v  a2 s  . com
        return mongoClient.getDatabaseNames();
    } finally {
        mongoClient.close();
    }
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBTestHelper.java

License:Apache License

public static boolean isConfigServer(AbstractMongoDBServer entity) {
    LOG.info("Checking if {} is a config server", entity);
    MongoClient mongoClient = clientForServer(entity);
    try {//from  w w  w .  j a  v  a  2 s  .  co m
        DB db = mongoClient.getDB(ADMIN_DB);
        CommandResult commandResult = db.command("getCmdLineOpts");
        Map<?, ?> parsedArgs = (Map<?, ?>) commandResult.get("parsed");
        if (parsedArgs == null)
            return false;
        Boolean configServer = (Boolean) parsedArgs.get("configsvr");
        if (configServer != null) {
            // v2.5 format
            return Boolean.TRUE.equals(configServer);
        } else {
            // v2.6 format
            String role = (String) ((Map) parsedArgs.get("sharding")).get("clusterRole");
            return "configsvr".equals(role);
        }
    } finally {
        mongoClient.close();
    }
}

From source file:ch.hslu.dmg.InitDB.java

public void initProfs() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("unidb");
    MongoCollection<Document> prof = database.getCollection("professoren");
    prof.drop();/*from w  w w. ja va 2 s.  c  om*/

    ArrayList<Document> profs = new ArrayList<>();
    profs.add(new Document("PersNr", 2125).append("Name", "Sokrates").append("Rang", "C4").append("Raum", 226));
    profs.add(new Document("PersNr", 2126).append("Name", "Russel").append("Rang", "C4").append("Raum", 232));
    profs.add(
            new Document("PersNr", 2127).append("Name", "Kopernikus").append("Rang", "C3").append("Raum", 310));
    profs.add(new Document("PersNr", 2133).append("Name", "Popper").append("Rang", "C3").append("Raum", 52));
    profs.add(
            new Document("PersNr", 2134).append("Name", "Augustinus").append("Rang", "C3").append("Raum", 309));
    profs.add(new Document("PersNr", 2136).append("Name", "Curie").append("Rang", "C4").append("Raum", 36));
    profs.add(new Document("PersNr", 2137).append("Name", "Kant").append("Rang", "C4").append("Raum", 7));
    prof.insertMany(profs);

    mongo.close();
}

From source file:ch.hslu.dmg.InitDB.java

public void initStuds() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("unidb");
    MongoCollection<Document> stud = database.getCollection("studenten");
    stud.drop();/*from w w w. j  av a 2  s  . c  om*/

    ArrayList<Document> studs = new ArrayList<>();
    studs.add(new Document("Legi", 24002).append("Name", "Xenokrates").append("Semester", 18));
    studs.add(
            new Document("Legi", 25403).append("Name", "Jonas").append("Semester", 12).append("Hoeren", 5022));
    studs.add(
            new Document("Legi", 26120).append("Name", "Fichte").append("Semester", 10).append("Hoeren", 5001));
    studs.add(new Document("Legi", 26830).append("Name", "Aristoxenos").append("Semester", 8));
    BasicDBList hoeren = new BasicDBList();
    hoeren.add(5001);
    hoeren.add(4052);
    studs.add(new Document("Legi", 27550).append("Name", "Schopenhauer").append("Semester", 6).append("Hoeren",
            hoeren));
    BasicDBList hoeren2 = new BasicDBList();
    hoeren2.add(5041);
    hoeren2.add(5052);
    hoeren2.add(5216);
    hoeren2.add(5259);
    studs.add(new Document("Legi", 28106).append("Name", "Carnap").append("Semester", 3).append("Hoeren",
            hoeren2));
    BasicDBList hoeren3 = new BasicDBList();
    hoeren3.add(5001);
    hoeren3.add(5041);
    hoeren3.add(5049);
    studs.add(new Document("Legi", 29120).append("Name", "Theophrastos").append("Semester", 2).append("Hoeren",
            hoeren3));
    BasicDBList hoeren4 = new BasicDBList();
    hoeren4.add(5022);
    hoeren4.add(5001);
    studs.add(new Document("Legi", 29555).append("Name", "Feuerbach").append("Semester", 2).append("Hoeren",
            hoeren4));
    stud.insertMany(studs);

    mongo.close();
}