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

The following examples show how to use org.springframework.data.mongodb.core.MongoClientFactoryBean. 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: MongoTransactionOperateRepository.java    From Lottor with MIT License 6 votes vote down vote up
/**
 * 生成mongoClientFacotryBean
 *
 * @param config 配置信息
 * @return bean
 */
private MongoClientFactoryBean buildMongoClientFactoryBean(TxMongoConfig config) {
    MongoClientFactoryBean clientFactoryBean = new MongoClientFactoryBean();
    MongoCredential credential = MongoCredential.createScramSha1Credential(config.getMongoUserName(),
            config.getMongoDbName(),
            config.getMongoUserPwd().toCharArray());
    clientFactoryBean.setCredentials(new MongoCredential[]{
            credential
    });
    List<String> urls = Splitter.on(",").trimResults().splitToList(config.getMongoDbUrl());
    final ServerAddress[] serverAddresses = urls.stream().filter(Objects::nonNull)
            .map(url -> {
                List<String> adds = Splitter.on(":").trimResults().splitToList(url);
                return new ServerAddress(adds.get(0), Integer.valueOf(adds.get(1)));
            }).collect(Collectors.toList()).toArray(new ServerAddress[urls.size()]);

    clientFactoryBean.setReplicaSetSeeds(serverAddresses);
    return clientFactoryBean;
}
 
Example #2
Source File: MongoTransactionRecoverRepository.java    From Raincat with GNU Lesser General Public License v3.0 6 votes vote down vote up
private MongoClientFactoryBean buildMongoClientFactoryBean(final TxMongoConfig config) {
    MongoClientFactoryBean clientFactoryBean = new MongoClientFactoryBean();
    MongoCredential credential = MongoCredential.createScramSha1Credential(config.getMongoUserName(),
            config.getMongoDbName(),
            config.getMongoUserPwd().toCharArray());
    clientFactoryBean.setCredentials(new MongoCredential[]{credential});
    List<String> urls = Splitter.on(",").trimResults().splitToList(config.getMongoDbUrl());
    final ServerAddress[] serverAddresses = urls.stream().filter(Objects::nonNull)
            .map(url -> {
                List<String> adds = Splitter.on(":").trimResults().splitToList(url);
                return new ServerAddress(adds.get(0), Integer.valueOf(adds.get(1)));
            }).collect(Collectors.toList()).toArray(new ServerAddress[urls.size()]);

    clientFactoryBean.setReplicaSetSeeds(serverAddresses);
    return clientFactoryBean;
}
 
Example #3
Source File: MongoCoordinatorRepository.java    From hmily with Apache License 2.0 6 votes vote down vote up
private MongoClientFactoryBean buildMongoClientFactoryBean(final HmilyMongoConfig hmilyMongoConfig) {
    MongoClientFactoryBean clientFactoryBean = new MongoClientFactoryBean();
    MongoCredential credential = MongoCredential.createScramSha1Credential(hmilyMongoConfig.getMongoUserName(),
            hmilyMongoConfig.getMongoDbName(),
            hmilyMongoConfig.getMongoUserPwd().toCharArray());
    clientFactoryBean.setCredentials(new MongoCredential[]{credential});

    List<String> urls = Lists.newArrayList(Splitter.on(",").trimResults().split(hmilyMongoConfig.getMongoDbUrl()));
    ServerAddress[] sds = new ServerAddress[urls.size()];
    for (int i = 0; i < sds.length; i++) {
        List<String> adds = Lists.newArrayList(Splitter.on(":").trimResults().split(urls.get(i)));
        InetSocketAddress address = new InetSocketAddress(adds.get(0), Integer.parseInt(adds.get(1)));
        sds[i] = new ServerAddress(address);
    }
    clientFactoryBean.setReplicaSetSeeds(sds);
    return clientFactoryBean;
}
 
Example #4
Source File: MongoTransactionOperateRepository.java    From Lottor with MIT License 5 votes vote down vote up
/**
 * 初始化操作
 *
 * @param modelName 模块名称
 * @param txConfig  配置信息
 */
