List of usage examples for com.mongodb ServerAddress ServerAddress
public ServerAddress(@Nullable final String host, final int port)
From source file:com.redhat.lightblue.mongo.config.metadata.MongoConfiguration.java
License:Open Source License
public void addServerAddress(String hostname, Integer port) throws UnknownHostException { this.servers.add(new ServerAddress(hostname, port)); }
From source file:com.redhat.lightblue.mongo.config.MongoConfiguration.java
License:Open Source License
public void addServerAddress(String hostname, int port) throws UnknownHostException { this.servers.add(new ServerAddress(hostname, port)); }
From source file:com.redhat.lightblue.mongo.config.MongoConfiguration.java
License:Open Source License
public void setServer(String hostname, int port) throws UnknownHostException { theServer = new ServerAddress(hostname, port); }
From source file:com.redhat.thermostat.gateway.common.mongodb.ThermostatMongoStorage.java
License:Open Source License
public ThermostatMongoStorage(Map<String, String> mongoConfiguration) { String username = null;//from w w w .j a va 2 s . com char[] password = null; String host = "127.0.0.1"; int port = 27518; int timeout = 1000; String dbName = DEFAULT_DB_NAME; if (mongoConfiguration.containsKey(MongoConfiguration.MONGO_DB.toString())) { dbName = mongoConfiguration.get(MongoConfiguration.MONGO_DB.toString()); } if (mongoConfiguration.containsKey(MongoConfiguration.MONGO_USERNAME.toString())) { username = mongoConfiguration.get(MongoConfiguration.MONGO_USERNAME.toString()); } if (mongoConfiguration.containsKey(MongoConfiguration.MONGO_PASSWORD.toString())) { password = mongoConfiguration.get(MongoConfiguration.MONGO_PASSWORD.toString()).toCharArray(); } if (mongoConfiguration.containsKey(MongoConfiguration.MONGO_URL.toString())) { try { URI url = new URI(mongoConfiguration.get(MongoConfiguration.MONGO_URL.toString())); host = url.getHost(); port = url.getPort(); } catch (URISyntaxException e) { // Do nothing. Defaults will be used. } } if (mongoConfiguration.containsKey(MongoConfiguration.MONGO_SERVER_TIMEOUT.toString())) { timeout = Integer.valueOf(mongoConfiguration.get(MongoConfiguration.MONGO_SERVER_TIMEOUT.toString())); } ServerAddress address = new ServerAddress(host, port); if (username != null && password != null) { MongoCredential credential = MongoCredential.createCredential(username, dbName, password); mongoClient = new MongoClient(address, Collections.singletonList(credential), new MongoClientOptions.Builder().serverSelectionTimeout(timeout).connectTimeout(0) .socketTimeout(0).build()); } else { mongoClient = new MongoClient(address, new MongoClientOptions.Builder().serverSelectionTimeout(timeout) .connectTimeout(0).socketTimeout(0).build()); } Logger mongoLog = Logger.getLogger("org.mongodb.driver"); mongoLog.setLevel(Level.OFF); this.dbName = dbName; }
From source file:com.redhat.thermostat.gateway.tests.utils.MongodTestUtil.java
License:Open Source License
public void startMongod() throws IOException, InterruptedException { tempDbDir = Files.createTempDirectory("tms-mongo"); tempDbDir.toFile().deleteOnExit();/* ww w.java2 s . co m*/ Files.createDirectories(tempDbDir.resolve("data/db")); tempLogFile = tempDbDir.resolve("mongod.log"); tempLogFile.toFile().deleteOnExit(); String[] posixCommand = { "mongod", "--dbpath", tempDbDir.resolve("data/db").toAbsolutePath().toString(), "--port", String.valueOf(port), "--fork", "--logpath", tempLogFile.toAbsolutePath().toString() }; String[] windowsCommand = { "cmd", "/c", "mongod", "--dbpath", tempDbDir.resolve("data/db").toAbsolutePath().toString(), "--port", String.valueOf(port), "--logpath", tempLogFile.toAbsolutePath().toString() }; ProcessBuilder builder = new ProcessBuilder(OS.IS_UNIX ? posixCommand : windowsCommand); process = builder.start(); mongoClient = new MongoClient(new ServerAddress(host, port)); connectedToDatabase = waitForMongodStart(); }
From source file:com.redhat.thermostat.web2.endpoint.command.MongoStorage.java
License:Open Source License
public static void start(final String dbName, int port) { MongoStorage.dbName = dbName;/* ww w . j a va 2 s . c o m*/ MongoCredential credential = MongoCredential.createCredential(username, dbName, password); ServerAddress address = new ServerAddress("127.0.0.1", port); mongoClient = new MongoClient(address, Collections.singletonList(credential), new MongoClientOptions.Builder().serverSelectionTimeout(0).build()); }
From source file:com.sag.tn.storm.stormmaven.bolts.CounterIncrementorBolt.java
License:Open Source License
@SuppressWarnings("rawtypes") public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) { this.collector = collector; MongoCredential credential = MongoCredential.createCredential((String) stormConf.get("MongoUser"), (String) stormConf.get("MongoDatabase"), ((String) stormConf.get("MongoPass")).toCharArray()); this.mClient = new MongoClient(new ServerAddress((String) stormConf.get("MongoHost"), Integer.parseInt((String) stormConf.get("MongoPort"))), Arrays.asList(credential)); this.db = this.mClient.getDatabase((String) stormConf.get("MongoDatabase")); this.coll = db.getCollection((String) stormConf.get("MongoColl")); }
From source file:com.sag.tn.storm.stormmaven.bolts.DocumentTypeIdentifierBolt.java
License:Open Source License
@SuppressWarnings("rawtypes") public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) { this.collector = collector; MongoCredential credential = MongoCredential.createCredential((String) stormConf.get("MongoUser"), (String) stormConf.get("MongoDatabase"), ((String) stormConf.get("MongoPass")).toCharArray()); this.mClient = new MongoClient(new ServerAddress((String) stormConf.get("MongoHost"), Integer.parseInt((String) stormConf.get("MongoPort"))), Arrays.asList(credential)); this.db = this.mClient.getDatabase((String) stormConf.get("MongoDatabase")); this.coll = db.getCollection((String) stormConf.get("MongoColl")); }
From source file:com.sample.db.Database.java
private void init() { try {// ww w. j a v a 2 s .com props = new Properties(); props.load( Thread.currentThread().getContextClassLoader().getResourceAsStream("./application.properties")); max_conn_idle = Integer.parseInt(props.getProperty("mongo.max.connection.idle")); min_conn = Integer.parseInt(props.getProperty("mongo.min.connection")); server_port = Integer.parseInt(props.getProperty("mongo.port")); server_url = props.getProperty("mongo.server"); auth_db = props.getProperty("mongo.auth.db"); app_db = props.getProperty("mongo.app.db"); app_user = props.getProperty("mongo.auth.user"); app_pwd = props.getProperty("mongo.auth.pwd"); ServerAddress addr = new ServerAddress(server_url, server_port); MongoCredential creds = MongoCredential.createScramSha1Credential(app_user, auth_db, app_pwd.toCharArray()); List<MongoCredential> credList = new ArrayList<>(); credList.add(creds); MongoClientOptions options = new MongoClientOptions.Builder() .codecRegistry(getApplicationCodecRegistry()).minConnectionsPerHost(min_conn) .maxConnectionIdleTime(max_conn_idle).build(); client = new MongoClient(addr, credList, options); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.setronica.ucs.server.MessageServer.java
License:LGPL
private static ClassPathXmlApplicationContext bootstrapMongo(String mongoUrls) throws UnknownHostException { List<ServerAddress> seeds = new ArrayList<>(); for (String s : mongoUrls.split(",")) { int idx = s.indexOf(":"); ServerAddress address;//from ww w .j a va2 s .c o m if (idx > 0) { address = new ServerAddress(s.substring(0, idx), Integer.parseInt(s.substring(idx + 1))); } else { address = new ServerAddress(s); } seeds.add(address); } //create parent BeanFactory DefaultListableBeanFactory parentBeanFactory = new DefaultListableBeanFactory(); //register your pre-fabricated object in it parentBeanFactory.registerSingleton("addresses", seeds); ClassPathXmlApplicationContext applicationContext = initApplication("mongo", parentBeanFactory); MongoClient client = (MongoClient) applicationContext.getBean("mongoClient"); logger.info("addresses: " + client.getAllAddress()); return applicationContext; }