org.springframework.data.mongodb.core.SimpleMongoDbFactory Java Examples

The following examples show how to use org.springframework.data.mongodb.core.SimpleMongoDbFactory. 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: MongoConfig.java    From iot-dc3 with Apache License 2.0 6 votes vote down vote up
public MongoDbFactory mongoDbFactory(MongoClientOptionProperties properties) {
    //创建客户端参数
    MongoClientOptions options = mongoClientOptions(properties);

    //创建客户端和Factory
    List<ServerAddress> serverAddresses = new ArrayList<>();
    for (String address : properties.getAddress()) {
        String[] hostAndPort = address.split(":");
        String host = hostAndPort[0];
        int port = Integer.parseInt(hostAndPort[1]);
        ServerAddress serverAddress = new ServerAddress(host, port);
        serverAddresses.add(serverAddress);
    }

    //创建认证客户端
    MongoCredential mongoCredential = MongoCredential
            .createScramSha1Credential(
                    properties.getUsername(),
                    properties.getAuthenticationDatabase() != null ? properties.getAuthenticationDatabase() : properties.getDatabase(),
                    properties.getPassword().toCharArray()
            );
    MongoClient mongoClient = new MongoClient(serverAddresses, mongoCredential, options);

    return new SimpleMongoDbFactory(mongoClient, properties.getDatabase());
}
 
Example #2
Source File: BeihuMongoDataAutoConfiguration.java    From beihu-boot with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean(MongoDbFactory.class)
public MongoDbFactorySupport<?> mongoDbFactory(ObjectProvider<MongoClient> mongo,
		ObjectProvider<com.mongodb.client.MongoClient> mongoClient) {
	MongoClient preferredClient = mongo.getIfAvailable();
	if (preferredClient != null) {
		return new SimpleMongoDbFactory(preferredClient,
				this.beihuMongoProperties.getMongoClientDatabase());
	}
	com.mongodb.client.MongoClient fallbackClient = mongoClient.getIfAvailable();
	if (fallbackClient != null) {
		return new SimpleMongoClientDbFactory(fallbackClient,
				this.beihuMongoProperties.getMongoClientDatabase());
	}
	throw new IllegalStateException("Expected to find at least one MongoDB client.");
}
 
Example #3
Source File: CustomMongoHealthIndicator.java    From hesperides with GNU General Public License v3.0 5 votes vote down vote up
static MongoClient getMongoClient(SimpleMongoDbFactory mongoClient) {
    try {
        Method privateMethod = MongoDbFactorySupport.class.getDeclaredMethod("getMongoClient", null);
        privateMethod.setAccessible(true);
        return (MongoClient) privateMethod.invoke(mongoClient);
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}
 
Example #4
Source File: MongoDbFactoryCreator.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
public SimpleMongoDbFactory configure(SimpleMongoDbFactory mongoDbFactory, MongoDbFactoryConfig config) {
	if (config != null && config.getWriteConcern() != null) {
		WriteConcern writeConcern = WriteConcern.valueOf(config.getWriteConcern());
		if (writeConcern != null) {
			mongoDbFactory.setWriteConcern(writeConcern);
		}
	}
	return mongoDbFactory;
}
 
Example #5
Source File: MongoLocalConfig.java    From spring-music with Apache License 2.0 5 votes vote down vote up
@Bean
public MongoDbFactory mongoDbFactory() {
    try {
        return new SimpleMongoDbFactory(new MongoClient(), "music");
    } catch (UnknownHostException e) {
        throw new RuntimeException("Error creating MongoDbFactory: " + e);
    }
}
 
Example #6
Source File: $ Spring Data MongoDB Setup.java    From allegro-intellij-templates with Apache License 2.0 5 votes vote down vote up
@Bean(name = "mongoTemplate")
public MongoTemplate createMongoTemplate() throws UnknownHostException {
    MongoClient mongoClient = new MongoClient(host, port);
    //TODO Configure additional MongoDB mongoClient settings if needed
    MongoDbFactory factory = new SimpleMongoDbFactory(mongoClient, database, new UserCredentials(username, password));
    MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), new MongoMappingContext());
    converter.setTypeMapper(new DefaultMongoTypeMapper(null));

    return new MongoTemplate(factory, converter);
}
 
