List of usage examples for com.mongodb MongoClientOptions.Builder connectTimeout
int connectTimeout
To view the source code for com.mongodb MongoClientOptions.Builder connectTimeout.
Click Source Link
From source file:com.edgytech.umongo.ConnectDialog.java
License:Apache License
MongoClientOptions getMongoClientOptions() { MongoClientOptions.Builder builder = MongoClientOptions.builder(); // moptions.connectionsPerHost = getIntFieldValue(Item.connectionsPerHost); // moptions.threadsAllowedToBlockForConnectionMultiplier = getIntFieldValue(Item.blockingThreadMultiplier); // moptions.maxWaitTime = getIntFieldValue(Item.maxWaitTime); builder.connectTimeout(getIntFieldValue(Item.connectTimeout)); builder.socketTimeout(getIntFieldValue(Item.socketTimeout)); // moptions.autoConnectRetry = getBooleanFieldValue(Item.autoConnectRetry); if (!getBooleanFieldValue(Item.safeWrites)) { builder.writeConcern(WriteConcern.NONE); }//from ww w . ja va2 s . c om // moptions.slaveOk = getBooleanFieldValue(Item.secondaryReads); if (getBooleanFieldValue(Item.secondaryReads)) { builder.readPreference(ReadPreference.secondaryPreferred()); } int stype = getIntFieldValue(Item.socketType); int proxy = getIntFieldValue(Item.proxyType); if (proxy == 1) { // SOCKS proxy final String host = getStringFieldValue(Item.proxyHost); final int port = getIntFieldValue(Item.proxyPort); builder.socketFactory(new SocketFactory() { @Override public Socket createSocket() throws IOException { SocketAddress addr = new InetSocketAddress(host, port); Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); Socket socket = new Socket(proxy); return socket; } @Override public Socket createSocket(String string, int i) throws IOException, UnknownHostException { SocketAddress addr = new InetSocketAddress(host, port); Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); Socket socket = new Socket(proxy); InetSocketAddress dest = new InetSocketAddress(string, i); socket.connect(dest); return socket; } @Override public Socket createSocket(String string, int i, InetAddress ia, int i1) throws IOException, UnknownHostException { throw new UnsupportedOperationException("Not supported yet."); } @Override public Socket createSocket(InetAddress ia, int i) throws IOException { SocketAddress addr = new InetSocketAddress(host, port); Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); Socket socket = new Socket(proxy); InetSocketAddress dest = new InetSocketAddress(ia, i); socket.connect(dest); return socket; } @Override public Socket createSocket(InetAddress ia, int i, InetAddress ia1, int i1) throws IOException { throw new UnsupportedOperationException("Not supported yet."); } }); // // authentication.. only supports 1 global for all proxies :( // final String user = getStringFieldValue(Item.proxyUser); // final String pwd = getStringFieldValue(Item.proxyPassword); // if (!user.isEmpty()) { // Authenticator.setDefault(new Authenticator() { // @Override // protected PasswordAuthentication getPasswordAuthentication() { // PasswordAuthentication p = new PasswordAuthentication(user, pwd.toCharArray()); // return p; // } // }); // } } if (stype == 1) { builder.socketFactory(SSLSocketFactory.getDefault()); } else if (stype == 2) { // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { } public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { } } }; try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); builder.socketFactory(sc.getSocketFactory()); } catch (Exception e) { } } return builder.build(); }
From source file:com.github.maasdi.mongo.wrapper.NoAuthMongoClientWrapper.java
License:Apache License
/** * Utility method to configure Mongo connection options * * @param optsBuilder// w w w . j a v a 2 s .c o m * an options builder * @param connTimeout * the connection timeout to use (can be null) * @param socketTimeout * the socket timeout to use (can be null) * @param readPreference * the read preference to use (can be null) * @param writeConcern * the writeConcern to use (can be null) * @param wTimeout * the w timeout to use (can be null) * @param journaled * whether to use journaled writes * @param tagSet * the tag set to use in conjunction with the read preference (can be null) * @param vars * variables to use * @param log * for logging * @throws KettleException * if a problem occurs */ private void configureConnectionOptions(MongoClientOptions.Builder optsBuilder, String connTimeout, String socketTimeout, String readPreference, String writeConcern, String wTimeout, boolean journaled, List<String> tagSet, VariableSpace vars, LogChannelInterface log) throws KettleException { // connection timeout if (!Const.isEmpty(connTimeout)) { String connS = vars.environmentSubstitute(connTimeout); try { int cTimeout = Integer.parseInt(connS); if (cTimeout > 0) { optsBuilder.connectTimeout(cTimeout); } } catch (NumberFormatException n) { throw new KettleException(n); } } // socket timeout if (!Const.isEmpty(socketTimeout)) { String sockS = vars.environmentSubstitute(socketTimeout); try { int sockTimeout = Integer.parseInt(sockS); if (sockTimeout > 0) { optsBuilder.socketTimeout(sockTimeout); } } catch (NumberFormatException n) { throw new KettleException(n); } } if (log != null) { String rpLogSetting = NamedReadPreference.PRIMARY.getName(); if (!Const.isEmpty(readPreference)) { rpLogSetting = readPreference; } log.logBasic( BaseMessages.getString(PKG, "MongoNoAuthWrapper.Message.UsingReadPreference", rpLogSetting)); //$NON-NLS-1$ } DBObject firstTagSet = null; DBObject[] remainingTagSets = new DBObject[0]; if (tagSet != null && tagSet.size() > 0) { if (tagSet.size() > 1) { remainingTagSets = new DBObject[tagSet.size() - 1]; } firstTagSet = (DBObject) JSON.parse(tagSet.get(0).trim()); for (int i = 1; i < tagSet.size(); i++) { remainingTagSets[i - 1] = (DBObject) JSON.parse(tagSet.get(i).trim()); } if (log != null && (!Const.isEmpty(readPreference) && !readPreference.equalsIgnoreCase(NamedReadPreference.PRIMARY.getName()))) { StringBuilder builder = new StringBuilder(); for (String s : tagSet) { builder.append(s).append(" "); //$NON-NLS-1$ } log.logBasic(BaseMessages.getString(PKG, "MongoNoAuthWrapper.Message.UsingReadPreferenceTagSets", //$NON-NLS-1$ builder.toString())); } } else { if (log != null) { log.logBasic( BaseMessages.getString(PKG, "MongoNoAuthWrapper.Message.NoReadPreferenceTagSetsDefined")); //$NON-NLS-1$ } } // read preference if (!Const.isEmpty(readPreference)) { String rp = vars.environmentSubstitute(readPreference); NamedReadPreference preference = NamedReadPreference.byName(rp); if ((firstTagSet != null) && (preference.getPreference() instanceof TaggableReadPreference)) { optsBuilder.readPreference(preference.getTaggableReadPreference(firstTagSet, remainingTagSets)); } else { optsBuilder.readPreference(preference.getPreference()); } } // write concern writeConcern = vars.environmentSubstitute(writeConcern); wTimeout = vars.environmentSubstitute(wTimeout); WriteConcern concern = null; if (Const.isEmpty(writeConcern) && Const.isEmpty(wTimeout) && !journaled) { // all defaults - timeout 0, journal = false, w = 1 concern = new WriteConcern(); concern.setWObject(new Integer(1)); if (log != null) { log.logBasic(BaseMessages.getString(PKG, "MongoNoAuthWrapper.Message.ConfiguringWithDefaultWriteConcern")); //$NON-NLS-1$ } } else { int wt = 0; if (!Const.isEmpty(wTimeout)) { try { wt = Integer.parseInt(wTimeout); } catch (NumberFormatException n) { throw new KettleException(n); } } if (!Const.isEmpty(writeConcern)) { // try parsing as a number first try { int wc = Integer.parseInt(writeConcern); concern = new WriteConcern(wc, wt, false, journaled); } catch (NumberFormatException n) { // assume its a valid string - e.g. "majority" or a custom // getLastError label associated with a tag set concern = new WriteConcern(writeConcern, wt, false, journaled); } } else { concern = new WriteConcern(1, wt, false, journaled); } if (log != null) { String lwc = "w = " + concern.getW() + ", wTimeout = " + concern.getWtimeout() + ", journaled = " + concern.getJ(); log.logBasic( BaseMessages.getString(PKG, "MongoNoAuthWrapper.Message.ConfiguringWithWriteConcern", lwc)); } } optsBuilder.writeConcern(concern); }
From source file:com.github.nlloyd.hornofmongo.adaptor.Mongo.java
License:Open Source License
private void initMongoConnection() throws UnknownHostException { if ((innerMongo == null)) { MongoClientOptions.Builder builder = MongoClientOptions.builder(); if (mongoOptions != null) { //Restore previous options builder.description(mongoOptions.description); builder.connectionsPerHost(mongoOptions.connectionsPerHost); builder.threadsAllowedToBlockForConnectionMultiplier( mongoOptions.threadsAllowedToBlockForConnectionMultiplier); builder.maxWaitTime(mongoOptions.maxWaitTime); builder.connectTimeout(mongoOptions.connectTimeout); builder.socketTimeout(mongoOptions.socketTimeout); builder.socketKeepAlive(mongoOptions.socketKeepAlive); }/*from w ww . jav a2 s . c o m*/ MongoClientOptions clientOptions = builder.dbEncoderFactory(HornOfMongoBSONEncoder.FACTORY).build(); this.innerMongo = new com.mongodb.MongoClient(this.hosts, clientOptions); if (options != 0) this.innerMongo.setOptions(options); } if (mongoScope.useMongoShellWriteConcern()) innerMongo.setWriteConcern(WriteConcern.UNACKNOWLEDGED); }
From source file:com.novemberain.quartz.mongodb.MongoDBJobStore.java
License:Open Source License
private Mongo connectToMongoDB() throws SchedulerConfigException { if (mongoUri != null) { return connectToMongoDB(mongoUri); }/*from w w w .j a v a 2 s .co m*/ MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder(); optionsBuilder.writeConcern(WriteConcern.SAFE); if (mongoOptionAutoConnectRetry != null) optionsBuilder.autoConnectRetry(mongoOptionAutoConnectRetry); if (mongoOptionMaxConnectionsPerHost != null) optionsBuilder.connectionsPerHost(mongoOptionMaxConnectionsPerHost); if (mongoOptionConnectTimeoutMillis != null) optionsBuilder.connectTimeout(mongoOptionConnectTimeoutMillis); if (mongoOptionSocketTimeoutMillis != null) optionsBuilder.socketTimeout(mongoOptionSocketTimeoutMillis); if (mongoOptionSocketKeepAlive != null) optionsBuilder.socketKeepAlive(mongoOptionSocketKeepAlive); if (mongoOptionThreadsAllowedToBlockForConnectionMultiplier != null) optionsBuilder.threadsAllowedToBlockForConnectionMultiplier( mongoOptionThreadsAllowedToBlockForConnectionMultiplier); MongoClientOptions options = optionsBuilder.build(); try { ArrayList<ServerAddress> serverAddresses = new ArrayList<ServerAddress>(); for (String a : addresses) { serverAddresses.add(new ServerAddress(a)); } return new MongoClient(serverAddresses, options); } catch (UnknownHostException e) { throw new SchedulerConfigException("Could not connect to MongoDB", e); } catch (MongoException e) { throw new SchedulerConfigException("Could not connect to MongoDB", e); } }
From source file:io.gravitee.repository.mongodb.common.MongoFactory.java
License:Apache License
private MongoClientOptions.Builder builder() { MongoClientOptions.Builder builder = MongoClientOptions.builder(); builder.writeConcern(WriteConcern.SAFE); Integer connectionsPerHost = readPropertyValue(propertyPrefix + "connectionsPerHost", Integer.class); Integer connectTimeout = readPropertyValue(propertyPrefix + "connectTimeout", Integer.class, 500); Integer maxWaitTime = readPropertyValue(propertyPrefix + "maxWaitTime", Integer.class); Integer socketTimeout = readPropertyValue(propertyPrefix + "socketTimeout", Integer.class, 500); Boolean socketKeepAlive = readPropertyValue(propertyPrefix + "socketKeepAlive", Boolean.class); Integer maxConnectionLifeTime = readPropertyValue(propertyPrefix + "maxConnectionLifeTime", Integer.class); Integer maxConnectionIdleTime = readPropertyValue(propertyPrefix + "maxConnectionIdleTime", Integer.class); // We do not want to wait for a server Integer serverSelectionTimeout = readPropertyValue(propertyPrefix + "serverSelectionTimeout", Integer.class, 0);/*from w ww .j a v a 2 s . co m*/ Integer minHeartbeatFrequency = readPropertyValue(propertyPrefix + "minHeartbeatFrequency", Integer.class); String description = readPropertyValue(propertyPrefix + "description", String.class, "gravitee.io"); Integer heartbeatConnectTimeout = readPropertyValue(propertyPrefix + "heartbeatConnectTimeout", Integer.class, 1000); Integer heartbeatFrequency = readPropertyValue(propertyPrefix + "heartbeatFrequency", Integer.class); Integer heartbeatSocketTimeout = readPropertyValue(propertyPrefix + "heartbeatSocketTimeout", Integer.class); Integer localThreshold = readPropertyValue(propertyPrefix + "localThreshold", Integer.class); Integer minConnectionsPerHost = readPropertyValue(propertyPrefix + "minConnectionsPerHost", Integer.class); Boolean sslEnabled = readPropertyValue(propertyPrefix + "sslEnabled", Boolean.class); Integer threadsAllowedToBlockForConnectionMultiplier = readPropertyValue( propertyPrefix + "threadsAllowedToBlockForConnectionMultiplier", Integer.class); Boolean cursorFinalizerEnabled = readPropertyValue(propertyPrefix + "cursorFinalizerEnabled", Boolean.class); if (connectionsPerHost != null) builder.connectionsPerHost(connectionsPerHost); if (maxWaitTime != null) builder.maxWaitTime(maxWaitTime); if (connectTimeout != null) builder.connectTimeout(connectTimeout); if (socketTimeout != null) builder.socketTimeout(socketTimeout); if (socketKeepAlive != null) builder.socketKeepAlive(socketKeepAlive); if (maxConnectionLifeTime != null) builder.maxConnectionLifeTime(maxConnectionLifeTime); if (maxConnectionIdleTime != null) builder.maxConnectionIdleTime(maxConnectionIdleTime); if (minHeartbeatFrequency != null) builder.minHeartbeatFrequency(minHeartbeatFrequency); if (description != null) builder.description(description); if (heartbeatConnectTimeout != null) builder.heartbeatConnectTimeout(heartbeatConnectTimeout); if (heartbeatFrequency != null) builder.heartbeatFrequency(heartbeatFrequency); if (heartbeatSocketTimeout != null) builder.heartbeatSocketTimeout(heartbeatSocketTimeout); if (localThreshold != null) builder.localThreshold(localThreshold); if (minConnectionsPerHost != null) builder.minConnectionsPerHost(minConnectionsPerHost); if (sslEnabled != null) builder.sslEnabled(sslEnabled); if (threadsAllowedToBlockForConnectionMultiplier != null) builder.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier); if (cursorFinalizerEnabled != null) builder.cursorFinalizerEnabled(cursorFinalizerEnabled); if (serverSelectionTimeout != null) builder.serverSelectionTimeout(serverSelectionTimeout); return builder; }
From source file:org.craftercms.commons.mongo.MongoClientOptionsFactory.java
License:Open Source License
@Override protected MongoClientOptions createInstance() throws Exception { MongoClientOptions.Builder builder = MongoClientOptions.builder(); builder.alwaysUseMBeans(this.alwaysUseMBeans); builder.connectionsPerHost(this.connectionsPerHost); builder.cursorFinalizerEnabled(this.cursorFinalizerEnabled); builder.connectTimeout(this.connectTimeout); builder.maxWaitTime(this.maxWaitTime); switch (this.readPreference) { case PRIMARY_READ_PREFERENCE: builder.readPreference(ReadPreference.primary()); break;//w ww .j av a 2 s . c o m case NEAREST_READ_PREFERENCE: builder.readPreference(ReadPreference.nearest()); break; case SECONDARY_READ_PREFERENCE: builder.readPreference(ReadPreference.secondary()); break; default: builder.readPreference(ReadPreference.primary()); break; } builder.writeConcern(WriteConcern.valueOf(this.writeConcern)); builder.threadsAllowedToBlockForConnectionMultiplier(this.threadsAllowedToBlockForConnectionMultiplier); return builder.build(); }
From source file:org.craftercms.studio.impl.repository.mongodb.data.ClientOptionsFactory.java
License:Open Source License
public void init() { MongoClientOptions.Builder builder = MongoClientOptions.builder(); builder.alwaysUseMBeans(this.alwaysUseMBeans); builder.autoConnectRetry(this.autoConnectRetry); builder.connectionsPerHost(this.connectionsPerHost); builder.cursorFinalizerEnabled(this.cursorFinalizerEnabled); builder.connectTimeout(this.connectTimeout); builder.maxAutoConnectRetryTime(this.maxAutoConnectRetryTime); builder.maxWaitTime(this.maxWaitTime); switch (this.readPreference) { case PRIMARY_READ_PREFERENCE: builder.readPreference(ReadPreference.primary()); break;/*w ww . j a v a 2 s. co m*/ case NEAREST_READ_PREFERENCE: builder.readPreference(ReadPreference.nearest()); break; case SECONDARY_READ_PREFERENCE: builder.readPreference(ReadPreference.secondary()); break; default: builder.readPreference(ReadPreference.primary()); break; } builder.socketKeepAlive(this.socketKeepAlive); builder.writeConcern(WriteConcern.valueOf(this.writeConcern)); builder.threadsAllowedToBlockForConnectionMultiplier(this.threadsAllowedToBlockForConnectionMultiplier); this.clientOptions = builder.build(); }
From source file:org.eclipselabs.emongo.components.MongoClientProviderComponent.java
License:Open Source License
private MongoClientOptions createMongoClientOptions(Map<String, Object> properties) { MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder(); String description = (String) properties.get(PROP_DESCRIPTION); if (description != null) optionsBuilder.description(description); Integer connectionsPerHost = (Integer) properties.get(PROP_CONNECTIONS_PER_HOST); if (connectionsPerHost != null) optionsBuilder.connectionsPerHost(connectionsPerHost); Integer threadsAllowedToBlockForConnectionMultiplier = (Integer) properties .get(PROP_THREADS_ALLOWED_TO_BLOCK_FOR_CONNECTION_MULTIPLIER); if (threadsAllowedToBlockForConnectionMultiplier != null) optionsBuilder/*ww w. j av a2 s. c o m*/ .threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier); Integer maxWaitTime = (Integer) properties.get(PROP_MAX_WAIT_TIME); if (maxWaitTime != null) optionsBuilder.maxWaitTime(maxWaitTime); Integer connectTimeout = (Integer) properties.get(PROP_CONNECT_TIMEOUT); if (connectTimeout != null) optionsBuilder.connectTimeout(connectTimeout); Integer socketTimeout = (Integer) properties.get(PROP_SOCKET_TIMEOUT); if (socketTimeout != null) optionsBuilder.socketTimeout(socketTimeout); Boolean socketKeepAlive = (Boolean) properties.get(PROP_SOCKET_KEEP_ALIVE); if (socketKeepAlive != null) optionsBuilder.socketKeepAlive(socketKeepAlive); Boolean autoConnectRetry = (Boolean) properties.get(PROP_AUTO_CONNECT_RETRY); if (autoConnectRetry != null) optionsBuilder.autoConnectRetry(autoConnectRetry); Long maxAutoConnectRetryTime = (Long) properties.get(PROP_MAX_AUTO_CONNECT_RETRY_TIME); if (maxAutoConnectRetryTime != null) optionsBuilder.maxAutoConnectRetryTime(maxAutoConnectRetryTime); Boolean continueOnInsertError = (Boolean) properties.get(PROP_CONTINUE_ON_INSERT_ERROR); if (continueOnInsertError == null) continueOnInsertError = Boolean.FALSE; Integer w = (Integer) properties.get(PROP_W); if (w == null) w = Integer.valueOf(1); Integer wtimeout = (Integer) properties.get(PROP_WTIMEOUT); if (wtimeout == null) wtimeout = Integer.valueOf(0); Boolean fsync = (Boolean) properties.get(PROP_FSYNC); if (fsync == null) fsync = Boolean.FALSE; Boolean j = (Boolean) properties.get(PROP_J); if (j == null) j = Boolean.FALSE; WriteConcern writeConcern = new WriteConcern(w, wtimeout, fsync, j, continueOnInsertError); optionsBuilder.writeConcern(writeConcern); return optionsBuilder.build(); }
From source file:org.hibernate.ogm.datastore.mongodb.configuration.impl.MongoDBConfiguration.java
License:LGPL
/** * Create a {@link MongoClientOptions} using the {@link MongoDBConfiguration}. * * @return the {@link MongoClientOptions} corresponding to the {@link MongoDBConfiguration} *//*from w ww . j a v a2 s . c o m*/ public MongoClientOptions buildOptions() { MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder(); optionsBuilder.connectTimeout(timeout); optionsBuilder.writeConcern(writeConcern); optionsBuilder.readPreference(readPreference); return optionsBuilder.build(); }
From source file:org.hibernate.ogm.datastore.mongodb.impl.configuration.MongoDBConfiguration.java
License:LGPL
/** * Create a {@link MongoClientOptions} using the {@link MongoDBConfiguration}. * * @return the {@link MongoClientOptions} corresponding to the {@link MongoDBConfiguration} *//*from w ww.ja va 2 s .c o m*/ public MongoClientOptions buildOptions() { MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder(); optionsBuilder.connectTimeout(timeout); optionsBuilder.writeConcern(writeConcern); optionsBuilder.readPreference(readPreference); return optionsBuilder.build(); }