Example usage for com.mongodb WriteConcern JOURNAL_SAFE

List of usage examples for com.mongodb WriteConcern JOURNAL_SAFE

Introduction

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

Prototype

WriteConcern JOURNAL_SAFE

To view the source code for com.mongodb WriteConcern JOURNAL_SAFE.

Click Source Link

Document

Exceptions are raised for network issues, and server errors; the write operation waits for the server to group commit to the journal file on disk.

Usage

From source file:ch.agent.crnickl.mongodb.MongoDB.java

License:Apache License

private WriteConcern getWriteConcernFromKeyword(String keyword) throws T2DBException {
    WriteConcern wc = null;/*from   w ww .  ja  v  a 2s.  c  o m*/
    if (keyword == null)
        keyword = "SAFE";
    WriteConcernKeyword k = null;
    try {
        k = WriteConcernKeyword.valueOf(keyword);
    } catch (IllegalArgumentException e) {
        throw T2DBMMsg.exception(e, J.J81020, keyword);
    }
    switch (k) {
    case NONE:
        wc = WriteConcern.NONE;
        break;
    case NORMAL:
        wc = WriteConcern.NORMAL;
        break;
    case SAFE:
        wc = WriteConcern.SAFE;
        break;
    case MAJORITY:
        wc = WriteConcern.MAJORITY;
        break;
    case FSYNC_SAFE:
        wc = WriteConcern.FSYNC_SAFE;
        break;
    case JOURNAL_SAFE:
        wc = WriteConcern.JOURNAL_SAFE;
        break;
    case REPLICAS_SAFE:
        wc = WriteConcern.REPLICAS_SAFE;
        break;
    default:
        throw new RuntimeException("bug: " + k.name());
    }
    if (wc != WriteConcern.SAFE)
        throw T2DBMMsg.exception(J.J81021, keyword);
    return wc;
}

From source file:com.bluedragon.profiler.ProfilerExtension.java

License:Open Source License

@Override
public void run() {
    cfEngine.log("ProfileExtension started logging to " + thisInst.mongourl);

    while (bRunning) {

        Map m = (Map) thisInst.producerConsumer.onConsume();
        if (m == null) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }/*from  w w w  . ja  va  2s  . c o m*/
            continue;
        }

        BasicDBObject dbo = new BasicDBObject(m);

        try {
            coll.save(dbo, WriteConcern.JOURNAL_SAFE);
        } catch (Exception e) {
            thisInst.producerConsumer.unConsume(m);

            try {
                Thread.sleep(1000);
            } catch (InterruptedException te) {
            }
        }

        Thread.yield();
    }

}

From source file:com.porvak.bracket.config.EmbeddedDataConfig.java

License:Apache License

@Bean
@Override/*  www .java  2s. c  o  m*/
public MongoTemplate mongoTemplate() throws Exception {
    MongoTemplate mongoTemplate = super.mongoTemplate();
    mongoTemplate.setWriteConcern(WriteConcern.JOURNAL_SAFE);
    mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);
    return mongoTemplate;
}

From source file:mongodb.JavaDocSave.java

