Search in sources :

Example 6 with ClusterSettings

use of com.mongodb.connection.ClusterSettings in project JavaForFun by gumartinm.

the class MongoConfiguration method reactiveMongoClient.

@Bean
@Override
public MongoClient reactiveMongoClient() {
    SocketSettings socketSettings = SocketSettings.builder().connectTimeout(30000, TimeUnit.MILLISECONDS).readTimeout(30000, TimeUnit.MILLISECONDS).build();
    ServerAddress serverAddress = new ServerAddress(host, port);
    ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Collections.singletonList(serverAddress)).build();
    ConnectionPoolSettings connectionPoolSettings = ConnectionPoolSettings.builder().maxWaitTime(15000, TimeUnit.MILLISECONDS).build();
    MongoClientSettings mongoClientSettings = MongoClientSettings.builder().applicationName(databaseName).socketSettings(socketSettings).clusterSettings(clusterSettings).connectionPoolSettings(connectionPoolSettings).build();
    return MongoClients.create(mongoClientSettings);
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) ServerAddress(com.mongodb.ServerAddress) MongoClientSettings(com.mongodb.async.client.MongoClientSettings) SocketSettings(com.mongodb.connection.SocketSettings) ConnectionPoolSettings(com.mongodb.connection.ConnectionPoolSettings) Bean(org.springframework.context.annotation.Bean)

Example 7 with ClusterSettings

use of com.mongodb.connection.ClusterSettings in project georocket by georocket.

the class MongoDBChunkReadStreamTest method createAsyncClient.

/**
 * Create an asynchronous MongoDB client
 * @return the client
 */
private com.mongodb.async.client.MongoClient createAsyncClient() {
    ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Arrays.asList(mongoConnector.serverAddress)).build();
    MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).build();
    return MongoClients.create(settings);
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) MongoClientSettings(com.mongodb.async.client.MongoClientSettings)

Example 8 with ClusterSettings

use of com.mongodb.connection.ClusterSettings in project mongo-java-driver by mongodb.

the class SingleServerClusterTest method setUpCluster.

private void setUpCluster(final ServerAddress serverAddress) {
    SocketStreamFactory streamFactory = new SocketStreamFactory(SocketSettings.builder().build(), getSslSettings());
    ClusterId clusterId = new ClusterId();
    ClusterSettings clusterSettings = ClusterSettings.builder().mode(ClusterConnectionMode.SINGLE).hosts(singletonList(serverAddress)).build();
    cluster = new SingleServerCluster(clusterId, clusterSettings, new DefaultClusterableServerFactory(ServerSettings.builder().build(), ConnectionPoolSettings.builder().maxSize(1).build(), InternalConnectionPoolSettings.builder().build(), streamFactory, streamFactory, getCredential(), null, null, null, Collections.<MongoCompressor>emptyList(), getServerApi()));
}
Also used : SocketStreamFactory(com.mongodb.connection.SocketStreamFactory) ClusterSettings(com.mongodb.connection.ClusterSettings) ClusterId(com.mongodb.connection.ClusterId)

Example 9 with ClusterSettings

use of com.mongodb.connection.ClusterSettings in project mongo-java-driver by mongodb.

the class DefaultClusterFactory method createCluster.

/**
 * Creates a cluster with the given settings.  The cluster mode will be based on the mode from the settings.
 *
 * @param originalClusterSettings        the cluster settings
 * @param originalServerSettings         the server settings
 * @param connectionPoolSettings the connection pool settings
 * @param internalConnectionPoolSettings the internal connection pool settings
 * @param streamFactory          the stream factory
 * @param heartbeatStreamFactory the heartbeat stream factory
 * @param credential             the credential, which may be null
 * @param commandListener        an optional listener for command-related events
 * @param applicationName        an optional application name to associate with connections to the servers in this cluster
 * @param mongoDriverInformation the optional driver information associate with connections to the servers in this cluster
 * @param compressorList         the list of compressors to request, in priority order
 * @param serverApi              the server api, which may be null
 * @return the cluster
 *
 * @since 3.6
 */
public Cluster createCluster(final ClusterSettings originalClusterSettings, final ServerSettings originalServerSettings, final ConnectionPoolSettings connectionPoolSettings, final InternalConnectionPoolSettings internalConnectionPoolSettings, final StreamFactory streamFactory, final StreamFactory heartbeatStreamFactory, @Nullable final MongoCredential credential, final CommandListener commandListener, final String applicationName, final MongoDriverInformation mongoDriverInformation, final List<MongoCompressor> compressorList, @Nullable final ServerApi serverApi) {
    ClusterId clusterId = new ClusterId();
    ClusterSettings clusterSettings;
    ServerSettings serverSettings;
    if (noClusterEventListeners(originalClusterSettings, originalServerSettings)) {
        clusterSettings = ClusterSettings.builder(originalClusterSettings).clusterListenerList(singletonList(NO_OP_CLUSTER_LISTENER)).build();
        serverSettings = ServerSettings.builder(originalServerSettings).serverListenerList(singletonList(NO_OP_SERVER_LISTENER)).serverMonitorListenerList(singletonList(NO_OP_SERVER_MONITOR_LISTENER)).build();
    } else {
        AsynchronousClusterEventListener clusterEventListener = AsynchronousClusterEventListener.startNew(clusterId, getClusterListener(originalClusterSettings), getServerListener(originalServerSettings), getServerMonitorListener(originalServerSettings));
        clusterSettings = ClusterSettings.builder(originalClusterSettings).clusterListenerList(singletonList(clusterEventListener)).build();
        serverSettings = ServerSettings.builder(originalServerSettings).serverListenerList(singletonList(clusterEventListener)).serverMonitorListenerList(singletonList(clusterEventListener)).build();
    }
    DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory = new DefaultDnsSrvRecordMonitorFactory(clusterId, serverSettings);
    if (clusterSettings.getMode() == ClusterConnectionMode.LOAD_BALANCED) {
        ClusterableServerFactory serverFactory = new LoadBalancedClusterableServerFactory(serverSettings, connectionPoolSettings, internalConnectionPoolSettings, streamFactory, credential, commandListener, applicationName, mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(), compressorList, serverApi);
        return new LoadBalancedCluster(clusterId, clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
    } else {
        ClusterableServerFactory serverFactory = new DefaultClusterableServerFactory(serverSettings, connectionPoolSettings, internalConnectionPoolSettings, streamFactory, heartbeatStreamFactory, credential, commandListener, applicationName, mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(), compressorList, serverApi);
        if (clusterSettings.getMode() == ClusterConnectionMode.SINGLE) {
            return new SingleServerCluster(clusterId, clusterSettings, serverFactory);
        } else if (clusterSettings.getMode() == ClusterConnectionMode.MULTIPLE) {
            if (clusterSettings.getSrvHost() == null) {
                return new MultiServerCluster(clusterId, clusterSettings, serverFactory);
            } else {
                return new DnsMultiServerCluster(clusterId, clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
            }
        } else {
            throw new UnsupportedOperationException("Unsupported cluster mode: " + clusterSettings.getMode());
        }
    }
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) ClusterId(com.mongodb.connection.ClusterId) ServerSettings(com.mongodb.connection.ServerSettings)

Example 10 with ClusterSettings

use of com.mongodb.connection.ClusterSettings in project mongo-java-driver by mongodb.

the class AbstractServerDiscoveryAndMonitoringTest method init.

protected void init(final ServerListenerFactory serverListenerFactory, final ClusterListener clusterListener) {
    ConnectionString connectionString = new ConnectionString(definition.getString("uri").getValue());
    ClusterSettings settings = ClusterSettings.builder().applyConnectionString(connectionString).serverSelectionTimeout(1, TimeUnit.SECONDS).build();
    ClusterId clusterId = new ClusterId();
    factory = new DefaultTestClusterableServerFactory(settings.getMode(), serverListenerFactory);
    ClusterSettings clusterSettings = settings.getClusterListeners().contains(clusterListener) ? settings : ClusterSettings.builder(settings).addClusterListener(clusterListener).build();
    if (settings.getMode() == ClusterConnectionMode.SINGLE) {
        cluster = new SingleServerCluster(clusterId, clusterSettings, factory);
    } else if (settings.getMode() == ClusterConnectionMode.MULTIPLE) {
        cluster = new MultiServerCluster(clusterId, clusterSettings, factory);
    } else {
        cluster = new LoadBalancedCluster(clusterId, clusterSettings, factory, null);
    }
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) ClusterId(com.mongodb.connection.ClusterId) ConnectionString(com.mongodb.ConnectionString)

Aggregations

ClusterSettings (com.mongodb.connection.ClusterSettings)24 ServerAddress (com.mongodb.ServerAddress)18 ClusterId (com.mongodb.connection.ClusterId)15 RepeatedTest (org.junit.jupiter.api.RepeatedTest)12 Test (org.junit.jupiter.api.Test)10 FutureResultCallback (com.mongodb.async.FutureResultCallback)6 ServerSelector (com.mongodb.selector.ServerSelector)6 MongoTimeoutException (com.mongodb.MongoTimeoutException)5 ConnectionString (com.mongodb.ConnectionString)4 MongoClientSettings (com.mongodb.async.client.MongoClientSettings)4 ConnectionPoolSettings (com.mongodb.connection.ConnectionPoolSettings)3 ServerSettings (com.mongodb.connection.ServerSettings)3 SocketSettings (com.mongodb.connection.SocketSettings)3 ArrayList (java.util.ArrayList)3 MongoClientException (com.mongodb.MongoClientException)2 MongoConfigurationException (com.mongodb.MongoConfigurationException)2 MongoCredential (com.mongodb.MongoCredential)2 Builder (com.mongodb.async.client.MongoClientSettings.Builder)2 SslSettings (com.mongodb.connection.SslSettings)2 Duration (java.time.Duration)2