Example #7
Source File: DatabasesForMongoDBCreator.java    From bluemix-cloud-connectors with Apache License 2.0 5 votes vote down vote up
public SimpleMongoDbFactory configure(SimpleMongoDbFactory mongoDbFactory, MongoDbFactoryConfig config) {
    if (config != null && config.getWriteConcern() != null) {
        WriteConcern writeConcern = WriteConcern.valueOf(config.getWriteConcern());
        if (writeConcern != null) {
            mongoDbFactory.setWriteConcern(writeConcern);
        }
    }
    return mongoDbFactory;
}
 
Example #8
Source File: CustomMongoHealthIndicator.java    From hesperides with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void doHealthCheck(Health.Builder builder) {
    // Les "hacks" d'appels à des méthodes privées sont nécessaires pour obtenir le serverSessionPoolInUseCount bien utile
    MongoClient mongoClient = getMongoClient((SimpleMongoDbFactory) this.mongoTemplate.getMongoDbFactory());
    builder.up()
            .withDetail("healthcheck", performHealthCheck(mongoTemplate))
            .withDetail("config", getMongoClientOptionsAsMap(mongoClient.getMongoClientOptions()))
            .withDetail("serverSessionPoolInUseCount", getServerSessionPool(mongoClient).getInUseCount())
            .withDetail("clusterTime", Optional.ofNullable(getCluster(mongoClient).getClusterTime()).map(BsonTimestamp::getValue).orElse(0L));
}
 
Example #9
Source File: MongoHealthProbe.java    From hesperides with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void bindTo(final MeterRegistry meterRegistry) {
    Gauge.builder(repoName + "_mongoHealthCheckQueryTime", this,
            probe -> performHealthCheck(probe.mongoTemplate).getExecTimeInMs())
            .tags("class", this.getClass().getSimpleName())
            .baseUnit("ms")
            .register(meterRegistry);
    Gauge.builder(repoName + "_mongoSessionPoolInUseCount", this,
            probe -> getServerSessionPool(getMongoClient((SimpleMongoDbFactory) probe.mongoTemplate.getMongoDbFactory())).getInUseCount())
            .tags("class", this.getClass().getSimpleName())
            .register(meterRegistry);
}
 
Example #10
Source File: MongoConfig.java    From XBDD with Apache License 2.0 4 votes vote down vote up
@Deprecated
@Bean(name = "mongoLegacyDb")
public DB mongoLegacyDb() {
	return new SimpleMongoDbFactory(legacyMongoClient(), getDatabaseName()).getLegacyDb();
}
 
Example #11
Source File: MongoConfig.java    From ZTuoExchange_framework with MIT License 4 votes vote down vote up
@Bean
public MongoDbFactory dbFactory() throws Exception {
    return new SimpleMongoDbFactory(getMongoClientURI());
}
 
Example #12
Source File: MongoDbFactoryCreator.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
private SimpleMongoDbFactory createMongoDbFactory(MongoServiceInfo serviceInfo, MongoClientOptions.Builder mongoOptionsToUse) throws UnknownHostException {
	MongoClientURI mongoClientURI = new MongoClientURI(serviceInfo.getUri(), mongoOptionsToUse);
	MongoClient mongo = new MongoClient(mongoClientURI);
	return new SimpleMongoDbFactory(mongo, mongoClientURI.getDatabase());
}
 
Example #13
Source File: MongoConfig.java    From ZTuoExchange_framework with MIT License 4 votes vote down vote up
@Bean
public MongoDbFactory dbFactory() throws Exception {
    return new SimpleMongoDbFactory(getMongoClientURI());
}
 
Example #14
Source File: MongoLocalConfig.java    From bearchoke with Apache License 2.0 4 votes vote down vote up
@Bean(name = "axonMongoDbFactory")
public SimpleMongoDbFactory axonMongoDbFactory() throws Exception {
    return mongoDbFactory();
}
 
Example #15
Source File: MongoLocalConfig.java    From bearchoke with Apache License 2.0 4 votes vote down vote up
@Bean(name = "mongoDbFactory")
public SimpleMongoDbFactory mongoDbFactory() throws Exception {
    return new SimpleMongoDbFactory(mongo(), environment.getProperty("mongodb.database"));
}
 
