Example usage for com.mongodb MongoClient MongoClient

List of usage examples for com.mongodb MongoClient MongoClient

Introduction

In this page you can find the example usage for com.mongodb MongoClient MongoClient.

Prototype

public MongoClient(final List<ServerAddress> seeds, final MongoCredential credential,
        final MongoClientOptions options) 

Source Link

Document

Creates an instance based on a list of replica set members or mongos servers.

Usage

From source file:com.epam.ta.reportportal.config.MongodbConfiguration.java

License:Open Source License

@Bean
@Profile("!unittest")
MongoClient mongo() throws UnknownHostException {
    MongoClientOptions.Builder mongoClientBuilder = MongoClientOptions.builder();
    mongoClientBuilder.connectionsPerHost(mongoProperties.getConnectionsPerHost())
            .threadsAllowedToBlockForConnectionMultiplier(
                    mongoProperties.getThreadsAllowedToBlockForConnectionMultiplier())
            .connectTimeout(mongoProperties.getConnectTimeout())
            .socketTimeout(mongoProperties.getSocketTimeout()).maxWaitTime(mongoProperties.getMaxWaitTime())
            .socketKeepAlive(mongoProperties.getSocketKeepAlive());

    List<MongoCredential> credentials = Collections.emptyList();
    if (!Strings.isNullOrEmpty(mongoProperties.getUser())
            && !Strings.isNullOrEmpty(mongoProperties.getPassword())) {
        credentials = Collections.singletonList(MongoCredential.createCredential(mongoProperties.getUser(),
                mongoProperties.getDbName(), mongoProperties.getPassword().toCharArray()));
    }//from   ww w .java  2  s . com

    return new MongoClient(new ServerAddress(mongoProperties.getHost(), mongoProperties.getPort()), credentials,
            mongoClientBuilder.build());
}

From source file:com.facebook.presto.mongodb.MongoClientModule.java

License:Apache License

@Singleton
@Provides//ww w  .j a va2s.co  m
public static MongoSession createMongoSession(TypeManager typeManager, MongoClientConfig config) {
    requireNonNull(config, "config is null");

    MongoClientOptions.Builder options = MongoClientOptions.builder();

    options.connectionsPerHost(config.getConnectionsPerHost()).connectTimeout(config.getConnectionTimeout())
            .socketTimeout(config.getSocketTimeout()).socketKeepAlive(config.getSocketKeepAlive())
            .maxWaitTime(config.getMaxWaitTime()).minConnectionsPerHost(config.getMinConnectionsPerHost())
            .readPreference(config.getReadPreference().getReadPreference())
            .writeConcern(config.getWriteConcern().getWriteConcern());

    if (config.getRequiredReplicaSetName() != null) {
        options.requiredReplicaSetName(config.getRequiredReplicaSetName());
    }

    MongoClient client = new MongoClient(config.getSeeds(), config.getCredentials(), options.build());

    return new MongoSession(typeManager, client, config);
}

From source file:com.github.maasdi.mongo.wrapper.UsernamePasswordMongoClientWrapper.java

License:Apache License

@Override
protected MongoClient getClient(MongoDbMeta meta, VariableSpace vars, LogChannelInterface log,
        List<ServerAddress> repSet, boolean useAllReplicaSetMembers, MongoClientOptions opts)
        throws KettleException {
    try {//from ww  w  .j  a v a  2  s . c o m
        List<MongoCredential> credList = new ArrayList<MongoCredential>();
        credList.add(getCredential(meta, vars));
        return (repSet.size() > 1 || (useAllReplicaSetMembers && repSet.size() >= 1)
                ? new MongoClient(repSet, credList, opts)
                : (repSet.size() == 1 ? new MongoClient(repSet.get(0), credList, opts)
                        : new MongoClient(new ServerAddress("localhost"), credList, opts))); //$NON-NLS-1$
    } catch (UnknownHostException u) {
        throw new KettleException(u);
    }
}

From source file:com.heisenberg.mongo.MongoWorkflowEngine.java

License:Apache License

