Last active
March 15, 2023 05:10
-
-
Save jyemin/0bfe2e37b31bd66db8de4fc5acaeb73c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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