use of com.mongodb.connection.ClusterConnectionMode in project mongo-java-driver by mongodb.
the class DefaultClusterableServerFactory method create.
@Override
public ClusterableServer create(final Cluster cluster, final ServerAddress serverAddress) {
ServerId serverId = new ServerId(cluster.getClusterId(), serverAddress);
ClusterConnectionMode clusterMode = cluster.getSettings().getMode();
SameObjectProvider<SdamServerDescriptionManager> sdamProvider = SameObjectProvider.uninitialized();
ServerMonitor serverMonitor = new DefaultServerMonitor(serverId, serverSettings, cluster.getClock(), // no credentials, compressor list, or command listener for the server monitor factory
new InternalStreamConnectionFactory(clusterMode, heartbeatStreamFactory, null, applicationName, mongoDriverInformation, emptyList(), null, serverApi), clusterMode, serverApi, sdamProvider);
ConnectionPool connectionPool = new DefaultConnectionPool(serverId, new InternalStreamConnectionFactory(clusterMode, streamFactory, credential, applicationName, mongoDriverInformation, compressorList, commandListener, serverApi), connectionPoolSettings, internalConnectionPoolSettings, sdamProvider);
ServerListener serverListener = singleServerListener(serverSettings);
SdamServerDescriptionManager sdam = new DefaultSdamServerDescriptionManager(cluster, serverId, serverListener, serverMonitor, connectionPool, clusterMode);
sdamProvider.initialize(sdam);
serverMonitor.start();
return new DefaultServer(serverId, clusterMode, connectionPool, new DefaultConnectionFactory(), serverMonitor, sdam, serverListener, commandListener, cluster.getClock(), true);
}
use of com.mongodb.connection.ClusterConnectionMode in project mongo-java-driver by mongodb.
the class AbstractConnectionPoolTest method setUp.
@Before
public void setUp() {
assumeFalse(skipTest);
ConnectionPoolSettings.Builder settingsBuilder = ConnectionPoolSettings.builder();
BsonDocument poolOptions = definition.getDocument("poolOptions", new BsonDocument());
if (poolOptions.containsKey("maxPoolSize")) {
settingsBuilder.maxSize(poolOptions.getNumber("maxPoolSize").intValue());
}
if (poolOptions.containsKey("minPoolSize")) {
settingsBuilder.minSize(poolOptions.getNumber("minPoolSize").intValue());
}
if (poolOptions.containsKey("maxIdleTimeMS")) {
settingsBuilder.maxConnectionIdleTime(poolOptions.getNumber("maxIdleTimeMS").intValue(), TimeUnit.MILLISECONDS);
}
if (poolOptions.containsKey("waitQueueTimeoutMS")) {
settingsBuilder.maxWaitTime(poolOptions.getNumber("waitQueueTimeoutMS").intValue(), TimeUnit.MILLISECONDS);
}
if (poolOptions.containsKey("backgroundThreadIntervalMS")) {
long intervalMillis = poolOptions.getNumber("backgroundThreadIntervalMS").longValue();
assertFalse(intervalMillis == 0);
if (intervalMillis < 0) {
settingsBuilder.maintenanceInitialDelay(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} else {
/* Using frequency/period instead of an interval as required by the specification is incorrect, for example,
* because it opens up a possibility to run the background thread non-stop if runs are as long as or longer than the period.
* Nevertheless, I am reusing what we already have in the driver instead of clogging up the implementation. */
settingsBuilder.maintenanceFrequency(poolOptions.getNumber("backgroundThreadIntervalMS").longValue(), TimeUnit.MILLISECONDS);
}
}
if (poolOptions.containsKey("maxConnecting")) {
settingsBuilder.maxConnecting(poolOptions.getInt32("maxConnecting").intValue());
}
listener = new TestConnectionPoolListener();
settingsBuilder.addConnectionPoolListener(listener);
settings = settingsBuilder.build();
InternalConnectionPoolSettings internalSettings = InternalConnectionPoolSettings.builder().prestartAsyncWorkManager(PRESTART_POOL_ASYNC_WORK_MANAGER_FILE_NAMES.contains(fileName)).build();
Style style = Style.of(definition.getString("style").getValue());
switch(style) {
case UNIT:
{
ServerId serverId = new ServerId(new ClusterId(), new ServerAddress("host1"));
pool = new DefaultConnectionPool(serverId, new TestInternalConnectionFactory(), settings, internalSettings, SameObjectProvider.initialized(mock(SdamServerDescriptionManager.class)));
break;
}
case INTEGRATION:
{
ServerId serverId = new ServerId(new ClusterId(), ClusterFixture.getPrimary());
ClusterConnectionMode connectionMode = ClusterConnectionMode.MULTIPLE;
SameObjectProvider<SdamServerDescriptionManager> sdamProvider = SameObjectProvider.uninitialized();
pool = new ConnectionIdAdjustingConnectionPool(new DefaultConnectionPool(serverId, new InternalStreamConnectionFactory(connectionMode, createStreamFactory(SocketSettings.builder().build(), ClusterFixture.getSslSettings()), ClusterFixture.getCredentialWithCache(), poolOptions.getString("appName", new BsonString(fileName + ": " + description)).getValue(), MongoDriverInformation.builder().build(), Collections.emptyList(), new TestCommandListener(), ClusterFixture.getServerApi()), settings, internalSettings, sdamProvider));
sdamProvider.initialize(new DefaultSdamServerDescriptionManager(mockedCluster(), serverId, mock(ServerListener.class), mock(ServerMonitor.class), pool, connectionMode));
setFailPoint();
break;
}
default:
{
throw new AssertionError(format("Style %s is not implemented", style));
}
}
if (internalSettings.isPrestartAsyncWorkManager()) {
waitForPoolAsyncWorkManagerStart();
}
}
use of com.mongodb.connection.ClusterConnectionMode in project spring-data-mongodb by spring-projects.
the class MongoClientSettingsFactoryBean method createInstance.
@Override
protected MongoClientSettings createInstance() {
Builder builder = //
MongoClientSettings.builder().readPreference(//
readPreference).writeConcern(//
writeConcern).readConcern(//
readConcern).codecRegistry(//
codecRegistry).applicationName(//
applicationName).autoEncryptionSettings(//
autoEncryptionSettings).applyToClusterSettings((settings) -> {
settings.serverSelectionTimeout(clusterServerSelectionTimeoutMS, TimeUnit.MILLISECONDS);
if (clusterConnectionMode != null) {
settings.mode(clusterConnectionMode);
}
settings.requiredReplicaSetName(clusterRequiredReplicaSetName);
if (!CollectionUtils.isEmpty(clusterHosts)) {
settings.hosts(clusterHosts);
}
settings.localThreshold(clusterLocalThresholdMS, TimeUnit.MILLISECONDS);
// settings.maxWaitQueueSize(clusterMaxWaitQueueSize);
settings.requiredClusterType(custerRequiredClusterType);
if (StringUtils.hasText(clusterSrvHost)) {
settings.srvHost(clusterSrvHost);
}
}).applyToConnectionPoolSettings((settings) -> {
settings.minSize(poolMinSize);
settings.maxSize(poolMaxSize);
settings.maxConnectionIdleTime(poolMaxConnectionIdleTimeMS, TimeUnit.MILLISECONDS);
settings.maxWaitTime(poolMaxWaitTimeMS, TimeUnit.MILLISECONDS);
settings.maxConnectionLifeTime(poolMaxConnectionLifeTimeMS, TimeUnit.MILLISECONDS);
// settings.maxWaitQueueSize(poolMaxWaitQueueSize);
settings.maintenanceFrequency(poolMaintenanceFrequencyMS, TimeUnit.MILLISECONDS);
settings.maintenanceInitialDelay(poolMaintenanceInitialDelayMS, TimeUnit.MILLISECONDS);
}).applyToServerSettings((settings) -> {
settings.minHeartbeatFrequency(serverMinHeartbeatFrequencyMS, TimeUnit.MILLISECONDS);
settings.heartbeatFrequency(serverHeartbeatFrequencyMS, TimeUnit.MILLISECONDS);
}).applyToSocketSettings((settings) -> {
settings.connectTimeout(socketConnectTimeoutMS, TimeUnit.MILLISECONDS);
settings.readTimeout(socketReadTimeoutMS, TimeUnit.MILLISECONDS);
settings.receiveBufferSize(socketReceiveBufferSize);
settings.sendBufferSize(socketSendBufferSize);
}).applyToSslSettings((settings) -> {
settings.enabled(sslEnabled);
if (sslEnabled) {
settings.invalidHostNameAllowed(sslInvalidHostNameAllowed);
try {
settings.context(StringUtils.hasText(sslProvider) ? SSLContext.getInstance(sslProvider) : SSLContext.getDefault());
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
});
if (streamFactoryFactory != null) {
builder = builder.streamFactoryFactory(streamFactoryFactory);
}
if (retryReads != null) {
builder = builder.retryReads(retryReads);
}
if (retryWrites != null) {
builder = builder.retryWrites(retryWrites);
}
if (uUidRepresentation != null) {
builder = builder.uuidRepresentation(uUidRepresentation);
}
if (serverApi != null) {
builder = builder.serverApi(serverApi);
}
return builder.build();
}
Aggregations