@Override
protected void initializeStorageServices(WorkflowEngineConfiguration cfg) {
    MongoWorkflowEngineConfiguration configuration = (MongoWorkflowEngineConfiguration) cfg;
    JsonService jsonService = serviceRegistry.getService(JsonService.class);

    MongoClient mongoClient = new MongoClient(configuration.getServerAddresses(),
            configuration.getCredentials(), configuration.getOptionBuilder().build());

    this.db = mongoClient.getDB(configuration.getDatabaseName());
    configuration.registerService(db);// w ww . j av a2 s . c o m

    boolean isPretty = configuration.isPretty();

    MongoWorkflowStore processDefinitions = new MongoWorkflowStore(serviceRegistry);
    processDefinitions.dbCollection = db.getCollection(configuration.getWorkflowsCollectionName());
    processDefinitions.isPretty = isPretty;
    processDefinitions.fields = configuration.getProcessDefinitionFields();
    processDefinitions.writeConcernInsertProcessDefinition = processDefinitions
            .getWriteConcern(configuration.getWriteConcernInsertProcessDefinition());
    configuration.registerService(processDefinitions);

    MongoWorkflowInstanceStore workflowInstances = new MongoWorkflowInstanceStore(serviceRegistry);
    workflowInstances.dbCollection = db.getCollection(configuration.workflowInstancesCollectionName);
    workflowInstances.processEngine = this;
    workflowInstances.isPretty = isPretty;
    workflowInstances.fields = configuration.getProcessInstanceFields();
    workflowInstances.writeConcernStoreProcessInstance = workflowInstances
            .getWriteConcern(configuration.getWriteConcernInsertProcessInstance());
    workflowInstances.writeConcernFlushUpdates = workflowInstances
            .getWriteConcern(configuration.getWriteConcernFlushUpdates());
    configuration.registerService(workflowInstances);

    // TODO
    // MongoTaskService mongoTaskService = new MongoTaskService();
    configuration.registerService(new MemoryTaskService());

    MongoJobs mongoJobs = new MongoJobs(serviceRegistry);
    DBCollection jobsDbCollection = db.getCollection(configuration.getJobsCollectionName());
    mongoJobs.dbCollection = jobsDbCollection;
    mongoJobs.isPretty = configuration.isPretty();
    mongoJobs.fields = configuration.getJobFields();
    mongoJobs.writeConcernJobs = mongoJobs.getWriteConcern(configuration.getWriteConcernJobs());
    mongoJobs.lockOwner = configuration.getId();
    mongoJobs.jsonService = jsonService;
    configuration.registerService(mongoJobs);

    MongoJobService mongoJobService = new MongoJobService(serviceRegistry);
    mongoJobService.jobs = mongoJobs;
    configuration.registerService(mongoJobService);
}

From source file:com.meltmedia.dropwizard.mongo.MongoBundle.java

License:Apache License

MongoClient buildClient(MongoConfiguration configuration) {
    try {//  w  w  w . j  av a  2s  . c o m
        // build the seed server list.
        List<ServerAddress> servers = new ArrayList<>();
        for (Server seed : configuration.getSeeds()) {
            servers.add(new ServerAddress(seed.getHost(), seed.getPort()));
        }

        log.info("Found {} mongo seed servers", servers.size());
        for (ServerAddress server : servers) {
            log.info("Found mongo seed server {}:{}", server.getHost(), server.getPort());
        }

        // build the credentials
        Credentials credentialConfig = configuration.getCredentials();
        List<MongoCredential> credentials = credentialConfig == null ? Collections.<MongoCredential>emptyList()
                : Collections.singletonList(MongoCredential.createCredential(credentialConfig.getUserName(),
                        configuration.getDatabase(), credentialConfig.getPassword().toCharArray()));

        if (credentials.isEmpty()) {
            log.info("Found {} mongo credentials.", credentials.size());
        } else {
            for (MongoCredential credential : credentials) {
                log.info("Found mongo credential for {} on database {}.", credential.getUserName(),
                        credential.getSource());
            }
        }

        // build the options.
        MongoClientOptions options = new MongoClientOptions.Builder()
                .writeConcern(writeConcern(configuration.getWriteConcern())).build();

        log.info("Mongo database is {}", configuration.getDatabase());

        return new MongoClient(servers, credentials, options);
    } catch (Exception e) {
        throw new RuntimeException("Could not configure MongoDB client.", e);
    }
}