Example usage for com.mongodb.async.client MongoClient close

List of usage examples for com.mongodb.async.client MongoClient close

Introduction

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

Prototype

void close();

Source Link

Document

Close the client, which will close all underlying cached resources, including, for example, sockets and background monitoring threads.

Usage

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/*  w w  w. ja  v a2s . c  om*/
                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: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;//from w ww.  ja  va 2s .c  om
    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();

}