下面列出了怎么用com.mongodb.async.client.MongoClientSettings的API类实例代码及写法,或者点击链接到github查看源代码。
private static void assertWithExpected(final DittoMongoClient mongoClient, final boolean sslEnabled,
final boolean withCredentials) {
final MongoClientSettings mongoClientSettings = mongoClient.getSettings();
assertThat(mongoClientSettings.getClusterSettings().getHosts())
.isEqualTo(Collections.singletonList(new ServerAddress(KNOWN_SERVER_ADDRESS)));
final List<MongoCredential> expectedCredentials = withCredentials ? Collections.singletonList(
MongoCredential.createCredential(KNOWN_USER, KNOWN_DB_NAME, KNOWN_PASSWORD.toCharArray())) :
Collections.emptyList();
assertThat(mongoClientSettings.getCredentialList()).isEqualTo(
expectedCredentials);
assertThat(mongoClientSettings.getSslSettings().isEnabled()).isEqualTo(sslEnabled);
final MongoDatabase mongoDatabase = mongoClient.getDefaultDatabase();
assertThat(mongoDatabase).isNotNull();
assertThat(mongoDatabase.getName()).isEqualTo(KNOWN_DB_NAME);
}
@Override
public void afterPropertiesSet() throws Exception {
final IMongodConfig mongodConfig = new MongodConfigBuilder()
.version(Version.Main.PRODUCTION).build();
IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
.defaultsWithLogger(Command.MongoD, logger)
.processOutput(ProcessOutput.getDefaultInstanceSilent())
.build();
MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
MongodExecutable mongodExecutable = runtime.prepare(mongodConfig);
mongod = mongodExecutable.start();
// cluster configuration
ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Collections.singletonList(new ServerAddress(mongodConfig.net().getServerAddress().getHostName(), mongodConfig.net().getPort()))).build();
// codec configuration
CodecRegistry pojoCodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry(),
fromProviders(PojoCodecProvider.builder().automatic(true).build()));
MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).codecRegistry(pojoCodecRegistry).writeConcern(WriteConcern.ACKNOWLEDGED).build();
mongoClient = MongoClients.create(settings);
mongoDatabase = mongoClient.getDatabase(databaseName);
}
@Override
public void afterPropertiesSet() throws Exception {
final IMongodConfig mongodConfig = new MongodConfigBuilder()
.version(Version.Main.PRODUCTION).build();
IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
.defaultsWithLogger(Command.MongoD, logger)
.processOutput(ProcessOutput.getDefaultInstanceSilent())
.build();
MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
MongodExecutable mongodExecutable = runtime.prepare(mongodConfig);
mongod = mongodExecutable.start();
// cluster configuration
ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Collections.singletonList(new ServerAddress(mongodConfig.net().getServerAddress().getHostName(), mongodConfig.net().getPort()))).build();
// codec configuration
CodecRegistry pojoCodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry(),
fromProviders(PojoCodecProvider.builder().automatic(true).build()));
MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).codecRegistry(pojoCodecRegistry).writeConcern(WriteConcern.ACKNOWLEDGED).build();
mongoClient = MongoClients.create(settings);
mongoDatabase = mongoClient.getDatabase(databaseName);
}
@Bean
public MongoClient mongoClient() throws IOException {
ServerAddress serverAddress = getServerAddress();
MongoClientSettings settings = MongoClientSettings.builder()
.clusterSettings(ClusterSettings.builder()
.hosts(singletonList(serverAddress))
.requiredClusterType(STANDALONE)
.build()).build();
return MongoClients.create(settings);
}
@Override
public MongoClientSettings getSettings() {
return wrapped.getSettings();
}
/**
* Gets the settings that this client uses to connect to server.
*
* <p>Note: {@link MongoClientSettings} is immutable.</p>
*
* @return the settings
*/
MongoClientSettings getSettings();
/**
* Create a new client with the given client settings.
*
* @param settings the settings
* @return the client
*/
public static MongoClient create(final MongoClientSettings settings) {
return create(settings, new NoopObservableAdapter());
}
/**
* Create a new client with the given client settings.
*
* @param settings the settings
* @param observableAdapter the {@link ObservableAdapter} to adapt all {@code Observables}
* @return the client
* @since 1.2
*/
public static MongoClient create(final MongoClientSettings settings, final ObservableAdapter observableAdapter) {
return create(settings, observableAdapter, null);
}
/**
* Creates a new client with the given client settings.
*
* <p>Note: Intended for driver and library authors to associate extra driver metadata with the connections.</p>
*
* @param settings the settings
* @param observableAdapter the {@link ObservableAdapter} to adapt all {@code Observables}.
* @param mongoDriverInformation any driver information to associate with the MongoClient
* @return the client
* @since 1.3
*/
public static MongoClient create(final MongoClientSettings settings, final ObservableAdapter observableAdapter,
final MongoDriverInformation mongoDriverInformation) {
return create(com.mongodb.async.client.MongoClients.create(settings, getMongoDriverInformation(mongoDriverInformation)),
observableAdapter);
}