com.mongodb.client.MongoClient Java Examples
The following examples show how to use
com.mongodb.client.MongoClient.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EmbedMongoConfiguration.java From syndesis with Apache License 2.0 | 8 votes |
private static MongoClient getClient(Boolean useCredentials, String replicaSet) { MongoClientSettings.Builder settings = MongoClientSettings.builder(); if (useCredentials) { MongoCredential credentials = MongoCredential.createCredential( USER, ADMIN_DB, PASSWORD.toCharArray()); settings.credential(credentials); } StringBuilder connectionString = new StringBuilder(String.format("mongodb://%s:%d", HOST, PORT)); if (replicaSet != null) { connectionString.append(String.format("/?replicaSet=%s", REPLICA_SET)); } ConnectionString uri = new ConnectionString(connectionString.toString()); settings.applyConnectionString(uri); settings.readPreference(ReadPreference.primaryPreferred()); return MongoClients.create(settings.build()); }
Example #2
Source File: SpringBootWebApplication.java From runelite with BSD 2-Clause "Simplified" License | 6 votes |
@Bean(destroyMethod = "") public MongoClient mongoClient(@Value("${mongo.host:}") String host, @Value("${mongo.jndiName:}") String jndiName) throws NamingException { if (!Strings.isNullOrEmpty(jndiName)) { JndiTemplate jndiTemplate = new JndiTemplate(); return jndiTemplate.lookup(jndiName, MongoClient.class); } else if (!Strings.isNullOrEmpty(host)) { return MongoClients.create(host); } else { throw new RuntimeException("Either mongo.host or mongo.jndiName must be set"); } }
Example #3
Source File: MongoMetricsTest.java From quarkus with Apache License 2.0 | 6 votes |
@Test void testMetricsInitialization() { // Clients are created eagerly, this metric should always be initialized to zero once connected assertEquals(0L, getGaugeValueOrNull("mongodb.connection-pool.size", getTags())); assertEquals(0L, getGaugeValueOrNull("mongodb.connection-pool.checked-out-count", getTags())); // Just need to execute something so that an connection is opened String name = client.listDatabaseNames().first(); assertEquals(1L, getGaugeValueOrNull("mongodb.connection-pool.size", getTags())); assertEquals(0L, getGaugeValueOrNull("mongodb.connection-pool.checked-out-count", getTags())); client.close(); assertEquals(0L, getGaugeValueOrNull("mongodb.connection-pool.size", getTags())); assertEquals(0L, getGaugeValueOrNull("mongodb.connection-pool.checked-out-count", getTags())); // doing this here instead of in another method in order to avoid messing with the initialization stats assertThat(Arc.container().instance(MongoClient.class).get()).isNotNull(); assertThat(Arc.container().instance(ReactiveMongoClient.class).get()).isNull(); }
Example #4
Source File: MongoCopyDataManager.java From mongo-kafka with Apache License 2.0 | 6 votes |
MongoCopyDataManager(final MongoSourceConfig sourceConfig, final MongoClient mongoClient) { this.sourceConfig = sourceConfig; this.mongoClient = mongoClient; String database = sourceConfig.getString(DATABASE_CONFIG); String collection = sourceConfig.getString(COLLECTION_CONFIG); List<MongoNamespace> namespaces; if (database.isEmpty()) { namespaces = getCollections(mongoClient); } else if (collection.isEmpty()) { namespaces = getCollections(mongoClient, database); } else { namespaces = singletonList(createNamespace(database, collection)); } LOGGER.info("Copying existing data on the following namespaces: {}", namespaces); namespacesToCopy = new AtomicInteger(namespaces.size()); queue = new ArrayBlockingQueue<>(sourceConfig.getInt(COPY_EXISTING_QUEUE_SIZE_CONFIG)); executor = Executors.newFixedThreadPool( Math.max( 1, Math.min( namespaces.size(), sourceConfig.getInt(COPY_EXISTING_MAX_THREADS_CONFIG)))); namespaces.forEach(n -> executor.submit(() -> copyDataFrom(n))); }
Example #5
Source File: MongoDBSinkBuilder.java From hazelcast-jet-contrib with Apache License 2.0 | 6 votes |
MongoSinkContext( MongoClient client, MongoCollection<T> collection, ConsumerEx<MongoClient> destroyFn, boolean ordered, boolean bypassValidation ) { this.client = client; this.collection = collection; this.destroyFn = destroyFn; this.insertManyOptions = new InsertManyOptions() .ordered(ordered) .bypassDocumentValidation(bypassValidation); documents = new ArrayList<>(); }
Example #6
Source File: MongoDb4MapMessageTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void test() { final Logger logger = LogManager.getLogger(); final MapMessage<?, Object> mapMessage = new MapMessage<>(); mapMessage.with("SomeName", "SomeValue"); mapMessage.with("SomeInt", 1); logger.info(mapMessage); // try (final MongoClient mongoClient = mongoDbTestRule.getMongoClient()) { final MongoDatabase database = mongoClient.getDatabase("testDb"); Assert.assertNotNull(database); final MongoCollection<Document> collection = database.getCollection("testCollection"); Assert.assertNotNull(collection); final Document first = collection.find().first(); Assert.assertNotNull(first); final String firstJson = first.toJson(); Assert.assertEquals(firstJson, "SomeValue", first.getString("SomeName")); Assert.assertEquals(firstJson, Integer.valueOf(1), first.getInteger("SomeInt")); } }
Example #7
Source File: MongoHealthCheck.java From quarkus with Apache License 2.0 | 6 votes |
@Override public HealthCheckResponse call() { HealthCheckResponseBuilder builder = HealthCheckResponse.named("MongoDB connection health check").up(); for (Map.Entry<String, MongoClient> client : clients.entrySet()) { boolean isDefault = DEFAULT_CLIENT.equals(client.getKey()); MongoClient mongoClient = client.getValue(); try { Document document = mongoClient.getDatabase("admin").runCommand(new Document("ping", 1)); String mongoClientName = isDefault ? "default" : client.getKey(); builder.up().withData(mongoClientName, document.toJson()); } catch (Exception e) { return builder.down().withData("reason", e.getMessage()).build(); } } return builder.build(); }
Example #8
Source File: MongoDb4AuthFailureTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void test() { final Logger logger = LogManager.getLogger(); logger.info("Hello log"); try (final MongoClient mongoClient = mongoDbTestRule.getMongoClient()) { final MongoDatabase database = mongoClient.getDatabase("testDb"); Assert.assertNotNull(database); final MongoCollection<Document> collection = database.getCollection("testCollection"); Assert.assertNotNull(collection); final Document first = collection.find().first(); Assert.assertNull(first); } }
Example #9
Source File: MongoHealthCheck.java From quarkus with Apache License 2.0 | 5 votes |
@PostConstruct protected void init() { Set<Bean<?>> beans = Arc.container().beanManager().getBeans(MongoClient.class); for (Bean<?> bean : beans) { if (bean.getName() == null) { // this is the default mongo client: retrieve it by type MongoClient defaultClient = Arc.container().instance(MongoClient.class).get(); clients.put(DEFAULT_CLIENT, defaultClient); } else { MongoClient client = (MongoClient) Arc.container().instance(bean.getName()).get(); clients.put(bean.getName(), client); } } }
Example #10
Source File: MongoClientCustomizer.java From syndesis with Apache License 2.0 | 5 votes |
@Override public void customize(ComponentProxyComponent component, Map<String, Object> options) { MongoCustomizersUtil.replaceAdminDBIfMissing(options); // Set connection parameter if (!options.containsKey("mongoConnection")) { if (options.containsKey("user") && options.containsKey("password") && options.containsKey("host")) { ConnectionParamsConfiguration mongoConf = new ConnectionParamsConfiguration(cast(options)); // We need to force consumption in order to perform property placeholder done by Camel consumeOption(camelContext, options, "password", String.class, mongoConf::setPassword); LOGGER.debug("Creating and registering a client connection to {}", mongoConf); MongoClientSettings.Builder settings = MongoClientSettings.builder(); MongoCredential credentials = MongoCredential.createCredential( mongoConf.getUser(), mongoConf.getAdminDB(), mongoConf.getPassword().toCharArray()); ConnectionString uri = new ConnectionString(mongoConf.getMongoClientURI()); settings.applyConnectionString(uri); settings.credential(credentials); MongoClient mongoClient = MongoClients.create(settings.build()); options.put("mongoConnection", mongoClient); if (!options.containsKey("connectionBean")) { //We safely put a default name instead of leaving null options.put("connectionBean", String.format("%s-%s", mongoConf.getHost(), mongoConf.getUser())); } } else { LOGGER.warn( "Not enough information provided to set-up the MongoDB client. Required at least host, user and " + "password."); } } }
Example #11
Source File: LocalMongoDbService.java From stitch-android-sdk with Apache License 2.0 | 5 votes |
/** * Helper function that drops all local databases for every client. */ public static void clearallLocalDBs() { for (final Map.Entry<MongoClient, Boolean> entry : localInstances.entrySet()) { for (final String dbName : entry.getKey().listDatabaseNames()) { entry.getKey().getDatabase(dbName).drop(); } } }
Example #12
Source File: AuthorServiceImpl.java From biliob_backend with MIT License | 5 votes |
@Autowired public AuthorServiceImpl(AuthorRepository respository, MongoClient mongoClient, MongoTemplate mongoTemplate, InputInspection inputInspection, AuthorUtil authorUtil, RealTimeFansRepository realTimeFansRepository, RedisOps redisOps, BiliobUtils biliOBUtils, AdminService adminService, AuthorAchievementService authorAchievementService) { this.respository = respository; this.mongoTemplate = mongoTemplate; this.mongoClient = mongoClient; this.authorUtil = authorUtil; this.realTimeFansRepository = realTimeFansRepository; this.redisOps = redisOps; this.biliOBUtils = biliOBUtils; this.adminService = adminService; this.authorAchievementService = authorAchievementService; }
Example #13
Source File: MongoOperations.java From quarkus with Apache License 2.0 | 5 votes |
private static MongoClient mongoClient(MongoEntity entity) { if (entity != null && !entity.clientName().isEmpty()) { Set<Bean<?>> beans = Arc.container().beanManager().getBeans(MongoClient.class); for (Bean<?> bean : beans) { if (bean.getName() != null) { return (MongoClient) Arc.container().instance(entity.clientName()).get(); } } } return Arc.container().instance(MongoClient.class).get(); }
Example #14
Source File: MongoOperations.java From quarkus with Apache License 2.0 | 5 votes |
private static MongoDatabase mongoDatabase(MongoEntity entity) { MongoClient mongoClient = mongoClient(entity); if (entity != null && !entity.database().isEmpty()) { return mongoClient.getDatabase(entity.database()); } String databaseName = getDefaultDatabaseName(); return mongoClient.getDatabase(databaseName); }
Example #15
Source File: PanacheMongoResourceProcessor.java From quarkus with Apache License 2.0 | 5 votes |
@BuildStep void unremoveableClients(BuildProducer<UnremovableBeanBuildItem> unremovable) { unremovable.produce( new UnremovableBeanBuildItem( new UnremovableBeanBuildItem.BeanClassNamesExclusion(new HashSet<>( Arrays.asList(MongoClient.class.getName(), ReactiveMongoClient.class.getName()))))); }
Example #16
Source File: MongoWithReplicasTestBase.java From quarkus with Apache License 2.0 | 5 votes |
private static void initializeReplicaSet(final List<IMongodConfig> mongodConfigList) throws UnknownHostException { final String arbitrerAddress = "mongodb://" + mongodConfigList.get(0).net().getServerAddress().getHostName() + ":" + mongodConfigList.get(0).net().getPort(); final MongoClientSettings mo = MongoClientSettings.builder() .applyConnectionString(new ConnectionString(arbitrerAddress)).build(); try (MongoClient mongo = MongoClients.create(mo)) { final MongoDatabase mongoAdminDB = mongo.getDatabase("admin"); Document cr = mongoAdminDB.runCommand(new Document("isMaster", 1)); LOGGER.infof("isMaster: %s", cr); // Build replica set configuration settings final Document rsConfiguration = buildReplicaSetConfiguration(mongodConfigList); LOGGER.infof("replSetSettings: %s", rsConfiguration); // Initialize replica set cr = mongoAdminDB.runCommand(new Document("replSetInitiate", rsConfiguration)); LOGGER.infof("replSetInitiate: %s", cr); // Check replica set status before to proceed await() .pollInterval(100, TimeUnit.MILLISECONDS) .atMost(1, TimeUnit.MINUTES) .until(() -> { Document result = mongoAdminDB.runCommand(new Document("replSetGetStatus", 1)); LOGGER.infof("replSetGetStatus: %s", result); return !isReplicaSetStarted(result); }); } }
Example #17
Source File: BaseMorphiaSession.java From morphia with Apache License 2.0 | 5 votes |
BaseMorphiaSession(final ClientSession session, final MongoClient mongoClient, final MongoDatabase database, final Mapper mapper, final QueryFactory queryFactory) { super(database, mongoClient, mapper, queryFactory); this.session = session; }
Example #18
Source File: MongoWithReplicasTestBase.java From quarkus with Apache License 2.0 | 5 votes |
private static void initializeReplicaSet(final List<IMongodConfig> mongodConfigList) throws UnknownHostException { final String arbitrerAddress = "mongodb://" + mongodConfigList.get(0).net().getServerAddress().getHostName() + ":" + mongodConfigList.get(0).net().getPort(); final MongoClientSettings mo = MongoClientSettings.builder() .applyConnectionString(new ConnectionString(arbitrerAddress)).build(); try (MongoClient mongo = MongoClients.create(mo)) { final MongoDatabase mongoAdminDB = mongo.getDatabase("admin"); Document cr = mongoAdminDB.runCommand(new Document("isMaster", 1)); LOGGER.infof("isMaster: %s", cr); // Build replica set configuration settings final Document rsConfiguration = buildReplicaSetConfiguration(mongodConfigList); LOGGER.infof("replSetSettings: %s", rsConfiguration); // Initialize replica set cr = mongoAdminDB.runCommand(new Document("replSetInitiate", rsConfiguration)); LOGGER.infof("replSetInitiate: %s", cr); // Check replica set status before to proceed await() .pollInterval(100, TimeUnit.MILLISECONDS) .atMost(1, TimeUnit.MINUTES) .until(() -> { Document result = mongoAdminDB.runCommand(new Document("replSetGetStatus", 1)); LOGGER.infof("replSetGetStatus: %s", result); return !isReplicaSetStarted(result); }); } }
Example #19
Source File: AfterConvertCallbacksBenchmark.java From spring-data-dev-tools with Apache License 2.0 | 5 votes |
@Setup public void setUp() { MongoClient client = mock(MongoClient.class); MongoDatabase db = mock(MongoDatabase.class); MongoCollection<Document> collection = mock(MongoCollection.class); when(client.getDatabase(anyString())).thenReturn(db); when(db.getCollection(anyString(), eq(Document.class))).thenReturn(collection); MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(client, "mock-database"); templateWithoutContext = new MongoTemplate(factory); templateWithEmptyContext = new MongoTemplate(factory); StaticApplicationContext empty = new StaticApplicationContext(); empty.refresh(); templateWithEmptyContext.setApplicationContext(empty); templateWithContext = new MongoTemplate(factory); templateWithContext.setApplicationContext(new AnnotationConfigApplicationContext(EntityCallbackConfig.class)); source = new Person(); source.id = "luke-skywalker"; source.firstname = "luke"; source.lastname = "skywalker"; source.address = new Address(); source.address.street = "melenium falcon 1"; source.address.city = "deathstar"; }
Example #20
Source File: MongoDB.java From aion with MIT License | 5 votes |
@Override public boolean open() { if (isOpen()) { return true; } LOG.info("Initializing MongoDB at {}", mongoClientUri); // Get the client and create a session for this instance MongoClient mongoClient = MongoConnectionManager.inst().getMongoClientInstance(this.mongoClientUri); ClientSessionOptions sessionOptions = ClientSessionOptions.builder() .causallyConsistent(true) .defaultTransactionOptions( TransactionOptions.builder() .readConcern(ReadConcern.DEFAULT) .writeConcern(WriteConcern.MAJORITY) .readPreference(ReadPreference.nearest()) .build()) .build(); this.clientSession = mongoClient.startSession(sessionOptions); // Get the database and our collection. Mongo takes care of creating these if they don't // exist MongoDatabase mongoDb = mongoClient.getDatabase(MongoConstants.AION_DB_NAME); // Gets the collection where we will be saving our values. Mongo creates it if it doesn't // yet exist this.collection = mongoDb.getCollection(this.name, BsonDocument.class); LOG.info("Finished opening the Mongo connection"); return isOpen(); }
Example #21
Source File: ConnectorValidationTest.java From mongo-kafka with Apache License 2.0 | 5 votes |
private boolean isReplicaSetOrSharded() { try (MongoClient mongoClient = MongoClients.create(getConnectionString())) { Document isMaster = mongoClient.getDatabase("admin").runCommand(BsonDocument.parse("{isMaster: 1}")); return isMaster.containsKey("setName") || isMaster.get("msg", "").equals("isdbgrid"); } catch (Exception e) { return false; } }
Example #22
Source File: MongoSourceTask.java From mongo-kafka with Apache License 2.0 | 5 votes |
private ChangeStreamIterable<Document> getChangeStreamIterable( final MongoSourceConfig sourceConfig, final MongoClient mongoClient) { String database = sourceConfig.getString(DATABASE_CONFIG); String collection = sourceConfig.getString(COLLECTION_CONFIG); Optional<List<Document>> pipeline = sourceConfig.getPipeline(); ChangeStreamIterable<Document> changeStream; if (database.isEmpty()) { LOGGER.info("Watching all changes on the cluster"); changeStream = pipeline.map(mongoClient::watch).orElse(mongoClient.watch()); } else if (collection.isEmpty()) { LOGGER.info("Watching for database changes on '{}'", database); MongoDatabase db = mongoClient.getDatabase(database); changeStream = pipeline.map(db::watch).orElse(db.watch()); } else { LOGGER.info("Watching for collection changes on '{}.{}'", database, collection); MongoCollection<Document> coll = mongoClient.getDatabase(database).getCollection(collection); changeStream = pipeline.map(coll::watch).orElse(coll.watch()); } int batchSize = sourceConfig.getInt(BATCH_SIZE_CONFIG); if (batchSize > 0) { changeStream.batchSize(batchSize); } sourceConfig.getFullDocument().ifPresent(changeStream::fullDocument); sourceConfig.getCollation().ifPresent(changeStream::collation); return changeStream; }
Example #23
Source File: MongoSourceTask.java From mongo-kafka with Apache License 2.0 | 5 votes |
private MongoCursor<BsonDocument> tryCreateCursor( final MongoSourceConfig sourceConfig, final MongoClient mongoClient, final BsonDocument resumeToken) { try { ChangeStreamIterable<Document> changeStreamIterable = getChangeStreamIterable(sourceConfig, mongoClient); if (resumeToken != null && supportsStartAfter) { LOGGER.info("Resuming the change stream after the previous offset: {}", resumeToken); changeStreamIterable.startAfter(resumeToken); } else if (resumeToken != null && !invalidatedCursor) { LOGGER.info("Resuming the change stream after the previous offset using resumeAfter."); changeStreamIterable.resumeAfter(resumeToken); } else { LOGGER.info("New change stream cursor created without offset."); } return changeStreamIterable.withDocumentClass(BsonDocument.class).iterator(); } catch (MongoCommandException e) { if (resumeToken != null) { if (e.getErrorCode() == 260) { invalidatedCursor = true; return tryCreateCursor(sourceConfig, mongoClient, null); } else if ((e.getErrorCode() == 9 || e.getErrorCode() == 40415) && e.getErrorMessage().contains("startAfter")) { supportsStartAfter = false; return tryCreateCursor(sourceConfig, mongoClient, resumeToken); } } LOGGER.info("Failed to resume change stream: {} {}", e.getErrorMessage(), e.getErrorCode()); return null; } }
Example #24
Source File: MongoCopyDataManager.java From mongo-kafka with Apache License 2.0 | 5 votes |
private static List<MongoNamespace> getCollections(final MongoClient mongoClient) { return mongoClient.listDatabaseNames().into(new ArrayList<>()).stream() .filter(s -> !(s.startsWith("admin") || s.startsWith("config") || s.startsWith("local"))) .map(d -> getCollections(mongoClient, d)) .flatMap(Collection::stream) .collect(Collectors.toList()); }
Example #25
Source File: MongoSinkTask.java From mongo-kafka with Apache License 2.0 | 5 votes |
private MongoClient getMongoClient() { if (mongoClient == null) { mongoClient = MongoClients.create( sinkConfig.getConnectionString(), getMongoDriverInformation(CONNECTOR_TYPE)); } return mongoClient; }
Example #26
Source File: MongoDBSourceTest.java From hazelcast-jet-contrib with Apache License 2.0 | 5 votes |
static MongoClient mongoClient(String connectionString, int connectionTimeoutSeconds) { MongoClientSettings settings = MongoClientSettings .builder() .applyConnectionString(new ConnectionString(connectionString)) .applyToClusterSettings(b -> { b.serverSelectionTimeout(connectionTimeoutSeconds, SECONDS); }) .build(); return MongoClients.create(settings); }
Example #27
Source File: UnitTestEmbeddedMongoClientFactory.java From stitch-android-sdk with Apache License 2.0 | 5 votes |
@Override protected MongoClient createClient(final String dbPath, final CodecRegistry codecRegistry) { return MongoClients.create(MongoClientSettings.builder() .dbPath(dbPath) .codecRegistry(codecRegistry) .build()); }
Example #28
Source File: MongoDBSourceBuilder.java From hazelcast-jet-contrib with Apache License 2.0 | 5 votes |
StreamContext( MongoClient client, ChangeStreamIterable<? extends T> changeStreamIterable, FunctionEx<? super ChangeStreamDocument<? extends T>, U> mapFn, ConsumerEx<? super MongoClient> destroyFn, FunctionEx<? super MongoClient, ? extends BsonTimestamp> startAtOperationTimeFn ) { this.client = client; this.changeStreamIterable = changeStreamIterable; this.mapFn = mapFn; this.destroyFn = destroyFn; this.timestamp = startAtOperationTimeFn == null ? null : startAtOperationTimeFn.apply(client); }
Example #29
Source File: MongoDBSourceBuilder.java From hazelcast-jet-contrib with Apache License 2.0 | 5 votes |
BatchContext( MongoClient client, MongoCollection<? extends T> collection, FunctionEx<? super MongoCollection<? extends T>, ? extends FindIterable<? extends T>> searchFn, FunctionEx<? super T, U> mapFn, ConsumerEx<? super MongoClient> destroyFn ) { this.client = client; this.mapFn = mapFn; this.destroyFn = destroyFn; cursor = searchFn.apply(collection).iterator(); }
Example #30
Source File: AndroidEmbeddedMongoClientFactory.java From stitch-android-sdk with Apache License 2.0 | 5 votes |
@Override protected MongoClient createClient(final String dbPath, final CodecRegistry codecRegistry) { return MongoClients.create(MongoClientSettings.builder() .dbPath(dbPath) .codecRegistry(codecRegistry) .build()); }