Example usage for com.mongodb WriteConcern ACKNOWLEDGED

List of usage examples for com.mongodb WriteConcern ACKNOWLEDGED

Introduction

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

Prototype

WriteConcern ACKNOWLEDGED

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

Click Source Link

Document

Write operations that use this write concern will wait for acknowledgement, using the default write concern configured on the server.

Usage

From source file:rapture.lock.mongodb.MongoLockHandler2.java

License:Open Source License

@Override
public LockHandle acquireLock(String lockHolder, String lockName, long secondsToWait, long secondsToHold) {
    log.debug("Mongo acquire lock2  " + lockName + ":" + secondsToHold + ":" + secondsToWait);
    long start = System.currentTimeMillis();

    MongoCollection<Document> coll = getLockCollection(lockName);
    log.debug("lock COLLECTION for " + lockName + "IS " + coll.getNamespace().getFullName());

    long bailTime = System.currentTimeMillis() + secondsToWait * 1000;
    long leaseTime = System.currentTimeMillis() + secondsToHold * 1000;

    Document lockFile = new Document();
    lockFile.put("lockName", lockName);
    lockFile.put("lockHolder", lockHolder);
    lockFile.put("lease", leaseTime);

    long myLockID = System.currentTimeMillis();
    lockFile.put("_id", "" + myLockID);
    log.debug("id is " + myLockID);
    log.debug("bailtime " + bailTime);

    while (bailTime > System.currentTimeMillis()) {
        try {/*w  w  w  .  j  a  v a  2  s  . co  m*/
            myLockID = System.currentTimeMillis();
            lockFile.put("_id", "" + myLockID);
            lockFile.put("lease", myLockID + secondsToHold * 1000);

            coll.withWriteConcern(WriteConcern.ACKNOWLEDGED).insertOne(lockFile);
            log.debug("inserted file" + lockFile);

            break;
        } catch (MongoServerException e) {
            log.error(ExceptionToString.format(e));
            try {
                Thread.sleep(50);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }
    }

    // loop until we acquire lock or timeout
    while (bailTime > System.currentTimeMillis()) {
        // we have the lock if no lock file exists with a smaller number
        FindIterable<Document> results = coll
                .find(new Document("lease", new Document("$gt", System.currentTimeMillis())))
                .sort(new Document("_id", 1)).limit(1);

        Document lock = results.first();
        if (lock != null && ((String) lock.get("_id")).equals("" + myLockID)) {
            log.debug("* i have the lock" + lock.get("_id") + ":" + myLockID);
            LockHandle lockHandle = new LockHandle();
            lockHandle.setLockName(lockName);
            lockHandle.setHandle("" + myLockID);
            lockHandle.setLockHolder(lockHolder);
            long end = System.currentTimeMillis();
            log.debug("* NG acquired lock in " + (end - start));
            return lockHandle;
        } else {
            // log.info("* waiting for lock held by "+ lock.get("_id"));
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    // ran out of time trying to get lock.
    log.debug("giving up " + myLockID);
    long end2 = System.currentTimeMillis();
    log.debug("denied lock in " + (end2 - start));
    return null;
}

From source file:rtpmt.models.Humidity.java

public void save() {
    DBCollection packageColl = db.getCollection(DBConstants.HUMIDITY_COLLECTION);
    /*/*  w ww .  j  ava2 s  .  c o  m*/
    BasicDBObject query = new BasicDBObject();
    query.put(DBConstants.SENSOR_ID, this.get(DBConstants.SENSOR_ID));
    query.put(DBConstants.TRUCK_ID, this.get(DBConstants.TRUCK_ID));
    query.put(DBConstants.PACKAGE_ID,this.get(DBConstants.PACKAGE_ID));
    query.put(DBConstants.TIMESTAMP, this.get(DBConstants.TIMESTAMP));
    BasicDBObject set = new BasicDBObject();
    set.put("$set", this);
            
    packageColl.update(query, set, true, false);*/
    packageColl.insert(this, WriteConcern.ACKNOWLEDGED);
}

From source file:rtpmt.models.Temperature.java

public void save() {
    DBCollection packageColl = db.getCollection(DBConstants.TEMPERATURE_COLLECTION);
    /*/* ww  w  .ja  v a  2  s  .co  m*/
    BasicDBObject query = new BasicDBObject();
    query.put(DBConstants.SENSOR_ID, this.get(DBConstants.SENSOR_ID));
    query.put(DBConstants.TRUCK_ID, this.get(DBConstants.TRUCK_ID));
    query.put(DBConstants.PACKAGE_ID,this.get(DBConstants.PACKAGE_ID));
    query.put(DBConstants.TIMESTAMP, this.get(DBConstants.TIMESTAMP));
    BasicDBObject set = new BasicDBObject();
    set.put("$set", this);
            
    packageColl.update(query, set, true, false);
        */
    packageColl.insert(this, WriteConcern.ACKNOWLEDGED);
}

From source file:testt.mongoConnector.java

public mongoConnector() throws UnknownHostException {
    /*/*from ww w.j  a  v a  2 s.com*/
    *  this section won't cause any exception yet, exception will be thrown
    *  when committing update to the database        
    */
    Scanner sc = new Scanner(System.in);
    String pwd;
    System.out.print("password:");
    pwd = sc.next();

    /*char [] pwd;
    pwd = System.console().readPassword();*/
    MongoCredential mc = MongoCredential.createCredential("rwmy", "myDB", pwd.toCharArray());
    ServerAddress sa = new ServerAddress("localhost");
    client = new MongoClient(sa, Arrays.asList(mc));
    db = client.getDB("myDB");
    coll = db.getCollection("websites");
    client.setWriteConcern(WriteConcern.ACKNOWLEDGED);
}