Skip to content

Instantly share code, notes, and snippets.

@jyemin
Last active March 15, 2023 05:10
Show Gist options
  • Save jyemin/0bfe2e37b31bd66db8de4fc5acaeb73c to your computer and use it in GitHub Desktop.
Save jyemin/0bfe2e37b31bd66db8de4fc5acaeb73c to your computer and use it in GitHub Desktop.
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.connection.SocketSettings;
import com.mongodb.connection.SocketStreamFactory;
import com.mongodb.connection.SslSettings;
import com.mongodb.connection.StreamFactory;
import com.mongodb.connection.StreamFactoryFactory;
import javax.net.SocketFactory;
public class SocketFactoryTest {
private static SocketFactory createCustomSocketFactory() {
return SocketFactory.getDefault();
}
private static class SocketStreamFactoryFactory implements StreamFactoryFactory {
private final SocketFactory socketFactory;
public SocketStreamFactoryFactory(SocketFactory socketFactory) {
this.socketFactory = socketFactory;
}
@Override
public StreamFactory create(final SocketSettings socketSettings, final SslSettings sslSettings) {
return new SocketStreamFactory(socketSettings, sslSettings, socketFactory);
}
}
public static void main(String[] args) {
SocketFactory socketFactory = createCustomSocketFactory();
ConnectionString connectionString = new ConnectionString("mongodb://localhost");
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.streamFactoryFactory(new SocketStreamFactoryFactory(socketFactory))
.build();
MongoClient client = MongoClients.create(settings);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment