Example usage for com.mongodb Mongo Mongo

List of usage examples for com.mongodb Mongo Mongo

Introduction

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

Prototype

Mongo(final MongoClientURI mongoURI) 

Source Link

Usage

From source file:com.appleframework.monitor.model.Project.java

License:Open Source License

public MongoTemplate fetchMongoTemplate() {

    try {/*  www.  j  a  v a 2 s  . c  om*/
        Mongo mongo;
        if (MONGO_MAP.containsKey(mongoUri)) {
            mongo = MONGO_MAP.get(mongoUri);

        } else {
            mongo = new Mongo(new MongoURI(mongoUri));
            MONGO_MAP.put(mongoUri, mongo);

        }

        MongoURI uri = new MongoURI(mongoUri);
        return new MongoTemplate(new SimpleMongoDbFactory(mongo, uri.getDatabase(),
                new UserCredentials(uri.getUsername(), parseChars(uri.getPassword()))));

    } catch (Exception e) {
        logger.error("mongo db error ,uri={}", mongoUri, e);
        return null;
    }

}

From source file:com.bia.monitor.dao.AppConfig.java

License:Apache License

public @Bean Mongo mongo() throws Exception {
    return new Mongo("localhost");
}

From source file:com.ccoe.build.alerts.connector.Connector.java

License:Apache License

public static DB connectDB(List<String> location, int port, String dbname) {

    try {/*from ww  w  .ja va  2s  .  c  o  m*/
        List<ServerAddress> serverAddressList = new ArrayList<ServerAddress>();
        ServerAddress serverAddress = null;
        for (String loc : location) {
            serverAddress = new ServerAddress(loc, port);
            serverAddressList.add(serverAddress);
        }
        Mongo mongo = new Mongo(serverAddressList);
        DB db = mongo.getDB(dbname);
        return db;

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.cloudbees.gasp.model.MongoConnection.java

License:Apache License

public void connect() throws Exception {
    try {/*from   ww  w.  j  a  va2s.  co m*/
        // Connect to Mongo and Authenticate
        MongoURI mongoURI = new MongoURI(mongoURL);
        mongo = new Mongo(mongoURI);
        mongoDB = mongo.getDB(mongoURI.getDatabase());
        mongoDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword());

        // Get Mongo collections and set WriteConcern
        String mongoLocations = "locations";
        locations = getMongoDB().getCollection(mongoLocations);
        mongoDB.setWriteConcern(WriteConcern.SAFE);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}

From source file:com.comcast.video.dawg.service.house.AbstractDawgService.java

License:Apache License

@PostConstruct
public void init() throws UnknownHostException {
    mongo = new Mongo(config.getDawgDbHost());
    template = new MongoTemplate(mongo, "test");
}

From source file:com.comcast.video.dawg.service.pound.DawgPoundMongoService.java

License:Apache License

@PostConstruct
public void init() throws UnknownHostException {
    mongoTemplate = new MongoTemplate(new Mongo(config.getDawgDbHost()), "test");
}

From source file:com.deftlabs.examples.mongo.MorphiaExample.java

License:Apache License

/**
 * A simple insert write concern.//w  w  w  .ja  va2  s  . c  om
 */
@Test
public void morphiaExample() throws Exception {

    final Morphia morphia = new Morphia();
    morphia.mapPackage("com.deftlabs.examples.mongo");

    final Datastore datastore = morphia.createDatastore(new Mongo(new MongoURI("mongodb://127.0.0.1:27017")),
            "mongo-java-driver-intro");

    // Create the object(s)
    final ObjectId docId = ObjectId.get();

    final TestEntity test = new TestEntity();
    test.setId(docId);
    test.setName("NameValueTest");

    final Child child = new Child();
    child.setChildName("NameValueTestChild");
    test.setChild(child);

    // Save
    datastore.save(test);

    // Query for the entity.
    final TestEntity findTest = datastore.find(TestEntity.class, "_id", docId).get();
    assertNotNull(findTest);

    assertNotNull(findTest.getChild());
}

From source file:com.deftlabs.examples.mongo.TailableCursorExample.java

License:Apache License

public static void main(final String[] pArgs) throws Exception {
    final Mongo mongo = new Mongo(new MongoURI("mongodb://127.0.0.1:27017"));

    mongo.getDB("testTailableCursor").dropDatabase();

    // Create the capped collection
    final BasicDBObject conf = new BasicDBObject("capped", true);
    conf.put("size", 20971520); // 20 MB
    mongo.getDB("testTailableCursor").createCollection("test", conf);

    final AtomicBoolean readRunning = new AtomicBoolean(true);
    final AtomicBoolean writeRunning = new AtomicBoolean(true);

    final AtomicLong writeCounter = new AtomicLong(0);
    final AtomicLong readCounter = new AtomicLong(0);

    final ArrayList<Thread> writeThreads = new ArrayList<Thread>();
    final ArrayList<Thread> readThreads = new ArrayList<Thread>();

    for (int idx = 0; idx < 10; idx++) {
        final Thread writeThread = new Thread(new Writer(mongo, writeRunning, writeCounter));
        final Thread readThread = new Thread(new Reader(mongo, readRunning, readCounter));
        writeThread.start();/*from ww w.ja v a 2 s  .  co m*/
        readThread.start();
        writeThreads.add(writeThread);
        readThreads.add(readThread);
    }

    // Run for five minutes
    //Thread.sleep(300000);
    Thread.sleep(20000);
    writeRunning.set(false);
    Thread.sleep(5000);
    readRunning.set(false);
    Thread.sleep(5000);

    for (final Thread readThread : readThreads)
        readThread.interrupt();
    for (final Thread writeThread : writeThreads)
        writeThread.interrupt();

    System.out.println("----- write count: " + writeCounter.get());
    System.out.println("----- read count: " + readCounter.get());
}

From source file:com.deftlabs.logging.mongo.MongoHandler.java

License:Apache License

/**
 * Returns the configured collection.//from w  ww  . j a  v a 2 s  .  c o  m
 */
private DBCollection getCollection() throws UnknownHostException {
    if (_collection != null)
        return _collection;

    synchronized (sMutex) {
        if (_collection != null)
            return _collection;

        _mongo = new Mongo(new MongoURI(_mongoUri));

        final DB db = _mongo.getDB(_databaseName);

        _collection = _mongo.getDB(_databaseName).getCollection(_collectionName);

        return _collection;
    }
}

From source file:com.dianping.swallow.web.dao.SimMongoDbFactory.java

License:Apache License

/**
 * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoURI}.
 * // w ww  . j  av  a  2s  . co m
 * @param uri must not be {@literal null}.
 * @throws MongoException
 * @throws UnknownHostException
 * @see MongoURI
 * @deprecated since 1.7. Please use {@link #SimpleMongoDbFactory(MongoClientURI)} instead.
 */
@Deprecated
public SimMongoDbFactory(MongoURI uri) throws MongoException, UnknownHostException {
    this(new Mongo(uri), uri.getDatabase(),
            new UserCredentials(uri.getUsername(), parseChars(uri.getPassword())), true, uri.getDatabase());
}