Example usage for com.mongodb WriteConcern WriteConcern

List of usage examples for com.mongodb WriteConcern WriteConcern

Introduction

In this page you can find the example usage for com.mongodb WriteConcern WriteConcern.

Prototype

private WriteConcern(@Nullable final Object w, @Nullable final Integer wTimeoutMS,
            @Nullable final Boolean fsync, @Nullable final Boolean journal) 

Source Link

Usage

From source file:com.edgytech.umongo.OptionDialog.java

License:Apache License

WriteConcern getWriteConcern() {
    int w = getIntFieldValue(Item.writeFactor);
    String wPolicy = getStringFieldValue(Item.writePolicy);
    int wtimeout = getIntFieldValue(Item.writeTimeout);
    //        boolean fsync = getBooleanFieldValue(Item.fsync);
    boolean jsync = getBooleanFieldValue(Item.jsync);
    if (!wPolicy.trim().isEmpty())
        return new WriteConcern(wPolicy, wtimeout, false, jsync);
    return new WriteConcern(w, wtimeout, false, jsync);
}

From source file:com.torodb.mongowp.mongoserver.api.toro.ToroLastError.java

License:Open Source License

@SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
private WriteConcern getWriteConcern(Object w, boolean j, boolean fsync, int wtimeout) {
    WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED;

    if (w instanceof Number) {
        if (((Number) w).intValue() <= 1 && wtimeout > 0) {
            throw new IllegalArgumentException("wtimeout cannot be grater than 0 for w <= 1");
        }//from  w ww  .ja  v a2  s  . c  om

        writeConcern = new WriteConcern(((Number) w).intValue(), wtimeout, fsync, j);
    } else if (w instanceof String && w.equals("majority")) {
        if (wtimeout > 0) {
            throw new IllegalArgumentException("wtimeout cannot be grater than 0 for w <= 1");
        }

        writeConcern = new WriteConcern.Majority(wtimeout, fsync, j);
    } else {
        throw new IllegalArgumentException("w:" + w + " is not supported");
    }

    return writeConcern;
}

From source file:com.torodb.mongowp.mongoserver.api.toro.ToroQueryCommandProcessor.java

License:Open Source License

private WriteConcern getWriteConcern(BSONDocument document) {
    WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED;
    if (document.hasKey("writeConcern")) {
        BSONObject writeConcernObject = (BSONObject) document.getValue("writeConcern");
        Object w = writeConcernObject.get("w");
        int wtimeout = 0;
        boolean fsync = false;
        boolean j = false;
        boolean continueOnError = false;
        Object jObject = writeConcernObject.get("j");
        if (jObject != null && jObject instanceof Boolean && (Boolean) jObject) {
            fsync = true;/*from  w w  w .j a  v  a 2  s  .  c  o m*/
            j = true;
            continueOnError = true;
        }
        Object wtimeoutObject = writeConcernObject.get("wtimneout");
        if (wtimeoutObject != null && wtimeoutObject instanceof Number) {
            wtimeout = ((Number) wtimeoutObject).intValue();
        }
        if (w != null) {
            if (w instanceof Number) {
                if (((Number) w).intValue() <= 1 && wtimeout > 0) {
                    throw new IllegalArgumentException("wtimeout cannot be grater than 0 for w <= 1");
                }

                writeConcern = new WriteConcern(((Number) w).intValue(), wtimeout, fsync, j);
            } else if (w instanceof String && w.equals("majority")) {
                if (wtimeout > 0) {
                    throw new IllegalArgumentException("wtimeout cannot be grater than 0 for w <= 1");
                }

                writeConcern = new WriteConcern.Majority(wtimeout, fsync, j);
            } else {
                throw new IllegalArgumentException("w:" + w + " is not supported");
            }
        }
    }
    return writeConcern;
}

From source file:com.torodb.torod.mongodb.unsafe.ToroQueryCommandProcessor.java

License:Open Source License

private WriteConcern getWriteConcern(BsonDocument document) {
    WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED;
    if (document.containsKey("writeConcern")) {
        BsonDocument writeConcernObject = document.get("writeConcern").asDocument();
        BsonValue w = writeConcernObject.get("w");
        int wtimeout = 0;
        boolean fsync = false;
        boolean j = false;
        boolean continueOnError;
        BsonValue jObject = writeConcernObject.get("j");
        if (jObject != null && jObject.isBoolean() && jObject.asBoolean().getValue()) {
            fsync = true;//from  ww  w .  j  a v  a2  s  .  c  om
            j = true;
            continueOnError = true;
        }
        BsonValue wtimeoutObject = writeConcernObject.get("wtimneout");
        if (wtimeoutObject != null && wtimeoutObject.isNumber()) {
            wtimeout = wtimeoutObject.asNumber().intValue();
        }
        if (w != null) {
            if (w.isNumber()) {
                if (w.asNumber().intValue() <= 1 && wtimeout > 0) {
                    throw new IllegalArgumentException("wtimeout cannot be grater than 0 for w <= 1");
                }

                writeConcern = new WriteConcern(w.asNumber().intValue(), wtimeout, fsync, j);
            } else if (w.isString() && w.asString().getValue().equals("majority")) {
                if (wtimeout > 0) {
                    throw new IllegalArgumentException("wtimeout cannot be grater than 0 for w <= 1");
                }

                writeConcern = new WriteConcern.Majority(wtimeout, fsync, j);
            } else {
                throw new IllegalArgumentException("w:" + w + " is not supported");
            }
        }
    }
    return writeConcern;
}