public static void main(String[] args) {
    try {//ww w  .  j  ava  2  s  . c o m
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        mongoClient.setWriteConcern(WriteConcern.JOURNAL_SAFE);
        DB db = mongoClient.getDB("words");
        DBCollection collection = db.getCollection("word_stats");
        JavaDocSave.showWord(collection, "Before save");
        JavaDocSave.saveBlueDoc(collection);
        JavaDocSave.showWord(collection, "After save");
        JavaDocSave.resetDoc(collection);
        JavaDocSave.showWord(collection, "After reset");
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:mongodb.JavaDocUpdate.java

public static void main(String[] args) {
    try {//w  w w  .  j  a  va2  s  .  c o  m
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        mongoClient.setWriteConcern(WriteConcern.JOURNAL_SAFE);
        DB db = mongoClient.getDB("words");
        DBCollection collection = db.getCollection("word_stats");
        JavaDocUpdate.showWord(collection, "Before update");
        JavaDocUpdate.updateDoc(collection);
        JavaDocUpdate.showWord(collection, "After update");
        JavaDocUpdate.resetDoc(collection);
        JavaDocUpdate.showWord(collection, "After reset");
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:mongodb.JavaDocUpsert.java

public static void main(String[] args) {
    try {/*from   w  ww .  ja  v a  2s  . c  om*/
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        mongoClient.setWriteConcern(WriteConcern.JOURNAL_SAFE);
        DB db = mongoClient.getDB("words");
        DBCollection collection = db.getCollection("word_stats");
        JavaDocUpsert.showWord(collection, "Before upsert");
        JavaDocUpsert.addUpsert(collection);
        JavaDocUpsert.updateUpsert(collection);
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:org.apache.tapestry5.mongodb.modules.MongodbModule.java

License:Apache License

/**
 * Contribute coercions for {@link WriteConcern} and {@link ReadPreference} to have them from
 * {@link org.apache.tapestry5.ioc.annotations.Symbol}
 *
 * @param configuration lets help the {@link org.apache.tapestry5.ioc.services.TypeCoercer} service
 *///from  w w w  . j  a  v a  2 s  . c  om
public static void contributeTypeCoercer(Configuration<CoercionTuple> configuration) {
    configuration.add(new CoercionTuple(String.class, WriteConcern.class, new Coercion<String, WriteConcern>() {
        public WriteConcern coerce(String input) {
            if (input.equalsIgnoreCase("FSYNC_SAFE")) {
                return WriteConcern.FSYNC_SAFE;
            } else if (input.equalsIgnoreCase("JOURNAL_SAFE")) {
                return WriteConcern.JOURNAL_SAFE;
            } else if (input.equalsIgnoreCase("MAJORITY")) {
                return WriteConcern.MAJORITY;
            } else if (input.equalsIgnoreCase("NONE")) {
                return WriteConcern.NONE;
            } else if (input.equalsIgnoreCase("REPLICAS_SAFE")) {
                return WriteConcern.REPLICAS_SAFE;
            } else if (input.equalsIgnoreCase("SAFE")) {
                return WriteConcern.SAFE;
            } else if (input.equalsIgnoreCase("NORMAL")) {
                return WriteConcern.NORMAL;
            } else // WriteConcern.ACKNOWLEDGED IS OUR DEFAULT
            {
                return WriteConcern.ACKNOWLEDGED;
            }
        }
    }));

    configuration
            .add(new CoercionTuple(String.class, ReadPreference.class, new Coercion<String, ReadPreference>() {
                public ReadPreference coerce(String input) {
                    if (input.equalsIgnoreCase("SECONDARY")) {
                        return ReadPreference.secondary();
                    } else // PRIMARY IS OUR DEFAULT
                    {
                        return ReadPreference.primary();
                    }
                }
            }));
}

From source file:org.aw20.mongoworkbench.command.RemoveMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);

    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);

    DB db = mdb.getDB(sDb);/*from www .  j a v a2 s.c  om*/
    BasicDBObject cmdMap = parseMongoCommandString(db, cmd);

    if (!cmdMap.containsField("removeArgs"))
        throw new Exception("no remove document");

    DBCollection collection = db.getCollection(sColl);

    BasicDBList args = (BasicDBList) cmdMap.get("removeArgs");

    // Run the command
    db.requestStart();
    WriteResult writeresult;
    try {
        writeresult = collection.remove((DBObject) args.get(0), WriteConcern.JOURNAL_SAFE);
    } finally {
        db.requestDone();
    }

    // Get the result
    Map mwriteresult = (Map) JSON.parse(writeresult.toString());
    mwriteresult.put("exeDate", new Date());

    EventWorkBenchManager.getInst().onEvent(Event.WRITERESULT, mwriteresult);

    setMessage("Removed");
}

From source file:org.aw20.mongoworkbench.command.SaveMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);

    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);

    DB db = mdb.getDB(sDb);/*from   ww w  .  j  a  v  a 2  s .c  om*/
    BasicDBObject cmdMap = parseMongoCommandString(db, cmd);

    if (!cmdMap.containsField("saveArg"))
        throw new Exception("no save document");

    DBObject document = fixNumbers((BasicDBObject) cmdMap.get("saveArg"));
    DBCollection collection = db.getCollection(sColl);

    // Run the command
    db.requestStart();
    WriteResult writeresult;
    try {
        writeresult = collection.save(document, WriteConcern.JOURNAL_SAFE);
        id = document.get("_id");
    } finally {
        db.requestDone();
    }

    // Get the result
    Map mwriteresult = (Map) JSON.parse(writeresult.toString());
    mwriteresult.put("exeDate", new Date());

    EventWorkBenchManager.getInst().onEvent(Event.WRITERESULT, mwriteresult);

    setMessage("Saved: updatedExisting=" + mwriteresult.get("updatedExisting") + "; documentsUpdated="
            + mwriteresult.get("n"));
}

From source file:org.aw20.mongoworkbench.command.SystemJavaScriptWriteCommand.java

License:Open Source License

@Override
public void execute() throws Exception {

    MongoClient mdb = MongoFactory.getInst().getMongo(sName);

    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);
    DB db = mdb.getDB(sDb);//from   w  w w  . j  av  a2  s.c om

    DBCollection coll = db.getCollection("system.js");

    BasicDBObject dbo = new BasicDBObject("_id", jsName).append("value", new Code(jsCode));
    coll.save(dbo, WriteConcern.JOURNAL_SAFE);

    setMessage("System JavaScript Written=" + jsName);
}