/*
 * Copyright 2008-present MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mongodb.connection;

import com.mongodb.ConnectionString;
import com.mongodb.annotations.Immutable;

import java.util.concurrent.TimeUnit;

import static com.mongodb.assertions.Assertions.notNull;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

An immutable class representing socket settings used for connections to a MongoDB server.
Since:3.0
/** * An immutable class representing socket settings used for connections to a MongoDB server. * * @since 3.0 */
@Immutable public class SocketSettings { private final long connectTimeoutMS; private final long readTimeoutMS; private final boolean keepAlive; private final int receiveBufferSize; private final int sendBufferSize;
Gets a builder for an instance of SocketSettings.
Returns:the builder
/** * Gets a builder for an instance of {@code SocketSettings}. * @return the builder */
public static Builder builder() { return new Builder(); }
Creates a builder instance.
Params:
  • socketSettings – existing SocketSettings to default the builder settings on.
Returns:a builder
Since:3.7
/** * Creates a builder instance. * * @param socketSettings existing SocketSettings to default the builder settings on. * @return a builder * @since 3.7 */
public static Builder builder(final SocketSettings socketSettings) { return builder().applySettings(socketSettings); }
A builder for an instance of SocketSettings.
/** * A builder for an instance of {@code SocketSettings}. */
public static final class Builder { private long connectTimeoutMS = 10000; private long readTimeoutMS; private boolean keepAlive = true; private int receiveBufferSize; private int sendBufferSize; private Builder() { }
Applies the socketSettings to the builder

Note: Overwrites all existing settings

Params:
  • socketSettings – the socketSettings
Returns:this
Since:3.7
/** * Applies the socketSettings to the builder * * <p>Note: Overwrites all existing settings</p> * * @param socketSettings the socketSettings * @return this * @since 3.7 */
public Builder applySettings(final SocketSettings socketSettings) { notNull("socketSettings", socketSettings); connectTimeoutMS = socketSettings.connectTimeoutMS; readTimeoutMS = socketSettings.readTimeoutMS; keepAlive = socketSettings.keepAlive; receiveBufferSize = socketSettings.receiveBufferSize; sendBufferSize = socketSettings.sendBufferSize; return this; }
Sets the socket connect timeout.
Params:
  • connectTimeout – the connect timeout
  • timeUnit – the time unit
Returns:this
/** * Sets the socket connect timeout. * * @param connectTimeout the connect timeout * @param timeUnit the time unit * @return this */
public Builder connectTimeout(final int connectTimeout, final TimeUnit timeUnit) { this.connectTimeoutMS = MILLISECONDS.convert(connectTimeout, timeUnit); return this; }
Sets the socket read timeout.
Params:
  • readTimeout – the read timeout
  • timeUnit – the time unit
Returns:this
/** * Sets the socket read timeout. * * @param readTimeout the read timeout * @param timeUnit the time unit * @return this */
public Builder readTimeout(final int readTimeout, final TimeUnit timeUnit) { this.readTimeoutMS = MILLISECONDS.convert(readTimeout, timeUnit); return this; }
Sets keep-alive.
Params:
  • keepAlive – false if keep-alive should be disabled
See Also:
Returns:this
Deprecated:configuring keep-alive has been deprecated. It now defaults to true and disabling it is not recommended.
/** * Sets keep-alive. * * @param keepAlive false if keep-alive should be disabled * @return this * @deprecated configuring keep-alive has been deprecated. It now defaults to true and disabling it is not recommended. * @see <a href="https://docs.mongodb.com/manual/faq/diagnostics/#does-tcp-keepalive-time-affect-mongodb-deployments"> * Does TCP keep-alive time affect MongoDB Deployments?</a> */
@Deprecated public Builder keepAlive(final boolean keepAlive) { this.keepAlive = keepAlive; return this; }
Sets the receive buffer size.
Params:
  • receiveBufferSize – the receive buffer size
Returns:this
/** * Sets the receive buffer size. * * @param receiveBufferSize the receive buffer size * @return this */
public Builder receiveBufferSize(final int receiveBufferSize) { this.receiveBufferSize = receiveBufferSize; return this; }
Sets the send buffer size.
Params:
  • sendBufferSize – the send buffer size
Returns:this
/** * Sets the send buffer size. * * @param sendBufferSize the send buffer size * @return this */
public Builder sendBufferSize(final int sendBufferSize) { this.sendBufferSize = sendBufferSize; return this; }
Takes the settings from the given ConnectionString and applies them to the builder
Params:
  • connectionString – the connection string containing details of how to connect to MongoDB
See Also:
Returns:this
/** * Takes the settings from the given {@code ConnectionString} and applies them to the builder * * @param connectionString the connection string containing details of how to connect to MongoDB * @return this * @see com.mongodb.ConnectionString#getConnectTimeout() * @see com.mongodb.ConnectionString#getSocketTimeout() */
public Builder applyConnectionString(final ConnectionString connectionString) { Integer connectTimeout = connectionString.getConnectTimeout(); if (connectTimeout != null) { this.connectTimeout(connectTimeout, MILLISECONDS); } Integer socketTimeout = connectionString.getSocketTimeout(); if (socketTimeout != null) { this.readTimeout(socketTimeout, MILLISECONDS); } return this; }
Build an instance of SocketSettings.
Returns:the socket settings for this builder
/** * Build an instance of {@code SocketSettings}. * @return the socket settings for this builder */
public SocketSettings build() { return new SocketSettings(this); } }
Gets the timeout for socket connect. Defaults to 10 seconds.
Params:
  • timeUnit – the time unit to get the timeout in
Returns:the connect timeout in the requested time unit.
/** * Gets the timeout for socket connect. Defaults to 10 seconds. * * @param timeUnit the time unit to get the timeout in * @return the connect timeout in the requested time unit. */
public int getConnectTimeout(final TimeUnit timeUnit) { return (int) timeUnit.convert(connectTimeoutMS, MILLISECONDS); }
Gets the timeout for socket reads. Defaults to 0, which indicates no timeout
Params:
  • timeUnit – the time unit to get the timeout in
Returns:the read timeout in the requested time unit, or 0 if there is no timeout
/** * Gets the timeout for socket reads. Defaults to 0, which indicates no timeout * * @param timeUnit the time unit to get the timeout in * @return the read timeout in the requested time unit, or 0 if there is no timeout */
public int getReadTimeout(final TimeUnit timeUnit) { return (int) timeUnit.convert(readTimeoutMS, MILLISECONDS); }
Gets whether keep-alive is enabled. Defaults to true.
See Also:
Returns:true if keep-alive is enabled.
Deprecated:configuring keep-alive has been deprecated. It now defaults to true and disabling it is not recommended.
/** * Gets whether keep-alive is enabled. Defaults to true. * * @return true if keep-alive is enabled. * @deprecated configuring keep-alive has been deprecated. It now defaults to true and disabling it is not recommended. * @see <a href="https://docs.mongodb.com/manual/faq/diagnostics/#does-tcp-keepalive-time-affect-mongodb-deployments"> * Does TCP keep-alive time affect MongoDB Deployments?</a> */
@Deprecated public boolean isKeepAlive() { return keepAlive; }
Gets the receive buffer size. Defaults to the operating system default.
Returns:the receive buffer size
/** * Gets the receive buffer size. Defaults to the operating system default. * @return the receive buffer size */
public int getReceiveBufferSize() { return receiveBufferSize; }
Gets the send buffer size. Defaults to the operating system default.
Returns:the send buffer size
/** * Gets the send buffer size. Defaults to the operating system default. * * @return the send buffer size */
public int getSendBufferSize() { return sendBufferSize; } @Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } SocketSettings that = (SocketSettings) o; if (connectTimeoutMS != that.connectTimeoutMS) { return false; } if (keepAlive != that.keepAlive) { return false; } if (readTimeoutMS != that.readTimeoutMS) { return false; } if (receiveBufferSize != that.receiveBufferSize) { return false; } if (sendBufferSize != that.sendBufferSize) { return false; } return true; } @Override public int hashCode() { int result = (int) (connectTimeoutMS ^ (connectTimeoutMS >>> 32)); result = 31 * result + (int) (readTimeoutMS ^ (readTimeoutMS >>> 32)); result = 31 * result + (keepAlive ? 1 : 0); result = 31 * result + receiveBufferSize; result = 31 * result + sendBufferSize; return result; } @Override public String toString() { return "SocketSettings{" + "connectTimeoutMS=" + connectTimeoutMS + ", readTimeoutMS=" + readTimeoutMS + ", keepAlive=" + keepAlive + ", receiveBufferSize=" + receiveBufferSize + ", sendBufferSize=" + sendBufferSize + '}'; } SocketSettings(final Builder builder) { connectTimeoutMS = builder.connectTimeoutMS; readTimeoutMS = builder.readTimeoutMS; keepAlive = builder.keepAlive; receiveBufferSize = builder.receiveBufferSize; sendBufferSize = builder.sendBufferSize; } }