Example #16
Source File: OrganizationMongoConfig.java    From micro-service with MIT License 4 votes vote down vote up
@Bean
public MongoDbFactory organizationFactory() throws Exception {
	ServerAddress serverAddress = new ServerAddress(mongoProperties.getUri());
	return new SimpleMongoDbFactory(new MongoClient(serverAddress), mongoProperties.getDatabase());
}
 
Example #17
Source File: CompanyMongoConfig.java    From micro-service with MIT License 4 votes vote down vote up
@Primary
@Bean
public MongoDbFactory companyFactory() throws Exception {
	ServerAddress serverAddress = new ServerAddress(mongoProperties.getUri());
	return new SimpleMongoDbFactory(new MongoClient(serverAddress), mongoProperties.getDatabase());
}
 
Example #18
Source File: MongoDBConfig.java    From Spring with Apache License 2.0 4 votes vote down vote up
@Bean
public MongoDbFactory mongoDb() throws Exception {
	return new SimpleMongoDbFactory(new MongoClient(MONGO_HOST, MONGO_PORT), DB_NAME);
}
 
Example #19
Source File: AggregateTestConfiguration.java    From mongodb-aggregate-query-support with Apache License 2.0 4 votes vote down vote up
@Bean
public MongoDbFactory mongoDbFactory(MongoClient mongo, String dbName) {
  return new SimpleMongoDbFactory(mongo, dbName);
}
 
Example #20
Source File: MongoConfig.java    From XBDD with Apache License 2.0 4 votes vote down vote up
@Deprecated
@Bean(name = "mongoLegacyGrid")
public DB mongoLegacyGrid() {
	return new SimpleMongoDbFactory(legacyMongoClient(), "grid").getLegacyDb();
}
 
Example #21
Source File: AppConfig.java    From Spring with Apache License 2.0 4 votes vote down vote up
@Bean
public MongoDbFactory mongoDb() throws Exception {
    return new SimpleMongoDbFactory(new MongoClient(MONGO_HOST, MONGO_PORT), DB_NAME);
}
 
Example #22
Source File: DatabasesForMongoDBCreator.java    From bluemix-cloud-connectors with Apache License 2.0 4 votes vote down vote up
private SimpleMongoDbFactory createMongoDbFactory(DatabasesForMongoDBServiceInfo serviceInfo,
        MongoClientOptions.Builder mongoOptionsToUse) throws UnknownHostException {
    MongoClientURI mongoClientURI = new MongoClientURI(buildUriString(serviceInfo), mongoOptionsToUse);
    MongoClient mongo = new MongoClient(mongoClientURI);
    return new SimpleMongoDbFactory(mongo, mongoClientURI.getDatabase());
}
 
Example #23
Source File: MultipleMongoConfig.java    From canal-mongo with Apache License 2.0 4 votes vote down vote up
@Bean
@Primary
public MongoDbFactory naiveFactory() throws Exception {
    MongoClient client = new MongoClient(new MongoClientURI(mongoProperties.getNaive().getUri()));
    return new SimpleMongoDbFactory(client, mongoProperties.getNaive().getDatabase());
}
 
Example #24
Source File: MultipleMongoConfig.java    From canal-mongo with Apache License 2.0 4 votes vote down vote up
@Bean
public MongoDbFactory completeFactory() throws Exception {
    MongoClient client = new MongoClient(new MongoClientURI(mongoProperties.getComplete().getUri()));
    return new SimpleMongoDbFactory(client, mongoProperties.getComplete().getDatabase());
}
 
Example #25
Source File: MongoServerConfig.java    From OpenLRW with Educational Community License v2.0 4 votes vote down vote up
@Bean
public MongoDbFactory mongoDbFactory(MongoClient mongoClient) {
    return new SimpleMongoDbFactory(mongoClient, "test");
}
 
Example #26
Source File: MongoDBTestConfiguration.java    From mongodb-aggregate-query-support with Apache License 2.0 4 votes vote down vote up
@Bean
public MongoDbFactory mongoDbFactory(MongoClient mongo, String dbName) {
  return new SimpleMongoDbFactory(mongo, dbName);
}