@Override
public void init(String modelName, TxConfig txConfig) {
    collectionName = RepositoryPathUtils.buildMongoTableName(modelName);
    final TxMongoConfig txMongoConfig = txConfig.getTxMongoConfig();
    MongoClientFactoryBean clientFactoryBean = buildMongoClientFactoryBean(txMongoConfig);
    try {
        clientFactoryBean.afterPropertiesSet();
        template = new MongoTemplate(clientFactoryBean.getObject(), txMongoConfig.getMongoDbName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #5
Source File: MongoTransactionRecoverRepository.java    From Raincat with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void init(final String appName, final TxConfig txConfig) {
    collectionName = RepositoryPathUtils.buildMongoTableName(appName);
    final TxMongoConfig txMongoConfig = txConfig.getTxMongoConfig();
    MongoClientFactoryBean clientFactoryBean = buildMongoClientFactoryBean(txMongoConfig);
    try {
        clientFactoryBean.afterPropertiesSet();
        template = new MongoTemplate(clientFactoryBean.getObject(), txMongoConfig.getMongoDbName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #6
Source File: MongoCoordinatorRepository.java    From hmily with Apache License 2.0 5 votes vote down vote up
@Override
public void init(final String modelName, final HmilyConfig hmilyConfig) {
    collectionName = RepositoryPathUtils.buildMongoTableName(modelName);
    final HmilyMongoConfig hmilyMongoConfig = hmilyConfig.getHmilyMongoConfig();
    MongoClientFactoryBean clientFactoryBean = buildMongoClientFactoryBean(hmilyMongoConfig);
    try {
        clientFactoryBean.afterPropertiesSet();
        template = new MongoTemplate(Objects.requireNonNull(clientFactoryBean.getObject()), hmilyMongoConfig.getMongoDbName());
    } catch (Exception e) {
        LogUtil.error(LOGGER, "mongo init error please check you config:{}", e::getMessage);
        throw new HmilyRuntimeException(e);
    }
}
 
Example #7
Source File: MongoCoordinatorRepository.java    From myth with Apache License 2.0 5 votes vote down vote up
@Override
public void init(final String modelName, final MythConfig mythConfig) {
    collectionName = RepositoryPathUtils.buildMongoTableName(modelName);
    final MythMongoConfig tccMongoConfig = mythConfig.getMythMongoConfig();
    MongoClientFactoryBean clientFactoryBean = buildMongoClientFactoryBean(tccMongoConfig);
    try {
        clientFactoryBean.afterPropertiesSet();
        template = new MongoTemplate(Objects.requireNonNull(clientFactoryBean.getObject()), tccMongoConfig.getMongoDbName());
    } catch (Exception e) {
        throw new MythRuntimeException(e);
    }
}
 
Example #8
Source File: MongoCoordinatorRepository.java    From myth with Apache License 2.0 5 votes vote down vote up
private MongoClientFactoryBean buildMongoClientFactoryBean(final MythMongoConfig mythMongoConfig) {
    MongoClientFactoryBean clientFactoryBean = new MongoClientFactoryBean();
    MongoCredential credential = MongoCredential.createScramSha1Credential(mythMongoConfig.getMongoUserName(),
            mythMongoConfig.getMongoDbName(),
            mythMongoConfig.getMongoUserPwd().toCharArray());
    clientFactoryBean.setCredentials(new MongoCredential[]{credential});
    List<String> urls = Splitter.on(",").trimResults().splitToList(mythMongoConfig.getMongoDbUrl());
    final ServerAddress[] sds = urls.stream().map(url -> {
        List<String> adds = Splitter.on(":").trimResults().splitToList(url);
        InetSocketAddress address = new InetSocketAddress(adds.get(0), Integer.parseInt(adds.get(1)));
        return new ServerAddress(address);
    }).collect(Collectors.toList()).toArray(new ServerAddress[]{});
    clientFactoryBean.setReplicaSetSeeds(sds);
    return clientFactoryBean;
}