List of usage examples for com.mongodb.async.client MongoClients create
public static MongoClient create(final com.mongodb.MongoClientSettings settings)
From source file:com.example.App.java
License:Apache License
public static void main(String[] args) throws InterruptedException { final MongoClientSettings settings = MongoClientSettings.builder() .addCommandListener(new CommandListener() { @Override/*from w ww .j av a 2 s . com*/ public void commandStarted(CommandStartedEvent event) { logger.info("command: db = {}, command = {}", event.getDatabaseName(), event.getCommandName()); } @Override public void commandSucceeded(CommandSucceededEvent event) { logger.info("command succeed: request = {}, command = {}", event.getRequestId(), event.getCommandName()); } @Override public void commandFailed(CommandFailedEvent event) { logger.info("command failed: request = {}, command = {}", event.getRequestId(), event.getCommandName()); logger.error("detail", event.getThrowable()); } }).applicationName("sample-app") .applyToConnectionPoolSettings(builder -> builder.maxSize(1).minSize(1)).build(); final MongoClient client = MongoClients.create(settings); final MongoDatabase database = client.getDatabase("sample"); final MongoCollection<Document> collection = database.getCollection("test"); final Document firstDocument = new Document("id", UUID.randomUUID()).append("name", "test user") .append("created", LocalDateTime.now()); logger.info("document to be saved: {}", firstDocument); final Mono<Document> firstMono = Mono.create(sink -> collection.insertOne(firstDocument, (result, t) -> { if (t == null) { logger.info("inserted: {}", firstDocument); sink.success(firstDocument); } else { logger.error("error", t); sink.error(t); } })); final Mono<List<Document>> secondMono = create100Users(collection, firstMono); final Mono<Long> thirdMono = secondMono.then(Mono.create(sink -> collection.countDocuments((count, t) -> { if (t == null) { logger.info("collection has {} items.", count); sink.success(count); } else { logger.error("error", t); sink.error(t); } }))); final Mono<List<Document>> fourthMono = create100Users(collection, thirdMono); final Mono<List<Document>> fifthMono = create100Users(collection, fourthMono); final Mono<List<Document>> sixthMono = create100Users(collection, fifthMono); final Mono<Document> seventhMono = sixthMono.then(Mono.create(sink -> collection.find().first((doc, t) -> { if (t == null) { logger.info("found document: {}", doc); sink.success(doc); } else { logger.error("error", t); sink.error(t); } }))); final CountDownLatch latch = new CountDownLatch(1); seventhMono.doOnTerminate(() -> { latch.countDown(); client.close(); }).subscribe(doc -> logger.info("first document: {}", doc)); latch.await(); }
From source file:com.supermy.im.mongo.MongoRepository.java
License:Apache License
@Bean(name = "mongoClient") public MongoClient mongoClient() { System.out.println("*******************" + mongoAddress); ClusterSettings clusterSettings = ClusterSettings.builder() .hosts(Arrays.asList(new ServerAddress(mongoAddress))).build(); MongoCredential credential = MongoCredential.createCredential(mongoUser, mydb, mongoPasswd.toCharArray()); MongoClientSettings settings = MongoClientSettings.builder() .streamFactoryFactory(new NettyStreamFactoryFactory()).clusterSettings(clusterSettings) .credentialList(Arrays.asList(credential)).build(); // MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential)); MongoClient mongoClient = MongoClients.create(settings); return mongoClient; }
From source file:com.waves_rsp.ikb4stream.consumer.database.DatabaseReader.java
License:Open Source License
/** * The constructor of {@link DatabaseReader} * This class is a singleton/* www.j av a 2s . co m*/ * * @see DatabaseReader#checkConfiguration() * @see DatabaseReader#PROPERTIES_MANAGER * @see DatabaseReader#mongoCollection * @see DatabaseReader#limit */ private DatabaseReader() { try { checkConfiguration(); final MongoClient mongoClient = MongoClients.create(PROPERTIES_MANAGER.getProperty("database.host")); final MongoDatabase mongoDatabase = mongoClient .getDatabase(PROPERTIES_MANAGER.getProperty("database.datasource")); this.mongoCollection = mongoDatabase .getCollection(PROPERTIES_MANAGER.getProperty("database.collection")); } catch (IllegalArgumentException e) { LOGGER.error(e.getMessage()); throw new IllegalStateException(e); } int tmp = 50000; try { tmp = Integer.parseInt(PROPERTIES_MANAGER.getProperty("database.limit")); } catch (IllegalArgumentException e) { LOGGER.warn("Use default database.limit"); } this.limit = tmp; LOGGER.info("DatabaseReader has been instantiate"); }
From source file:com.waves_rsp.ikb4stream.producer.DatabaseWriter.java
License:Open Source License
/** * DataWriter constructor/* w ww . j a v a 2 s .com*/ * * @throws IllegalStateException if database configuration is not set */ private DatabaseWriter() { try { final MongoClient mongoClient = MongoClients.create(PROPERTIES_MANAGER.getProperty("database.host")); final MongoDatabase mongoDatabase = mongoClient .getDatabase(PROPERTIES_MANAGER.getProperty("database.datasource")); this.mongoCollection = mongoDatabase .getCollection(PROPERTIES_MANAGER.getProperty("database.collection")); } catch (IllegalArgumentException e) { LOGGER.error(e.getMessage()); throw new IllegalStateException(e.getMessage()); } LOGGER.info("DatabaseWriter has been instantiate"); }
From source file:mongodb.clients.percunia.mongo.AsyncClient.java
License:Apache License
private AsyncClient() { logger.info("initiating connection to Mongo"); try {/*from w w w. j ava 2s . c o m*/ ConnectionString uri = new ConnectionString("mongodb://" + Config.USERNAME + ":" + Config.PASSWORD + "@" + Config.HOST + "/?authSource=" + Config.DB_NAME); //ConnectionString uri = new ConnectionString("mongodb://localhost"); mongoClient = MongoClients.create(uri); database = mongoClient.getDatabase(Config.DB_NAME); } catch (Exception e) { logger.warn(e.toString()); } }
From source file:net.modelbased.proasense.storage.writer.EventWriterMongoAsync.java
License:Apache License
public void run() { // Connect to MongoDB database MongoClient mongoClient = MongoClients.create(mongoURL); MongoDatabase database = mongoClient.getDatabase(EventProperties.STORAGE_DATABASE_NAME); // Create hash map of collections Map<String, MongoCollection<Document>> collectionMap = new HashMap<String, MongoCollection<Document>>(); int cnt = 0;/* www. j av a 2 s. com*/ long timer0 = System.currentTimeMillis(); long timer1 = timer0; long timer2 = timer0; boolean skipLog = false; Map<String, List<Document>> documentMap = new HashMap<String, List<Document>>(); try { if (isLogfile) logfileWriter = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("EventWriterMongoAsync_benchmark_" + this.threadNumber + ".txt"), "ISO-8859-1")); while (true) { long timeoutExpired = System.currentTimeMillis() + this.maxWait; EventDocument eventDocument = queue.take(); String collectionId = eventDocument.getCollectionId(); if (!collectionId.matches(EventProperties.STORAGE_HEARTBEAT)) { cnt++; skipLog = false; } else skipLog = true; // Add data for bulk write if (!collectionId.matches(EventProperties.STORAGE_HEARTBEAT)) { Document document = eventDocument.getDocument(); if (!collectionMap.containsKey(collectionId)) { collectionMap.put(collectionId, database.getCollection(collectionId)); List<Document> documentList = new ArrayList<Document>(); documentMap.put(collectionId, documentList); } documentMap.get(collectionId).add(document); } // Write data if heartbeat timeout is received else { for (Map.Entry<String, MongoCollection<Document>> entry : collectionMap.entrySet()) { String key = entry.getKey(); if (!documentMap.get(key).isEmpty()) { collectionMap.get(key).insertMany(documentMap.get(key), new SingleResultCallback<Void>() { // @Override public void onResult(final Void result, final Throwable t) { } }); documentMap.get(key).clear(); } } } // Write data if bulk size or max wait (ms) is reached if ((cnt % this.bulkSize == 0) || (System.currentTimeMillis() >= timeoutExpired)) { for (Map.Entry<String, MongoCollection<Document>> entry : collectionMap.entrySet()) { String key = entry.getKey(); if (!documentMap.get(key).isEmpty()) { collectionMap.get(key).insertMany(documentMap.get(key), new SingleResultCallback<Void>() { // @Override public void onResult(final Void result, final Throwable t) { } }); documentMap.get(key).clear(); } } } // Benchmark output if ((!skipLog) && (cnt % this.logSize == 0)) { timer2 = System.currentTimeMillis(); long difference = timer2 - timer1; if (difference != 0) { long average = (this.logSize * 1000) / (timer2 - timer1); System.out.println("Benchmark: "); System.out.println(" Records written : " + cnt); System.out.println(" Average records/s: " + average); timer1 = timer2; if (isLogfile) { logfileWriter.write(cnt + "," + average + System.getProperty("line.separator")); logfileWriter.flush(); } if (cnt == this.loadTestMaxMessages) { long loadTestStart = timer0 / 1000; long loadTestEnd = timer2 / 1000; long loadTestTime = loadTestEnd - loadTestStart; long loadTestAverage = this.loadTestMaxMessages / loadTestTime; System.out.println("*****************************"); System.out.println("Load test results: "); System.out.println(" Records written : " + cnt); System.out.println(" Average records/s: " + loadTestAverage); if (isLogfile) { logfileWriter.write( "*****************************" + System.getProperty("line.separator")); logfileWriter.write("Load test results: " + System.getProperty("line.separator")); logfileWriter.write( " Records written : " + cnt + System.getProperty("line.separator")); logfileWriter.write(" Average records/s: " + loadTestAverage + System.getProperty("line.separator")); logfileWriter.flush(); } } } } } } catch (InterruptedException e) { System.out.println(e.getClass().getName() + ": " + e.getMessage()); } catch (IOException e) { System.out.println(e.getClass().getName() + ": " + e.getMessage()); } finally { if (isLogfile) try { logfileWriter.close(); } catch (IOException e) { System.out.println(e.getClass().getName() + ": " + e.getMessage()); } } mongoClient.close(); }
From source file:org.apache.pulsar.io.mongodb.MongoSink.java
License:Apache License
@Override public void open(Map<String, Object> config, SinkContext sinkContext) throws Exception { log.info("Open MongoDB Sink"); mongoConfig = MongoConfig.load(config); mongoConfig.validate();// w w w.j a v a2 s . c o m mongoClient = MongoClients.create(mongoConfig.getMongoUri()); final MongoDatabase db = mongoClient.getDatabase(mongoConfig.getDatabase()); collection = db.getCollection(mongoConfig.getCollection()); incomingList = Lists.newArrayList(); flushExecutor = Executors.newScheduledThreadPool(1); flushExecutor.scheduleAtFixedRate(() -> flush(), mongoConfig.getBatchTimeMs(), mongoConfig.getBatchTimeMs(), TimeUnit.MILLISECONDS); }
From source file:org.fixtrading.timpani.securitydef.datastore.MongoDBSecurityDefinitionStore.java
License:Apache License
@Override public CompletableFuture<SecurityDefinitionStore> open() { List<ServerAddress> hostList = Arrays.stream(hosts).map(h -> new ServerAddress(h)) .collect(Collectors.toList()); ClusterSettings clusterSettings = ClusterSettings.builder().hosts(hostList).build(); MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).build(); mongoClient = MongoClients.create(settings); database = mongoClient.getDatabase(DATABASE_NAME); collection = database.getCollection(SECDEF_COLLECTION_NAME); // In the case of MongoDB, open is synchronous because it doesn't // actually communicate with the server until a query is invoked. return CompletableFuture.completedFuture(this); }
From source file:org.jnosql.diana.mongodb.document.MongoDBDocumentConfiguration.java
License:Open Source License
private com.mongodb.async.client.MongoClient getAsyncMongoClient(List<ServerAddress> servers) { ClusterSettings clusterSettings = ClusterSettings.builder().hosts(servers).build(); MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).build(); return MongoClients.create(settings); }
From source file:org.openo.commontosca.inventory.core.mongo.MongoInventoryProvider.java
License:Apache License
public static MongoClient create(final String url) { ConnectionString connectionString = new ConnectionString(url); Builder builder = MongoClientSettings.builder(); builder.clusterSettings(ClusterSettings.builder().applyConnectionString(connectionString).build()); builder.connectionPoolSettings(//from ww w. j a va 2 s. com ConnectionPoolSettings.builder().applyConnectionString(connectionString).build()); builder.serverSettings(ServerSettings.builder().build()); builder.credentialList(connectionString.getCredentialList()); builder.sslSettings(SslSettings.builder().applyConnectionString(connectionString).build()); builder.socketSettings(SocketSettings.builder().applyConnectionString(connectionString).build()); builder.codecRegistry( CodecRegistries.fromRegistries(MongoClients.getDefaultCodecRegistry(), new ObjectCodecRegistry())); // builder.streamFactoryFactory(new NettyStreamFactoryFactory()); return MongoClients.create(builder.build()); }