List of usage examples for com.mongodb ServerAddress ServerAddress
public ServerAddress()
From source file:org.springframework.data.document.mongodb.MongoFactoryBean.java
License:Apache License
public void afterPropertiesSet() throws Exception { // apply defaults - convenient when used to configure for tests // in an application context if (mongo == null) { if (host == null) { logger.warn("Property host not specified. Using default configuration"); mongo = new Mongo(); } else {/*from w w w .j av a2s . co m*/ ServerAddress defaultOptions = new ServerAddress(); if (mongoOptions == null) mongoOptions = new MongoOptions(); if (replicaPair != null) { if (replicaPair.size() < 2) { throw new CannotGetMongoDbConnectionException( "A replica pair must have two server entries"); } mongo = new Mongo(replicaPair.get(0), replicaPair.get(1), mongoOptions); } else if (replicaSetSeeds != null) { mongo = new Mongo(replicaSetSeeds, mongoOptions); } else { String mongoHost = host != null ? host : defaultOptions.getHost(); if (port != null) { mongo = new Mongo(new ServerAddress(mongoHost, port), mongoOptions); } else { mongo = new Mongo(mongoHost, mongoOptions); } } } } }
From source file:org.springframework.data.mongodb.core.MongoClientFactoryBean.java
License:Apache License
private ServerAddress createConfiguredOrDefaultServerAddress() throws UnknownHostException { ServerAddress defaultAddress = new ServerAddress(); return new ServerAddress(StringUtils.hasText(host) ? host : defaultAddress.getHost(), port != null ? port.intValue() : defaultAddress.getPort()); }
From source file:org.springframework.data.mongodb.core.MongoFactoryBean.java
License:Apache License
@Override protected Mongo createInstance() throws Exception { Mongo mongo;/*from w ww . j a va 2s. com*/ ServerAddress defaultOptions = new ServerAddress(); if (mongoOptions == null) { mongoOptions = new MongoOptions(); } if (!isNullOrEmpty(replicaPair)) { if (replicaPair.size() < 2) { throw new CannotGetMongoDbConnectionException("A replica pair must have two server entries"); } mongo = new Mongo(replicaPair.get(0), replicaPair.get(1), mongoOptions); } else if (!isNullOrEmpty(replicaSetSeeds)) { mongo = new Mongo(replicaSetSeeds, mongoOptions); } else { String mongoHost = StringUtils.hasText(host) ? host : defaultOptions.getHost(); mongo = port != null ? new Mongo(new ServerAddress(mongoHost, port), mongoOptions) : new Mongo(mongoHost, mongoOptions); } if (writeConcern != null) { mongo.setWriteConcern(writeConcern); } return mongo; }
From source file:org.springframework.datastore.mapping.mongo.MongoDatastore.java
License:Apache License
public void afterPropertiesSet() throws Exception { if (this.mongo == null) { ServerAddress defaults = new ServerAddress(); MongoFactoryBean dbFactory = new MongoFactoryBean(); dbFactory.setHost(read(String.class, MONGO_HOST, connectionDetails, defaults.getHost())); dbFactory.setPort(read(Integer.class, MONGO_PORT, connectionDetails, defaults.getPort())); if (mongoOptions != null) { dbFactory.setMongoOptions(mongoOptions); }//from www . jav a 2 s. co m dbFactory.afterPropertiesSet(); this.mongo = dbFactory.getObject(); } for (PersistentEntity entity : mappingContext.getPersistentEntities()) { createMongoTemplate(entity, mongo); } }
From source file:org.springframework.statemachine.buildtests.tck.mongodb.MongoDbRule.java
License:Apache License
@Override public Statement apply(Statement base, Description description) { MongoClient client = null;/* ww w.java 2 s . c om*/ try { client = new MongoClient(new ServerAddress(), MongoClientOptions.builder().connectTimeout(50).build()); client.getAddress(); } catch (Exception e) { return super.apply(new Statement() { @Override public void evaluate() throws Throwable { } }, Description.EMPTY); } finally { if (client != null) { client.close(); } } return super.apply(base, description); }
From source file:Tests.AddElementToCollection.java
public void addToCollection(String jsonString) { MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(MongoCredential.createCredential("admin", "test", "password".toCharArray()))); try {//from www . j av a2 s . com for (String databaseName : mongoClient.listDatabaseNames()) { System.out.println("Database: " + databaseName); } MongoDatabase db = mongoClient.getDatabase("test"); MongoCollection coll = db.getCollection("test2"); Document doc = Document.parse(jsonString); coll.insertOne(doc); } finally { mongoClient.close(); } }
From source file:Tests.PrintFristElementOfCollection.java
public static void main(String[] args) throws UnknownHostException { MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(MongoCredential.createCredential("admin", "test", "password".toCharArray()))); try {/*from ww w . ja v a 2 s . c o m*/ for (String databaseName : mongoClient.listDatabaseNames()) { System.out.println("Database: " + databaseName); } MongoDatabase db = mongoClient.getDatabase("test"); MongoCollection coll = db.getCollection("test"); System.out.println(coll.find().first()); } finally { mongoClient.close(); } }