use of com.mongodb.client.MongoClient in project spring-boot by spring-projects.
the class EmbeddedMongoAutoConfigurationTests method customMongoServerConfiguration.
@Test
void customMongoServerConfiguration() {
loadWithValidVersion(CustomMongoConfiguration.class);
Map<String, MongoClient> mongoClients = this.context.getBeansOfType(MongoClient.class);
assertThat(mongoClients).isNotEmpty();
for (String mongoClientBeanName : mongoClients.keySet()) {
BeanDefinition beanDefinition = this.context.getBeanFactory().getBeanDefinition(mongoClientBeanName);
assertThat(beanDefinition.getDependsOn()).contains("customMongoServer");
}
}
use of com.mongodb.client.MongoClient in project spring-boot by spring-projects.
the class EmbeddedMongoAutoConfigurationTests method useRandomPortByDefault.
@Test
void useRandomPortByDefault() {
loadWithValidVersion();
assertThat(this.context.getBeansOfType(MongoClient.class)).hasSize(1);
MongoClient client = this.context.getBean(MongoClient.class);
Integer mongoPort = Integer.valueOf(this.context.getEnvironment().getProperty("local.mongo.port"));
assertThat(getPort(client)).isEqualTo(mongoPort);
}
use of com.mongodb.client.MongoClient in project cas by apereo.
the class MongoDbConnectionFactory method buildMongoDbClient.
/**
* Build mongo db client.
*
* @param mongo the mongo
* @return the mongo client
*/
public MongoClient buildMongoDbClient(final BaseMongoDbProperties mongo) {
val settingsBuilder = MongoClientSettings.builder();
if (StringUtils.isNotBlank(mongo.getClientUri())) {
LOGGER.debug("Using MongoDb client URI [{}] to connect to MongoDb instance", mongo.getClientUri());
settingsBuilder.applyConnectionString(new ConnectionString(mongo.getClientUri()));
} else {
val serverAddresses = mongo.getHost().split(",");
if (serverAddresses.length == 0) {
throw new BeanCreationException("Unable to build a MongoDb client without any hosts/servers defined");
}
val servers = new ArrayList<ServerAddress>(0);
if (serverAddresses.length > 1) {
LOGGER.debug("Multiple MongoDb server addresses are defined. Ignoring port [{}], " + "assuming ports are defined as part of the address", mongo.getPort());
Arrays.stream(serverAddresses).filter(StringUtils::isNotBlank).map(ServerAddress::new).forEach(servers::add);
} else {
val port = mongo.getPort() > 0 ? mongo.getPort() : DEFAULT_PORT;
LOGGER.debug("Found single MongoDb server address [{}] using port [{}]", mongo.getHost(), port);
val addr = new ServerAddress(mongo.getHost(), port);
servers.add(addr);
}
settingsBuilder.applyToClusterSettings(builder -> builder.hosts(servers));
val credential = buildMongoCredential(mongo);
settingsBuilder.credential(credential).writeConcern(WriteConcern.valueOf(mongo.getWriteConcern())).codecRegistry(CodecRegistries.fromRegistries(CodecRegistries.fromProviders(new BaseConverters.ZonedDateTimeCodecProvider()), MongoClientSettings.getDefaultCodecRegistry())).readConcern(new ReadConcern(ReadConcernLevel.valueOf(mongo.getReadConcern()))).applyToConnectionPoolSettings(builder -> {
val poolConfig = mongo.getPool();
val pool = ConnectionPoolSettings.builder().maxConnectionLifeTime(Beans.newDuration(poolConfig.getLifeTime()).toMillis(), TimeUnit.MILLISECONDS).maxConnectionIdleTime(Beans.newDuration(poolConfig.getIdleTime()).toMillis(), TimeUnit.MILLISECONDS).maxSize(poolConfig.getMaxSize()).minSize(poolConfig.getMinSize()).maxWaitTime(Beans.newDuration(poolConfig.getMaxWaitTime()).toMillis(), TimeUnit.MILLISECONDS).build();
builder.applySettings(pool);
}).applyToSocketSettings(builder -> {
val socket = SocketSettings.builder().connectTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis(), TimeUnit.MILLISECONDS).readTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis(), TimeUnit.MILLISECONDS).build();
builder.applySettings(socket);
}).applyToSslSettings(builder -> {
val ssl = SslSettings.builder().enabled(mongo.isSslEnabled()).context(this.sslContext).build();
builder.applySettings(ssl);
}).applyToServerSettings(builder -> {
val server = ServerSettings.builder().heartbeatFrequency((int) Beans.newDuration(mongo.getTimeout()).toMillis(), TimeUnit.MILLISECONDS).build();
builder.applySettings(server);
}).retryWrites(mongo.isRetryWrites());
}
return MongoClients.create(settingsBuilder.build());
}
use of com.mongodb.client.MongoClient in project rocketmq-externals by apache.
the class MongoFactoryTest method testSSLTrustStore.
@Test
public void testSSLTrustStore() {
sourceTaskConfig.setMongoUserName("user_test");
sourceTaskConfig.setMongoPassWord("pwd_test");
sourceTaskConfig.setSsl(true);
sourceTaskConfig.setSslInvalidHostNameAllowed(true);
sourceTaskConfig.setTrustStore("/Users/home/test.pem");
sourceTaskConfig.setTrustStorePassword("test001");
sourceTaskConfig.setServerSelectionTimeoutMS(10000);
MongoClient client = mongoClientFactory.createMongoClient(replicaSetConfig);
MongoCollection<Document> collection = client.getDatabase("test").getCollection("person");
Document document = new Document();
document.put("name", "test");
collection.insertOne(document);
MongoCursor<Document> iterator = collection.find().iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
use of com.mongodb.client.MongoClient in project spring-data-mongodb by spring-projects.
the class MongoClientParserIntegrationTests method createsMongoClientWithDefaultsCorrectly.
// DATAMONGO-1158
@Test
public void createsMongoClientWithDefaultsCorrectly() {
reader.loadBeanDefinitions(new ClassPathResource("namespace/mongoClient-bean.xml"));
try (AbstractApplicationContext context = new GenericApplicationContext(factory)) {
context.refresh();
MongoClient client = context.getBean("mongoClient", MongoClient.class);
assertThat(client.getClusterDescription().getClusterSettings().getHosts()).containsExactly(new ServerAddress());
}
}
Aggregations