From source file:org.forgerock.openidm.repo.mongodb.impl.MongoClientSingleton.java

License:Open Source License

public MongoClientSingleton init(JsonValue config) {
    if (client == null) {
        JsonValue connPerHost = config.get(MongoDBRepoService.CONFIG_CONN_PER_HOST);
        JsonValue connMultiple = config.get(MongoDBRepoService.CONFIG_CONN_MULTIPLIER);
        int connectionsPerHost = (connPerHost.isNull() ? 100 : connPerHost.asInteger());
        int connectonMultiple = (connMultiple.isNull() ? 5 : connMultiple.asInteger());

        int w = 1;
        int wtimeout = 0;
        boolean j = false;
        JsonValue wc_conf = config.get(MongoDBRepoService.CONFIG_WRITE_CONCERN);
        if (wc_conf != null) {
            JsonValue jv_w = wc_conf.get("w");
            JsonValue jv_wtimeout = wc_conf.get("wtimeout");
            JsonValue jv_j = wc_conf.get("j");
            w = (jv_w.isNull() ? 1 : jv_w.asInteger());
            wtimeout = (jv_wtimeout.isNull() ? 0 : jv_wtimeout.asInteger());
            j = (jv_j.isNull() ? false : jv_j.asBoolean());
        }//from  ww w.ja v  a 2s.  c o  m
        WriteConcern wc = new WriteConcern(w, wtimeout, false, j);

        MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(connectionsPerHost)
                .threadsAllowedToBlockForConnectionMultiplier(connectonMultiple).writeConcern(wc).build();
        List<ServerAddress> replicaSet = getReplicaSet(config);
        client = new MongoClient(replicaSet, options);
        db = getDB(config);
        logger.info("Create new MongoClient");
    }
    return this;
}

From source file:org.mongodb.demos.replication.RetryDemo.java

License:Apache License

public static void main(String args[]) throws Exception {

    //TODO : See https://gist.github.com/tgrall/954aa021ba420639d614

    MongoClient client = new MongoClient(
            Arrays.asList(new ServerAddress("localhost", 27017), new ServerAddress("localhost", 27018)));

    DB db = client.getDB("jug");
    DBCollection coll = db.getCollection("bar");

    System.out.println("BEFORE");

    boolean loop = true;

    while (loop) {

        int backoff = 0, counter = 0;

        DBObject obj = null;// w  w  w . j a  va2  s .c o  m

        do {
            try {
                obj = BasicDBObjectBuilder.start().add("name", "mydoc").add("counter", counter++).get();
                //System.out.print("\t inserting...");
                coll.insert(obj, new WriteConcern(2, 3000, true, false));
                backoff = 0;
                // System.out.println(" OK : Document inserted...");
            } catch (Exception e) {
                System.out.println(e.toString());
                if (backoff == 3) {
                    throw new Exception("Tried 3 times... still failed");
                }

                backoff++;
                System.out.println("Waiting for " + backoff + "s");
                Thread.sleep(1500 * backoff);
            }

        } while (backoff != 0);

    }

    System.out.println("AFTER");

}

From source file:org.wisdom.mongodb.MongoDBClient.java

License:Apache License

/**
 * Open a connection with a mongo database. 4 different configurations are currently available.
 *//*w  w  w  .  ja  v a  2s . c  om*/
private void openMongoConnection() {
    if (mongoClient != null) {
        mongoClient.close();
        mongoClient = null;
    }
    ServerAddress address = createAddress();
    MongoCredential credential = createMongoCredential();

    //TODO all the aspects should be configurable.
    final MongoClientOptions options = MongoClientOptions.builder().autoConnectRetry(autoConRetry)
            .connectTimeout(connectTimeout).maxAutoConnectRetryTime(maxAutoConnectRetryTime)
            .writeConcern(new WriteConcern(confMongoW, confMongoWTimeout, confMongoFsync, confMongoJ))
            .connectionsPerHost(connectionsPerHost).description(description).maxWaitTime(maxWaitTime).build();

    if (credential != null) {
        mongoClient = new MongoClient(address, Collections.singletonList(credential), options);
    } else {
        mongoClient = new MongoClient(address, options);
    }

}