List of usage examples for com.mongodb MongoClient close
public void close()
From source file:com.ayu.filter.RegularService.java
License:Open Source License
@Async public void registerUser(String ip, String date, String type, String document) { //System.out.println(" Attack from "+ip +" captured at"+ date+" "+"type of attack is"+" "+type); //System.out.println(" Database Insertion "); try {/*from ww w . ja v a2 s.co m*/ Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } MongoClient mongoClient = null; try { mongoClient = new MongoClient(); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // or, to connect to a replica set, supply a seed list of members //MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017), // new ServerAddress("localhost", 27018), // new ServerAddress("localhost", 27019))); DB db = mongoClient.getDB(document); DBCollection coll; coll = db.getCollection(document); BasicDBObject doc = new BasicDBObject("Ip-Address", ip).append("Date", date).append("Attack-Type", type); //System.out.println("Data Display"); coll.insert(doc); mongoClient.close(); //System.out.println(" Asynchronous method call of database Complete "); }
From source file:com.baifendian.swordfish.common.job.struct.datasource.MongoDatasource.java
License:Apache License
@Override public void isConnectable() throws Exception { MongoClient mongoClient = new MongoClient(new MongoClientURI(this.address)); try {//from w w w. j a v a 2 s .c o m MongoClientOptions options = MongoClientOptions.builder().connectTimeout(10).socketKeepAlive(false) .build(); MongoDatabase db = mongoClient.getDatabase(this.database); for (Document doc : db.listCollections()) { logger.debug("{}", doc); } } finally { mongoClient.close(); } }
From source file:com.bdnc.ecommercebdnc.dao.DAODocumentos.java
public boolean salvarCompra(Compra compra) { MongoClient client = new MongoClient("localhost", 27017); MongoDatabase dataBase = client.getDatabase("ecommerce-bdnc"); MongoCollection<Document> collection = dataBase.getCollection("vendas"); collection.insertOne(compra.toDocument()); client.close(); return true;/*from www .ja v a2 s . c o m*/ }
From source file:com.bdnc.ecommercebdnc.dao.DAODocumentos.java
public List<Compra> buscarCompras() { MongoClient client = new MongoClient("localhost", 27017); MongoDatabase dataBase = client.getDatabase("ecommerce-bdnc"); MongoCollection<Document> collection = dataBase.getCollection("vendas"); List<Compra> compras = new ArrayList<>(); MongoCursor<Document> cursor = collection.find().iterator(); while (cursor.hasNext()) { Compra compra = new Compra(); compras.add(compra.fromDocument(cursor.next())); }// w w w . j a v a 2 s . com client.close(); return compras; }
From source file:com.bosscs.spark.mongodb.extractor.MongoNativeExtractor.java
License:Apache License
@Override public Partition[] getPartitions(S config) { MongoClient mongoClient = null; try {//from w w w. ja va 2 s .c o m mongoDeepJobConfig = initConfig(config, mongoDeepJobConfig); DBCollection collection; ServerAddress address = new ServerAddress(mongoDeepJobConfig.getHost()); List<ServerAddress> addressList = new ArrayList<>(); addressList.add(address); mongoClient = new MongoClient(addressList); //mongoClient.setReadPreference(ReadPreference.nearest()); DB db = mongoClient.getDB(mongoDeepJobConfig.getDatabase()); collection = db.getCollection(mongoDeepJobConfig.getCollection()); return isShardedCollection(collection) ? calculateShardChunks(collection) : calculateSplits(collection); } catch (UnknownHostException e) { throw new GenericException(e); } finally { if (mongoClient != null) { mongoClient.close(); } } }
From source file:com.bosscs.spark.mongodb.extractor.MongoNativeExtractor.java
License:Apache License
/** * Gets split data collection shard enviroment. * * @param shards the shards//from w w w . j ava2 s . com * @param dbName the db name * @param collectionName the collection name * @return the split data collection shard enviroment */ private Pair<BasicDBList, List<ServerAddress>> getSplitDataCollectionShardEnviroment( Map<String, String[]> shards, String dbName, String collectionName) { MongoClient mongoClient = null; try { Set<String> keys = shards.keySet(); for (String key : keys) { List<ServerAddress> addressList = getServerAddressList(Arrays.asList(shards.get(key))); mongoClient = new MongoClient(addressList); BasicDBList dbList = getSplitData(mongoClient.getDB(dbName).getCollection(collectionName)); if (dbList != null) { return Pair.create(dbList, addressList); } } } catch (UnknownHostException e) { throw new GenericException(e); } finally { if (mongoClient != null) { mongoClient.close(); } } return null; }
From source file:com.ca.apm.mongo.Collector.java
License:Open Source License
private CommandResult runDBCmd(final String host, final int port, final String database, final String cmd) throws Exception { MongoClient dbClient = null; try {//from ww w. j a v a 2 s .c om dbClient = setupDbClient(host, port); DB db = dbClient.getDB(database); return db.command(cmd); } finally { if (dbClient != null) { dbClient.close(); } } }
From source file:com.ca.apm.mongo.Collector.java
License:Open Source License
final boolean isConfigServer(final String host, final int port) { boolean isConfigServer = false; MongoClient dbClient = null; try {/*from w w w . j a v a 2 s. c o m*/ dbClient = setupDbClient(host, port); final DB configDB = dbClient.getDB("config"); if (configDB.getCollectionFromString("mongos").find().hasNext()) { isConfigServer = true; } } finally { if (dbClient != null) { dbClient.close(); } } return isConfigServer; }
From source file:com.ca.apm.mongo.ShardCluster.java
License:Open Source License
private List<String> getShardsFromConfig(final String host, final int port) { final List<String> shardList = new ArrayList<String>(); MongoClient dbClient = null; try {/*from www . j a v a 2 s. c om*/ dbClient = setupDbClient(host, port); final DB configDB = dbClient.getDB("config"); final DBCursor shardsCursor = configDB.getCollectionFromString("shards").find(); while (shardsCursor.hasNext()) { final DBObject dbo = shardsCursor.next(); String shards = (String) dbo.get("host"); String[] shardMembers = getShardMembers(shards); for (String member : shardMembers) { shardList.add(member); } } } catch (Exception e) { logger.log(Level.WARNING, "Exception getting shards from cfg servers: {0}", e); } finally { if (dbClient != null) { dbClient.close(); } } return shardList; }
From source file:com.ca.apm.mongo.ShardCluster.java
License:Open Source License
private List<String> getMongosFromConfig(final String host, final int port) { final List<String> shardRouters = new ArrayList<String>(); MongoClient dbClient = null; try {//w w w .ja v a 2s.c o m dbClient = setupDbClient(host, port); final DB configDB = dbClient.getDB("config"); final DBCursor mongosCursor = configDB.getCollectionFromString("mongos").find(); while (mongosCursor.hasNext()) { final DBObject dbo = mongosCursor.next(); final String mongos = (String) dbo.get("_id"); shardRouters.add(mongos); } } catch (Exception e) { logger.log(Level.WARNING, "Exception getting mongos from cfg server(s): {0}", e); } finally { if (dbClient != null) { dbClient.close(); } } return shardRouters; }