Example usage for com.mongodb MongoClient MongoClient

List of usage examples for com.mongodb MongoClient MongoClient

Introduction

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

Prototype

public MongoClient(final MongoClientURI uri, final MongoDriverInformation mongoDriverInformation) 

Source Link

Document

Creates a Mongo described by a URI.

Usage

From source file:com.edgytech.umongo.ServerNode.java

License:Apache License

public ServerNode(ServerAddress serverAddress, MongoClientOptions opts, boolean isReplica, boolean isConfig) {
    setLabel(serverAddress.toString());//w w w .ja v  a2  s.c o  m
    this.serverAddress = serverAddress;
    serverMongo = new MongoClient(serverAddress, opts);
    serverMongo.addOption(Bytes.QUERYOPTION_SLAVEOK);
    this.isReplica = isReplica;
    this.isConfig = isConfig;

    try {
        xmlLoad(Resource.getXmlDir(), Resource.File.serverNode, null);
    } catch (Exception ex) {
        getLogger().log(Level.SEVERE, null, ex);
    }

    markStructured();
}

From source file:com.edgytech.umongo.ServerNode.java

License:Apache License

public ServerNode(String host, MongoClientOptions opts, boolean isReplica, boolean isConfig)
        throws UnknownHostException {
    setLabel(host);//from  ww w.j av a  2 s.com
    this.host = host;
    this.serverAddress = new ServerAddress(host);
    serverMongo = new MongoClient(serverAddress, opts);
    serverMongo.addOption(Bytes.QUERYOPTION_SLAVEOK);
    this.isReplica = isReplica;
    this.isConfig = isConfig;

    try {
        xmlLoad(Resource.getXmlDir(), Resource.File.serverNode, null);
    } catch (Exception ex) {
        getLogger().log(Level.SEVERE, null, ex);
    }

    markStructured();
}

From source file:com.ejbmongoembeddedtomcat.listener.MongoDBContextListener.java

@Override
public void contextInitialized(ServletContextEvent sce) {

    ServletContext ctx = sce.getServletContext();
    MongoClient mongo = new MongoClient(ctx.getInitParameter("MONGODB_HOST"),
            Integer.parseInt(ctx.getInitParameter("MONGODB_PORT")));
    System.out.println("MongoClient initialized successfully");
    sce.getServletContext().setAttribute("MONGO_CLIENT", mongo);
}

From source file:com.emuneee.camerasyncmanager.util.DatabaseUtil.java

License:Apache License

private void initMongoClient() throws UnknownHostException {
    sLogger.info("Initializing MongoDB Client");
    String dbUrl = mProperties.getProperty("DATABASE_URL");
    int port = Integer.parseInt(mProperties.getProperty("DATABASE_PORT"));
    sLogger.debug("Database URL: " + dbUrl);
    sLogger.debug("Database Port: " + String.valueOf(port));

    // create the mongo client and connect
    ServerAddress serverAddr = new ServerAddress(mProperties.getProperty("DATABASE_URL"),
            Integer.parseInt(mProperties.getProperty("DATABASE_PORT")));
    MongoCredential credential = MongoCredential.createMongoCRCredential(
            mProperties.getProperty("DATABASE_USER"), mProperties.getProperty("DATABASE_NAME"),
            mProperties.getProperty("DATABASE_AUTH").toCharArray());
    mMongoClient = new MongoClient(serverAddr, Arrays.asList(credential));
}

From source file:com.englishtown.integration.java.IntegrationTestHelper.java

License:Open Source License

public static GridFS getGridFS(JsonObject config, String bucket) {

    Mongo mongo;/*from  w  ww .  j a v a2 s  . c o m*/
    try {
        mongo = new MongoClient(config.getString("host", "localhost"), config.getInteger("port", 27017));
    } catch (UnknownHostException e) {
        fail();
        return null;
    }

    String dbName = config.getString("db_name", "default_db");
    DB db = mongo.getDB(dbName);

    if (bucket == null) {
        return new GridFS(db);
    } else {
        return new GridFS(db, bucket);
    }

}

