private <T> ListDatabasesIterable<T> createListDatabasesIterable(@Nullable final ClientSession clientSession, final Class<T> clazz) { return MongoIterables.listDatabasesOf(clientSession, clazz, getMongoClientOptions().getCodecRegistry(), ReadPreference.primary(), createOperationExecutor()); }
private <TResult> ChangeStreamIterable<TResult> createChangeStreamIterable(@Nullable final ClientSession clientSession, final List<? extends Bson> pipeline, final Class<TResult> resultClass) { MongoClientOptions clientOptions = getMongoClientOptions(); return MongoIterables.changeStreamOf(clientSession, "admin", clientOptions.getCodecRegistry(), clientOptions.getReadPreference(), clientOptions.getReadConcern(), createOperationExecutor(), pipeline, resultClass, ChangeStreamLevel.CLIENT); }
/** * @param databaseName the name of the database to retrieve * @return a {@code MongoDatabase} representing the specified database * @throws IllegalArgumentException if databaseName is invalid * @see MongoNamespace#checkDatabaseNameValidity(String) */ public MongoDatabase getDatabase(final String databaseName) { MongoClientOptions clientOptions = getMongoClientOptions(); return new MongoDatabaseImpl(databaseName, clientOptions.getCodecRegistry(), clientOptions.getReadPreference(), clientOptions.getWriteConcern(), clientOptions.getRetryWrites(), clientOptions.getReadConcern(), createOperationExecutor()); }
private DatastoreImpl(final Morphia morphia, final Mapper mapper, final MongoClient mongoClient, final MongoDatabase database) { this.morphia = morphia; this.mapper = mapper; this.mongoClient = mongoClient; this.database = database.withCodecRegistry(CodecRegistries.fromRegistries( mongoClient.getMongoClientOptions().getCodecRegistry(), MongoClientSettings.getDefaultCodecRegistry())); this.db = mongoClient.getDB(database.getName()); this.defConcern = mongoClient.getWriteConcern(); this.indexHelper = new IndexHelper(mapper, database); }
@Override public Map<String, ?> properties(RequestContext ctx) throws Exception { Map properties = new HashMap<>(); List<Resource> addressStates = new ArrayList<>(); for (ServerAddress serverAddress : mongoClient.getAllAddress()) { addressStates.add(new ServerAddressState(this, serverAddress)); } properties.put(SERVERS, addressStates); List<Resource> credentialStates = new ArrayList<>(); for (MongoCredential mongoCredential: mongoClient.getCredentialsList()) { credentialStates.add(new CredentialState(this, mongoCredential)); } properties.put(CredentialState.ID, credentialStates); WriteConcernState writeConcern = new WriteConcernState(this, mongoClient.getWriteConcern()); properties.put(WriteConcernState.ID, writeConcern); ReadPreferenceState readPreference = new ReadPreferenceState(this, mongoClient.getReadPreference()); properties.put(ReadPreferenceState.ID, readPreference); MongoClientOptionsState mongoClientOptions = new MongoClientOptionsState(this, mongoClient.getMongoClientOptions()); properties.put(MongoClientOptionsState.ID, mongoClientOptions); return properties; }
Morphia morphia = new Morphia(); result = morphia.createDatastore( mongo ,mongoURI.getDatabase()); System.out.println("create mongo datastore with options:"+result.getMongo().getMongoClientOptions()); }catch (Exception e)
logger.info("created mongo datastore with options:"+datastore.getMongo().getMongoClientOptions());
private <T> ListDatabasesIterable<T> createListDatabasesIterable(@Nullable final ClientSession clientSession, final Class<T> clazz) { return MongoIterables.listDatabasesOf(clientSession, clazz, getMongoClientOptions().getCodecRegistry(), ReadPreference.primary(), createOperationExecutor()); }
private <TResult> ChangeStreamIterable<TResult> createChangeStreamIterable(@Nullable final ClientSession clientSession, final List<? extends Bson> pipeline, final Class<TResult> resultClass) { MongoClientOptions clientOptions = getMongoClientOptions(); return MongoIterables.changeStreamOf(clientSession, "admin", clientOptions.getCodecRegistry(), clientOptions.getReadPreference(), clientOptions.getReadConcern(), createOperationExecutor(), pipeline, resultClass, ChangeStreamLevel.CLIENT); }
log.debug("MongoClient initialized with options: " + client.getMongoClientOptions().toString());
/** * Retrieves the database with the specified {@code name} on the {@code client}. * The database will be created if it does not already exist. * * @param client the MongoDB client, connected on the server * @param name the name of the database * * @return the database */ @Nonnull private MongoDatabase createDatabase(MongoClient client, String name) { final CodecRegistry registry = fromRegistries( client.getMongoClientOptions().getCodecRegistry(), fromProviders(PojoCodecProvider.builder().automatic(true).build())); return client.getDatabase(name) .withCodecRegistry(registry) .withWriteConcern(WriteConcern.MAJORITY) .withReadConcern(ReadConcern.MAJORITY); } }
/** * @param databaseName the name of the database to retrieve * @return a {@code MongoDatabase} representing the specified database * @throws IllegalArgumentException if databaseName is invalid * @see MongoNamespace#checkDatabaseNameValidity(String) */ public MongoDatabase getDatabase(final String databaseName) { MongoClientOptions clientOptions = getMongoClientOptions(); return new MongoDatabaseImpl(databaseName, clientOptions.getCodecRegistry(), clientOptions.getReadPreference(), clientOptions.getWriteConcern(), clientOptions.getRetryWrites(), clientOptions.getReadConcern(), createOperationExecutor()); }
public static void assertConfigProperties(MongoDbFactory connector, String writeConcern, Integer connectionsPerHost, Integer maxWaitTime) { if (connectionsPerHost == null) { connectionsPerHost = 100; // default } if (maxWaitTime == null) { maxWaitTime = 120000; // default } assertNotNull(connector); assertEquals(ReflectionTestUtils.getField(connector, "writeConcern"), writeConcern == null ? null : WriteConcern.valueOf(writeConcern)); MongoClient mongoClient = (MongoClient) ReflectionTestUtils.getField(connector, "mongoClient"); assertEquals(connectionsPerHost.intValue(), mongoClient.getMongoClientOptions().getConnectionsPerHost()); assertEquals(maxWaitTime.intValue(), mongoClient.getMongoClientOptions().getMaxWaitTime()); }
@Test public void keepAlive() throws Exception { Map<String, Object> config = newConfig(repoHome); config.put(DocumentNodeStoreServiceConfiguration.PROP_SO_KEEP_ALIVE, true); MockOsgi.setConfigForPid(context.bundleContext(), PID, config); MockOsgi.activate(service, context.bundleContext()); DocumentNodeStore store = context.getService(DocumentNodeStore.class); MongoDocumentStore mds = getMongoDocumentStore(store); MongoClient client = MongoDocumentStoreTestHelper.getClient(mds); assertTrue(client.getMongoClientOptions().isSocketKeepAlive()); }
@Test public void socketKeepAlive() throws Exception { assumeTrue(MongoUtils.isAvailable()); MongoClientOptions.Builder options = MongoConnection.getDefaultBuilder(); options.socketKeepAlive(false); MongoConnection c = new MongoConnection(MongoUtils.URL, options); try { assertFalse(c.getMongoClient().getMongoClientOptions().isSocketKeepAlive()); } finally { c.close(); } // default is with keep-alive (starting with 3.6 driver) c = new MongoConnection(MongoUtils.URL); try { assertTrue(c.getMongoClient().getMongoClientOptions().isSocketKeepAlive()); } finally { c.close(); } }
@Test public void presetOverride() throws Exception { MockOsgi.setConfigForPid(context.bundleContext(), Configuration.PRESET_PID, DocumentNodeStoreServiceConfiguration.PROP_SO_KEEP_ALIVE, true); MockOsgi.activate(preset, context.bundleContext()); Map<String, Object> config = newConfig(repoHome); config.put(DocumentNodeStoreServiceConfiguration.PROP_SO_KEEP_ALIVE, false); MockOsgi.setConfigForPid(context.bundleContext(), PID, config); MockOsgi.activate(service, context.bundleContext()); DocumentNodeStore store = context.getService(DocumentNodeStore.class); MongoDocumentStore mds = getMongoDocumentStore(store); MongoClient client = MongoDocumentStoreTestHelper.getClient(mds); assertFalse(client.getMongoClientOptions().isSocketKeepAlive()); }
@Test public void preset() throws Exception { MockOsgi.setConfigForPid(context.bundleContext(), Configuration.PRESET_PID, DocumentNodeStoreServiceConfiguration.PROP_SO_KEEP_ALIVE, true); MockOsgi.activate(preset, context.bundleContext()); MockOsgi.setConfigForPid(context.bundleContext(), PID, newConfig(repoHome)); MockOsgi.activate(service, context.bundleContext()); DocumentNodeStore store = context.getService(DocumentNodeStore.class); MongoDocumentStore mds = getMongoDocumentStore(store); assertNotNull(mds); MongoClient client = MongoDocumentStoreTestHelper.getClient(mds); assertTrue(client.getMongoClientOptions().isSocketKeepAlive()); }