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:org.exoplatform.addons.persistence.services.mongodb.MongoBootstrap.java

License:Open Source License

private static Mongo setupEmbedMongo() throws IOException {
    MongodStarter runtime = MongodStarter.getDefaultInstance();
    int port = Integer.parseInt(PropertyManager.getProperty(PropertyManager.PROPERTY_DB_SERVER_PORT));
    mongodExe = runtime.prepare(new MongodConfig(Version.V2_3_0, port, Network.localhostIsIPv6()));
    mongod = mongodExe.start();//from   w  w w .j  av a2  s  .  c  o  m
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        log.info(e.getMessage());
    }
    String host = PropertyManager.getProperty(PropertyManager.PROPERTY_DB_SERVER_HOST);

    return new Mongo(new ServerAddress(host, port));
}

From source file:org.fao.fenix.wds.core.jdbc.MongoDBConnectionManager.java

License:Open Source License

public static Mongo getMongo(String url) throws UnknownHostException {
    if (mongo == null)
        mongo = new Mongo(url != null ? url : URL);
    return mongo;
}

From source file:org.helios.dashkuj.domain.Dashboard.java

License:Open Source License

public static void main(String[] args) {
    Jedis jedis = null;/*from ww w  .  j  a v  a  2s  .co  m*/
    Mongo mongo = null;
    DBCursor cursor = null;
    Morphia morphia = null;
    Datastore mongoDs = null;
    try {
        jedis = new Jedis("dashku");
        Map<String, String> apiKeys = jedis.hgetAll("apiKeys");
        log(apiKeys);
        mongo = new Mongo("dashku");
        morphia = new Morphia();
        mongoDs = morphia.createDatastore(mongo, "dashku_development");
        DB db = mongo.getDB("dashku_development");
        DBCollection dbColl = db.getCollection("users");
        cursor = dbColl.find();
        while (cursor.hasNext()) {
            DBObject dbo = cursor.next();
            String apiKey = dbo.get("apiKey").toString();
            String id = dbo.get("_id").toString();
            String user = dbo.get("username").toString();
            log("Inspecting user [" + user + "]");
            if (!apiKeys.containsKey(apiKey)) {
                jedis.hmset("apiKeys", Collections.singletonMap(apiKey, id));
                log("Added missing redis entry [" + apiKey + "] for user [" + user + "]");
            }
        }
        cursor.close();

        //         CommandResult cr = db.command("serverStatus");
        //         Date date = cr.getDate("localTime");
        //         
        //         JsonElement je = new JsonParser().parse(cr.toString());
        //         String jsDate = je.getAsJsonObject().get("localTime").getAsJsonObject().getAsJsonPrimitive("$date").toString();
        //         //log(GsonFactory.getInstance().printer().toJson(je));
        //         log("Date:[" + date + "]");
        //         log("JS-Date:[" + jsDate + "]");
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    } finally {
        if (jedis != null)
            try {
                jedis.disconnect();
            } catch (Exception ex) {
            }
        if (cursor != null)
            try {
                cursor.close();
            } catch (Exception ex) {
            }
        if (mongo != null)
            try {
                mongo.close();
            } catch (Exception ex) {
            }
    }
}

From source file:org.jeo.mongo.MongoOpts.java

License:Open Source License

public DB connect() throws IOException {
    ServerAddress server = new ServerAddress(host, port);
    Mongo mongo = new Mongo(server);
    DB database = mongo.getDB(db);//from   w  ww  .  ja v a2 s.  c om
    database.authenticate(user, passwd != null ? passwd.get() : new char[] {});

    return database;
}

From source file:org.jimmyray.mongo.events.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:29009"));

    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 www  . j a va  2s .  c o 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:org.mca.qmass.mongodb.MongoDBAccessorImpl.java

License:Apache License

public MongoDBAccessorImpl(String host) {
    try {//from ww w . jav a2 s  . c  o m
        mongo = new Mongo(host);
    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.modeshape.jcr.value.binary.MongodbBinaryStore.java

License:Apache License

private void initMongoDatabase() {
    // connect to database
    try {/* ww w.  j  a v a2  s.com*/
        Mongo mongo = null;
        if (!replicaSet.isEmpty()) {
            mongo = new Mongo(replicaSet(replicaSet));
        } else if (!StringUtil.isBlank(host)) {
            mongo = new Mongo(host, port);
        } else {
            mongo = new Mongo();
        }
        db = mongo.getDB(database);
    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.newhart.jexample.JExample0.java

License:Apache License

private static synchronized Mongo mongo() throws UnknownHostException {
    if (mongoInstance == null) {
        mongoInstance = new Mongo("localhost");
    }/*from  w  w  w  .  j  a v a  2  s . c  o  m*/

    return mongoInstance;
}

From source file:org.newhart.jexample.JExample1.java

License:Apache License

public static void main(final String args[]) throws UnknownHostException {
    final MongoConnection con = new MongoConnection(new Mongo("localhost"));
    final AuditStore store = new AuditStore(con);
    store.createAudit("audit_test", 100000000);

    final Date now = new Date();
    /*store.writeAuditRecord("org.newhart.jexample.JExample1", "123", "org.newhart.example.User",
       "low", now, now, now, "message",/*from w w  w.  j ava  2s .  co  m*/
       new java.util.HashMap(), new java.util.LinkedList(), new java.util.HashSet());*/
    testAudit(1, "test");
    testAuditException();
}

From source file:org.ossmeter.platform.Configuration.java

License:Open Source License

public Mongo getMongoConnection() throws UnknownHostException {
    //      System.out.println("Mongo hosts: " + properties.getProperty(MONGO_HOSTS, "localhost:27017"));
    String[] hosts = properties.getProperty(MONGO_HOSTS, "localhost:27017").split(",");

    if (hosts.length > 1) {
        List<ServerAddress> mongoHostAddresses = new ArrayList<>();
        for (String host : hosts) {
            String[] s = host.split(":");
            mongoHostAddresses.add(new ServerAddress(s[0], Integer.valueOf(s[1])));
        }// ww w .  java 2 s .  c  o  m

        //         MongoOptions options = new MongoOptions();
        //         options.connectTimeout = 1000;
        return new Mongo(mongoHostAddresses);//,options);

    } else {
        return new Mongo();//hosts[0]);
    }

}