@Test public void testSocketSettings() { int connectTimeoutMS = Math.abs(TestUtils.randomInt()); int socketTimeoutMS = Math.abs(TestUtils.randomInt()); boolean keepAlive = TestUtils.randomBoolean(); int receiveBufferSize = Math.abs(TestUtils.randomInt()); int sendBufferSize = Math.abs(TestUtils.randomInt()); JsonObject config = new JsonObject(); config.put("connectTimeoutMS", connectTimeoutMS); config.put("socketTimeoutMS", socketTimeoutMS); config.put("keepAlive", keepAlive); config.put("receiveBufferSize", receiveBufferSize); config.put("sendBufferSize", sendBufferSize); SocketSettings settings = new SocketSettingsParser(null, config).settings(); assertEquals(connectTimeoutMS, settings.getConnectTimeout(TimeUnit.MILLISECONDS)); assertEquals(socketTimeoutMS, settings.getReadTimeout(TimeUnit.MILLISECONDS)); assertEquals(keepAlive, settings.isKeepAlive()); assertEquals(receiveBufferSize, settings.getReceiveBufferSize()); assertEquals(sendBufferSize, settings.getSendBufferSize()); }
public SocketSettingsParser(ConnectionString connectionString, JsonObject config) { SocketSettings.Builder settings = SocketSettings.builder(); if (connectionString != null) { settings.applyConnectionString(connectionString); } else { Integer connectTimeoutMS = config.getInteger("connectTimeoutMS"); if (connectTimeoutMS != null) { settings.connectTimeout(connectTimeoutMS, MILLISECONDS); } Integer socketTimeoutMS = config.getInteger("socketTimeoutMS"); if (socketTimeoutMS != null) { settings.readTimeout(socketTimeoutMS, MILLISECONDS); } Boolean keepAlive = config.getBoolean("keepAlive"); if (keepAlive != null) { settings.keepAlive(keepAlive); } Integer receiveBufferSize = config.getInteger("receiveBufferSize"); if (receiveBufferSize != null) { settings.receiveBufferSize(receiveBufferSize); } Integer sendBufferSize = config.getInteger("sendBufferSize"); if (sendBufferSize != null) { settings.sendBufferSize(sendBufferSize); } } this.settings = settings.build(); }
static SocketSettings socket(final String path, final ConnectionString cstr, final Config dbconf) { SocketSettings.Builder settings = SocketSettings.builder().applyConnectionString(cstr); withConf(path, dbconf, c -> { withMs("connectTimeout", c, s -> settings.connectTimeout(s.intValue(), TimeUnit.MILLISECONDS)); withBool("keepAlive", c, settings::keepAlive); withMs("readTimeout", c, s -> settings.readTimeout(s.intValue(), TimeUnit.MILLISECONDS)); withInt("receiveBufferSize", c, settings::receiveBufferSize); withInt("sendBufferSize", c, settings::sendBufferSize); }); return settings.build(); }
private Block socket(final String db) { return unit -> { SocketSettings settings = Try.apply(() -> unit.get(SocketSettings.class)) .orElseGet(() -> unit.mock(SocketSettings.class)); unit.registerMock(SocketSettings.class, settings); SocketSettings.Builder builder = Try.apply(() -> unit.get(SocketSettings.Builder.class)) .orElseGet(() -> unit.mock(SocketSettings.Builder.class)); expect(builder.applyConnectionString(new ConnectionString(db))).andReturn(builder).times(2); expect(builder.build()).andReturn(settings).times(2); expect(SocketSettings.builder()).andReturn(builder).times(2); }; }
/** * Lazily instantiate the {@link MongoClient} instance. * * @return */ private MongoClient createMongoClient() { String host = applicationConfiguration.getMongoHost(); int port = applicationConfiguration.getMongoPort(); ConnectionString connectionString = new ConnectionString("mongodb://" + host + ":" + port); logger.info("Creating Mongo client for: {}:{}", host, port); MongoClientSettings mongoClientSettings = MongoClientSettings.builder() .applicationName("dragoman") .serverSettings( ServerSettings.builder() .applyConnectionString(connectionString) .addServerMonitorListener(new LoggingServerMonitorListener()) .addServerListener(new LoggingServerListener()) .build()) .clusterSettings( ClusterSettings.builder() .applyConnectionString(connectionString) .serverSelectionTimeout( applicationConfiguration.getMongoServerSelectionTimeout(), MILLISECONDS) .addClusterListener(new LoggingClusterListener()) .build()) .connectionPoolSettings( ConnectionPoolSettings.builder() .applyConnectionString(connectionString) .maxWaitTime( applicationConfiguration.getConnectionPoolMaxWaitTime(), MILLISECONDS) .minSize(applicationConfiguration.getConnectionPoolMinSize()) .maxSize(applicationConfiguration.getConnectionPoolMaxSize()) .addConnectionPoolListener(new LoggingConnectionPoolListener()) .build()) .socketSettings( SocketSettings.builder() .applyConnectionString(connectionString) .connectTimeout( applicationConfiguration.getMongoSocketConnectionTimeout(), MILLISECONDS) .readTimeout(applicationConfiguration.getMongoReadTimeout(), MILLISECONDS) .build()) .build(); return MongoClients.create(mongoClientSettings); }
public SocketSettings settings() { return settings; }