From source file:com.englishtown.vertx.GridFSModule.java

License:Open Source License

@Override
public void start() {
    eb = vertx.eventBus();/*from   w  ww.  j a  v  a2s.c  o  m*/
    logger = container.logger();

    JsonObject config = container.config();
    address = config.getString("address", DEFAULT_ADDRESS);

    host = config.getString("host", "localhost");
    port = config.getInteger("port", 27017);
    dbName = config.getString("db_name", "default_db");
    username = config.getString("username", null);
    password = config.getString("password", null);
    int poolSize = config.getInteger("pool_size", 10);

    JsonArray seedsProperty = config.getArray("seeds");

    try {
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.connectionsPerHost(poolSize);
        if (seedsProperty == null) {
            ServerAddress address = new ServerAddress(host, port);
            mongo = new MongoClient(address, builder.build());
        } else {
            List<ServerAddress> seeds = makeSeeds(seedsProperty);
            mongo = new MongoClient(seeds, builder.build());
        }
        db = mongo.getDB(dbName);
        if (username != null && password != null) {
            db.authenticate(username, password.toCharArray());
        }
    } catch (UnknownHostException e) {
        logger.error("Failed to connect to mongo server", e);
    }

    // Main Message<JsonObject> handler that inspects an "action" field
    eb.registerHandler(address, this);

    // Message<byte[]> handler to save file chunks
    eb.registerHandler(address + "/saveChunk", new Handler<Message<Buffer>>() {
        @Override
        public void handle(Message<Buffer> message) {
            saveChunk(message);
        }
    });

}

From source file:com.enitalk.configs.MongoConfig.java

@Override
@Bean//  w w  w .j  a  va  2s .c om
public Mongo mongo() throws Exception {
    return new MongoClient(singletonList(new ServerAddress(env.getProperty("mongo.host"), 27017)),
            singletonList(MongoCredential.createCredential(env.getProperty("mongo.user"), getDatabaseName(),
                    env.getProperty("mongo.pass").toCharArray())));
}

From source file:com.epam.dlab.billing.azure.MongoDbBillingClient.java

License:Apache License

public MongoDbBillingClient(String host, int port, String databaseName, String username, String password) {
    this.client = new MongoClient(new ServerAddress(host, port), Lists
            .newArrayList(MongoCredential.createCredential(username, databaseName, password.toCharArray())));

    this.database = client.getDatabase(databaseName);
}

From source file:com.epam.dlab.mongo.MongoDbConnection.java

License:Apache License

/**
 * Instantiate the helper for Mongo database adapter.
 *
 * @param host         the host name./*from w  w  w  .j a  v  a 2 s  . co m*/
 * @param port         the port.
 * @param databaseName the name of database.
 * @param username     the name of user.
 * @param password     the password.
 * @throws AdapterException
 */
public MongoDbConnection(String host, int port, String databaseName, String username, String password)
        throws AdapterException {
    try {
        client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(
                MongoCredential.createCredential(username, databaseName, password.toCharArray())));
        database = client.getDatabase(databaseName).withWriteConcern(WriteConcern.ACKNOWLEDGED);
    } catch (Exception e) {
        throw new AdapterException(
                "Cannot create connection to database " + databaseName + ". " + e.getLocalizedMessage(), e);
    }
}

From source file:com.epam.dlab.mongo.MongoServiceFactory.java

License:Apache License

public MongoService build(Environment environment) {
    MongoClient client = new MongoClient(new ServerAddress(host, port), Collections
            .singletonList(MongoCredential.createCredential(username, database, password.toCharArray())));
    environment.lifecycle().manage(new Managed() {
        @Override//from  ww w.ja  v  a  2  s  .  c om
        public void start() {
        }

        @Override
        public void stop() {
            client.close();
        }
    });
    return new MongoService(client, database);
}