List of usage examples for com.mongodb MongoClientOptions.Builder writeConcern
WriteConcern writeConcern
To view the source code for com.mongodb MongoClientOptions.Builder writeConcern.
Click Source Link
From source file:org.apache.jmeter.protocol.mongodb.config.MongoSourceElement.java
License:Apache License
@Override public void testStarted() { if (log.isDebugEnabled()) { log.debug(getTitle() + " testStarted"); }/*from www . j a va2 s .c om*/ MongoClientOptions.Builder builder = MongoClientOptions.builder().autoConnectRetry(getAutoConnectRetry()) .connectTimeout(getConnectTimeout()).connectionsPerHost(getConnectionsPerHost()) .maxAutoConnectRetryTime(getMaxAutoConnectRetryTime()).maxWaitTime(getMaxWaitTime()) .socketKeepAlive(getSocketKeepAlive()).socketTimeout(getSocketTimeout()) .threadsAllowedToBlockForConnectionMultiplier(getThreadsAllowedToBlockForConnectionMultiplier()); if (getSafe()) { builder.writeConcern(WriteConcern.SAFE); } else { builder.writeConcern(new WriteConcern(getWriteOperationNumberOfServers(), getWriteOperationTimeout(), getFsync(), getWaitForJournaling(), getContinueOnInsertError())); } MongoClientOptions mongoOptions = builder.build(); if (log.isDebugEnabled()) { log.debug("options : " + mongoOptions.toString()); } if (getThreadContext().getVariables().getObject(getSource()) != null) { if (log.isWarnEnabled()) { log.warn(getSource() + " has already been defined."); } } else { if (log.isDebugEnabled()) { log.debug(getSource() + " is being defined."); } try { getThreadContext().getVariables().putObject(getSource(), new MongoDB(MongoUtils.toServerAddresses(getConnection()), mongoOptions)); } catch (UnknownHostException e) { throw new IllegalStateException(e); } } }
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 om*/ 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 w w . j a v a 2 s .c om*/ 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//from w w w .jav a 2 s . co 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 va 2s .c om*/ 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 ww w . java 2s. 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.perftest.mongodb.nativeapi.NativeApiBenchmarkBase.java
License:LGPL
protected static MongoClient getMongoClient() throws UnknownHostException { ServerAddress serverAddress = new ServerAddress(properties.getProperty("host"), 27017); MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder(); optionsBuilder.connectTimeout(1000); optionsBuilder.writeConcern(WriteConcern.ACKNOWLEDGED); optionsBuilder.readPreference(ReadPreference.primary()); MongoClientOptions clientOptions = optionsBuilder.build(); MongoClient mongo = new MongoClient(serverAddress, clientOptions); return mongo; }
From source file:org.infinispan.loaders.mongodb.MongoDBCacheStore.java
License:Open Source License
@Override public void start() throws CacheLoaderException { super.start(); try {//ww w .ja v a 2 s .c om MongoClientOptions.Builder optionBuilder = new MongoClientOptions.Builder(); optionBuilder.connectTimeout(this.cfg.getTimeout()); WriteConcern writeConcern = new WriteConcern(this.cfg.getAcknowledgment()); optionBuilder.writeConcern(writeConcern); log.connectingToMongo(this.cfg.getHost(), this.cfg.getPort(), this.cfg.getTimeout(), this.cfg.getAcknowledgment()); ServerAddress serverAddress = new ServerAddress(this.cfg.getHost(), this.cfg.getPort()); this.mongo = new MongoClient(serverAddress, optionBuilder.build()); } catch (UnknownHostException e) { throw log.mongoOnUnknownHost(this.cfg.getHost()); } catch (RuntimeException e) { throw log.unableToInitializeMongoDB(e); } mongoDb = extractDatabase(); this.collection = mongoDb.getCollection(this.cfg.getCollectionName()); }
From source file:org.pentaho.mongo.MongoUtils.java
License:Open Source License
/** * Utility method to configure Mongo connection options * //from w w w . j a v a 2s. co m * @param optsBuilder 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 */ public static 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, "MongoUtils.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, "MongoUtils.Message.UsingReadPreferenceTagSets", //$NON-NLS-1$ builder.toString())); } } else { if (log != null) { log.logBasic(BaseMessages.getString(PKG, "MongoUtils.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, "MongoUtils.Message.ConfiguringWithDefaultWriteConcern")); } } 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); if (log != null) { String lwc = "w = " + writeConcern + ", wTimeout = " + wt + ", journaled = " + (new Boolean(journaled).toString()); log.logBasic( BaseMessages.getString(PKG, "MongoUtils.Message.ConfiguringWithWriteConcern", lwc)); } } 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); if (log != null) { String lwc = "w = " + writeConcern + ", wTimeout = " + wt + ", journaled = " + (new Boolean(journaled).toString()); log.logBasic( BaseMessages.getString(PKG, "MongoUtils.Message.ConfiguringWithWriteConcern", lwc)); } } } else { concern = new WriteConcern(1, wt, false, journaled); if (log != null) { String lwc = "w = 1" + ", wTimeout = " + wt + ", journaled = " + (new Boolean(journaled).toString()); log.logBasic( BaseMessages.getString(PKG, "MongoUtils.Message.ConfiguringWithWriteConcern", lwc)); } } } optsBuilder.writeConcern(concern); }
From source file:org.s1.mongodb.MongoDBConnectionHelper.java
License:Apache License
/** * * @param instance/*from w w w . j a v a 2s . c om*/ */ private static synchronized void initialize(String instance) { if (!connections.containsKey(instance)) { Map<String, Object> mopt = Options.getStorage().getMap(OPTIONS); Map<String, Object> m = Objects.get(mopt, "connections." + instance); if (Objects.isNullOrEmpty(m)) { m = Objects.get(mopt, "connections." + DEFAULT_INSTANCE); } MongoClientOptions.Builder b = MongoClientOptions.builder(); MongoClientOptions def_opt = MongoClientOptions.builder().build(); b.connectionsPerHost(Objects.get(m, "connectionsPerHost", def_opt.getConnectionsPerHost())); b.autoConnectRetry(Objects.get(m, "autoConnectRetry", def_opt.isAutoConnectRetry())); b.connectTimeout(Objects.get(m, "connectTimeout", def_opt.getConnectTimeout())); b.socketKeepAlive(Objects.get(m, "socketKeepAlive", def_opt.isSocketKeepAlive())); b.socketTimeout(Objects.get(m, "socketTimeout", def_opt.getSocketTimeout())); b.maxAutoConnectRetryTime( Objects.get(m, "maxAutoConnectRetryTime", def_opt.getMaxAutoConnectRetryTime())); b.maxWaitTime(Objects.get(m, "maxWaitTime", def_opt.getMaxWaitTime())); b.threadsAllowedToBlockForConnectionMultiplier( Objects.get(m, "threadsAllowedToBlockForConnectionMultiplier", def_opt.getThreadsAllowedToBlockForConnectionMultiplier())); b.writeConcern(WriteConcern.FSYNC_SAFE); MongoClientOptions opt = b.build(); MongoClient cl = null; try { cl = new MongoClient( new ServerAddress(Objects.get(m, "host", "localhost"), Objects.get(m, "port", 27017)), opt); } catch (UnknownHostException e) { throw S1SystemError.wrap(e); } String dbName = Objects.get(m, "name"); if (Objects.isNullOrEmpty(dbName)) throw new S1SystemError("Cannot initialize MongoDB connection, because name is not set"); DB db = cl.getDB(dbName); String user = Objects.get(m, "user"); String password = Objects.get(m, "password"); if (!Objects.isNullOrEmpty(user)) { if (!db.authenticate(user, password.toCharArray())) { throw new S1SystemError( "Cannot authenticate MongoDB connection " + dbName + " with user " + user); } } LOG.info("MongoDB connected " + cl.getAddress().getHost() + ":" + cl.getAddress().getPort()); connections.put(instance, db); } }