Example usage for com.mongodb WriteConcern UNACKNOWLEDGED

List of usage examples for com.mongodb WriteConcern UNACKNOWLEDGED

Introduction

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

Prototype

WriteConcern UNACKNOWLEDGED

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

Click Source Link

Document

Write operations that use this write concern will return as soon as the message is written to the socket.

Usage

From source file:CollectionOfMentions.ControllerCollectionOfMentions.java

License:Open Source License

public void run() {

    startDateTime = new DateTime();

    //checks on dates to make sure it's not abobe 7 days
    if (numberOfMinutes < 0) {
        numberOfMinutes = 0;/*  w w w  . j av a 2 s  . co  m*/
    }
    if (numberOfMinutes > 59) {
        numberOfMinutes = 59;
    }
    if (numberOfHours > 24) {
        numberOfHours = 24;
    }
    if (numberOfHours < 0) {
        numberOfHours = 0;
    }
    if (numberOfDays > 7) {
        numberOfDays = 7;
    }
    if (numberOfDays < 0) {
        numberOfDays = 0;
    }

    stopTime = startDateTime.getMillis() + numberOfMinutes * 60000 + numberOfHours * 3600000
            + numberOfDays * 3600000 * 24;
    if (stopTime - startDateTime.getMillis() > 3600000 * 24 * 7) {
        stopTime = startDateTime.getMillis() + 3600000 * 24 * 7;
    }

    //registers actual start time in the status field
    opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("status",
            String.valueOf(startDateTime.getMillis()));
    dsJobsInfo.update(updateQueryJobInfo, opsJobInfo, false, WriteConcern.UNACKNOWLEDGED);

    //registers actual end time in the end field
    opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("end", String.valueOf(stopTime));
    dsJobsInfo.update(updateQueryJobInfo, opsJobInfo, false, WriteConcern.UNACKNOWLEDGED);

    final Object lock = new Object();

    StatusListener listener;
    listener = new StatusListener() {
        @Override
        public void onStatus(Status status) {
            nbTweets++;

            if (System.currentTimeMillis() > stopTime) {

                //updating the job a last time;
                //**************************************
                //saving statuses to the db.
                //                    if (!twitterStatuses.isEmpty()) {
                //                        opsJob = dsJobs.createUpdateOperations(Job.class).addAll("statuses", statusesIds, true);
                //                        dsJobs.update(updateQueryJob, opsJob);
                //
                //                        dsTweets.save(twitterStatuses);
                //                    }
                // 91 is the code for twitter stream has stopped collecting.
                progress = 91;

                //recording the progress, nbTweets and end time of the job
                opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("progress", progress)
                        .set("nbTweets", nbTweets).set("end", System.currentTimeMillis());
                dsJobsInfo.update(updateQueryJobInfo, opsJobInfo);

                synchronized (lock) {
                    lock.notify();
                }

            } else {

                tweet = new Tweet();
                tweet.setStatus(TwitterObjectFactory.getRawJSON(status));
                tweet.setIdTweet(nbTweets);
                tweet.setJobId(jobUUID);
                ////                    System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
                //                    twitterStatus = convertStatus.convertOneToTwitterStatus(status);
                //                    twitterStatus.setJobId(jobUUID);
                //                    twitterStatuses.add(twitterStatus);
                //                    
                //                    
                //
                //                    statusesIds.add(status.getId());
                //                    timeSinceLastStatus = System.currentTimeMillis() - timeLastStatus;
                //
                //                    //**************************************
                //                    //adjusting the frequency of saves to DB, function of number of statuses received per second
                //                    if (timeSinceLastStatus < 200) {
                //                        sizeBatch = 100;
                //                    } else {
                //                        sizeBatch = 25;
                //                    }
                //                    timeLastStatus = System.currentTimeMillis();
                //                    progressLong = (Long) ((System.currentTimeMillis() - startDateTime.getMillis()) * 98 / (stopTime - startDateTime.getMillis()));

                //                    if (statusesIds.size() > sizeBatch || progressLong.intValue() > progress) {
                //**************************************
                //saving statuses to the db.
                try {
                    dsTweets.save(tweet, WriteConcern.UNACKNOWLEDGED);
                    opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("nbTweets", nbTweets);
                    dsJobsInfo.update(updateQueryJobInfo, opsJobInfo, false, WriteConcern.UNACKNOWLEDGED);
                } catch (MongoException m) {
                    System.out.println("saving of statuses to the db failed");
                }
                //                        twitterStatuses = new ArrayList();
                //
                //                        //**************************************
                //                        //updating list of status ids of the job.
                //                        opsJob = dsJobs.createUpdateOperations(Job.class).addAll("statuses", statusesIds, true);
                //                        dsJobs.update(updateQueryJob, opsJob);
                //                        statusesIds = new ArrayList();
                //
                //                        //updating progress.
                //                        System.out.println("progress: " + progressLong);
                //                        progress = progressLong.intValue();
                //                        opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("progress", progress).set("nbTweets", nbTweets);
                //                        dsJobsInfo.update(updateQueryJobInfo, opsJobInfo);

                //**************************************
                //                    }
            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        @Override
        public void onException(Exception ex) {
            System.out.println("Exception: " + ex);
        }

        @Override
        public void onStallWarning(StallWarning sw) {
            System.out.println("Got stall warning:" + sw.getMessage());

        }
    };
    twitterStream.addListener(listener);

    FilterQuery fq = new FilterQuery();
    String[] mentions = mention.split(",");
    fq.track(mentions);

    //        twitterStream.filter(new FilterQuery(0, users, keywords));
    twitterStream.filter(fq);

    try {
        synchronized (lock) {
            lock.wait();
        }
    } catch (InterruptedException e) {
    }
    try {
        twitterStream.shutdown();
    } catch (Exception e) {
        System.out.println("exception when shutdown of twitter stream");
        System.out.println("error: " + e.getMessage());
    }
    System.out.println("shutdown of twitter stream was successful");

}

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 ww  w  .  j a  va 2 s  .com
        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.ikanow.infinit.e.data_model.store.MongoDbManager.java

License:Apache License

private static DB initializeFastWriteDB(String dbName, Mongo savedMongo) {
    DB db = savedMongo.getDB(dbName);//from  ww w  . j av  a  2s .co  m
    //(keep performance in line with 2.4)
    db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
    return db;
}

From source file:com.ikanow.infinit.e.data_model.store.MongoDbManager.java

License:Apache License

@SuppressWarnings("deprecation")
public static void main(String[] args) throws UnknownHostException {
    MongoClient mc = new MongoClient(args[0]);
    long tnow = 0;
    DB db = mc.getDB("test");
    DBCollection test = db.getCollection("test123");
    BasicDBObject outObj = new BasicDBObject();
    int ITS = 1000;
    test.drop();//w  w w .j a  v  a 2  s. c o  m

    boolean checkPerformance = false;
    boolean checkFunctionality = false;
    boolean checkErrors = false;

    // 1] Performance

    if (checkPerformance) {

        // ack'd
        db.setWriteConcern(WriteConcern.ACKNOWLEDGED);
        test.drop();
        tnow = new Date().getTime();
        for (int i = 0; i < ITS; ++i) {
            outObj.remove("_id");
            outObj.put("val", i);
            test.save(outObj);
        }
        tnow = new Date().getTime() - tnow;
        System.out.println("1: Ack'd: " + tnow);

        // un ack'd
        db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
        test.drop();
        tnow = new Date().getTime();
        outObj = new BasicDBObject();
        for (int i = 0; i < ITS; ++i) {
            outObj.remove("_id");
            outObj.put("val", i);
            test.save(outObj);
        }
        tnow = new Date().getTime() - tnow;
        System.out.println("2: unAck'd: " + tnow);

        // un ack'd but call getLastError
        db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
        test.drop();
        tnow = new Date().getTime();
        outObj = new BasicDBObject();
        for (int i = 0; i < ITS; ++i) {
            outObj.remove("_id");
            outObj.put("val", i);
            test.save(outObj);
            db.getLastError();
        }
        tnow = new Date().getTime() - tnow;
        test.drop();
        System.out.println("3: unAck'd but GLEd: " + tnow);

        // ack'd override
        db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
        test.drop();
        tnow = new Date().getTime();
        outObj = new BasicDBObject();
        for (int i = 0; i < ITS; ++i) {
            outObj.remove("_id");
            outObj.put("val", i);
            test.save(outObj, WriteConcern.ACKNOWLEDGED);
            db.getLastError();
        }
        tnow = new Date().getTime() - tnow;
        System.out.println("4: unAck'd but ACKd: " + tnow);

        // Performance Results:
        // 2.6) (unack'd 100ms ... ack'd 27000)
        // 2.4) (same)
    }

    // 2] Functionality

    if (checkFunctionality) {

        // Unack:
        db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
        WriteResult wr = test.update(new BasicDBObject(),
                new BasicDBObject(DbManager.set_, new BasicDBObject("val2", "x")), false, true);
        CommandResult cr = db.getLastError();
        System.out.println("UNACK: wr: " + wr);
        System.out.println("UNACK: cr: " + cr);

        // bonus, check that we get N==0 when insert dup object
        WriteResult wr2 = test.insert(outObj);
        System.out.println("ACK wr2 = " + wr2.getN() + " all = " + wr2);
        CommandResult cr2 = db.getLastError();
        System.out.println("ACK cr2 = " + cr2);

        // Ack1:
        db.setWriteConcern(WriteConcern.ACKNOWLEDGED);
        wr = test.update(new BasicDBObject(), new BasicDBObject(DbManager.set_, new BasicDBObject("val3", "x")),
                false, true);
        cr = db.getLastError();
        System.out.println("ACK1: wr: " + wr);
        System.out.println("ACK1: cr: " + cr);

        // Ack2:
        db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
        wr = test.update(new BasicDBObject(), new BasicDBObject(DbManager.set_, new BasicDBObject("val4", "x")),
                false, true, WriteConcern.ACKNOWLEDGED);
        cr = db.getLastError();
        System.out.println("ACK2: wr: " + wr);
        System.out.println("ACK2: cr: " + cr);

        // bonus, check that we get N==0 when insert dup object
        wr2 = test.insert(outObj);
        System.out.println("ACK wr2 = " + wr2.getN() + " all = " + wr2);

        // Functionality results:
        // 2.6: unack wr == N/A, otherwise both have "n", "ok"
        // 2.4: unack wr == N/A all other wrs + crs identical 
    }

    if (checkErrors) {

        //set up sharding
        DbManager.getDB("admin").command(new BasicDBObject("enablesharding", "test"));
        // Ack:
        try {
            test.drop();
            test.createIndex(new BasicDBObject("key", 1));
            BasicDBObject command1 = new BasicDBObject("shardcollection", "test.test123");
            command1.append("key", new BasicDBObject("key", 1));
            DbManager.getDB("admin").command(command1);

            db.setWriteConcern(WriteConcern.ACKNOWLEDGED);
            outObj = new BasicDBObject("key", "test");
            test.save(outObj);
            WriteResult wr = test.update(new BasicDBObject(),
                    new BasicDBObject(DbManager.set_, new BasicDBObject("key", "test2")));
            System.out.println("ACK wr = " + wr);
        } catch (Exception e) {
            System.out.println("ACK err = " + e.toString());
        }

        // UnAck:
        try {
            test.drop();
            test.createIndex(new BasicDBObject("key", 1));
            BasicDBObject command1 = new BasicDBObject("shardcollection", "test.test123");
            command1.append("key", new BasicDBObject("key", 1));
            DbManager.getDB("admin").command(command1);

            db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
            outObj = new BasicDBObject("key", "test");
            test.save(outObj);
            WriteResult wr = test.update(new BasicDBObject(),
                    new BasicDBObject(DbManager.set_, new BasicDBObject("key", "test2")), false, false,
                    WriteConcern.ACKNOWLEDGED);
            System.out.println("ACK override wr = " + wr);
        } catch (Exception e) {
            System.out.println("ACK override  err = " + e.toString());
        }

        // UnAck:
        try {
            test.drop();
            test.createIndex(new BasicDBObject("key", 1));
            BasicDBObject command1 = new BasicDBObject("shardcollection", "test.test123");
            command1.append("key", new BasicDBObject("key", 1));
            DbManager.getDB("admin").command(command1);

            db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
            outObj = new BasicDBObject("key", "test");
            test.save(outObj);
            WriteResult wr = test.update(new BasicDBObject(),
                    new BasicDBObject(DbManager.set_, new BasicDBObject("key", "test2")));
            System.out.println("UNACK wr = " + wr);
        } catch (Exception e) {
            System.out.println("UNACK err = " + e.toString());
        }

        // UnAck + GLE:
        try {
            test.drop();
            test.createIndex(new BasicDBObject("key", 1));
            BasicDBObject command1 = new BasicDBObject("shardcollection", "test.test123");
            command1.append("key", new BasicDBObject("key", 1));
            DbManager.getDB("admin").command(command1);

            db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
            outObj = new BasicDBObject("key", "test");
            test.save(outObj);
            WriteResult wr = test.update(new BasicDBObject(),
                    new BasicDBObject(DbManager.set_, new BasicDBObject("key", "test2")));
            CommandResult cr = db.getLastError();
            System.out.println("UNACK GLE wr = " + wr);
            System.out.println("UNACK GLE cr = " + cr);
        } catch (Exception e) {
            System.out.println("UNACK GLE err = " + e.toString());
        }

        // Error handling:

        // 2.6:
        // Ack - exception
        // Ack override - exception
        // UnAck - no error given
        // UnAck + GLE  - gle error

        // 2.4:
        // Ack - exception
        // Ack override - exception
        // UnAck - no error given
        // UnAck + GLE  - gle error

    }
}

From source file:com.ricardolorenzo.identity.user.impl.UserIdentityManagerMongoDB.java

License:Open Source License

public UserIdentityManagerMongoDB(final Properties conf) throws UnknownHostException {
    this.properties = conf;

    this.databaseUsers = new Boolean(this.properties.getProperty("mongodb.databaseUsers", "true"));

    WriteConcern writeConcern = WriteConcern.MAJORITY;
    String writeConcernType = conf.getProperty("mongodb.writeConcern", "majority").toLowerCase();
    if ("majority".equals(writeConcernType)) {
        writeConcern = WriteConcern.UNACKNOWLEDGED;
    } else if ("unacknowledged".equals(writeConcernType)) {
        writeConcern = WriteConcern.UNACKNOWLEDGED;
    } else if ("acknowledged".equals(writeConcernType)) {
        writeConcern = WriteConcern.ACKNOWLEDGED;
    } else if ("journaled".equals(writeConcernType)) {
        writeConcern = WriteConcern.JOURNALED;
    } else if ("replica_acknowledged".equals(writeConcernType)) {
        writeConcern = WriteConcern.REPLICA_ACKNOWLEDGED;
    }/* ww  w . jav  a2s .  c om*/

    ReadPreference readPreference = null;
    String readPreferenceType = conf.getProperty("mongodb.readPreference", "primary").toLowerCase();
    if ("primary".equals(readPreferenceType)) {
        readPreference = ReadPreference.primary();
    } else if ("primary_preferred".equals(readPreferenceType)) {
        readPreference = ReadPreference.primaryPreferred();
    } else if ("secondary".equals(readPreferenceType)) {
        readPreference = ReadPreference.secondary();
    } else if ("secondary_preferred".equals(readPreferenceType)) {
        readPreference = ReadPreference.secondaryPreferred();
    } else if ("nearest".equals(readPreferenceType)) {
        readPreference = ReadPreference.nearest();
    }

    MongoClientOptions.Builder options = MongoClientOptions.builder();
    options.writeConcern(writeConcern);
    options.readPreference(readPreference);
    try {
        options.connectionsPerHost(Integer.parseInt(conf.getProperty("mongodb.threads", "100")));
    } catch (NumberFormatException e) {
        options.connectionsPerHost(100);
    }

    MongoClientURI mongoClientURI = new MongoClientURI(
            conf.getProperty("mongodb.url", "mongodb://localhost:27017"), options);
    if (!this.properties.containsKey("mongodb.database")) {
        if (mongoClientURI.getDatabase() != null && !mongoClientURI.getDatabase().isEmpty()) {
            this.properties.setProperty("mongodb.database", mongoClientURI.getDatabase());
        } else {
            this.properties.setProperty("mongodb.database", DEFAULT_DATABASE);
        }
    }
    mongoClient = new MongoClient(mongoClientURI);
}

From source file:com.softwear.plugins.mongodb.MongoDBScriptObject.java

License:BSD License

public MongoClient js_getMongo(String _host, int _port) {
    try {//from   w ww  .  j a v  a 2  s.  co  m
        if (mongoClient == null) {
            mongoClient = new MongoClient(_host, _port); //localhost 27017
            mongoClient.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
        } else {
            return mongoClient;
        }
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MongoException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return mongoClient;
}

From source file:com.softwear.plugins.mongodb.MongoDBScriptObject.java

License:BSD License

public MongoClient js_getMongo(String _host, int _port, String userName, String database, String password) {
    try {//from  w ww .ja v a  2 s .  c  om
        if (mongoClient == null) {
            char[] charPassword = password.toCharArray();
            MongoCredential credential = MongoCredential.createMongoCRCredential(userName, database,
                    charPassword);
            mongoClient = new MongoClient(new ServerAddress(_host, _port), Arrays.asList(credential)); //localhost 27017
            mongoClient.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
        } else {
            return mongoClient;
        }
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MongoException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return mongoClient;
}

From source file:com.softwear.plugins.mongodb.MongoDBScriptObject.java

License:BSD License

public WriteConcern js_getMongoWriteConcern(String _type) {
    WriteConcern wc = WriteConcern.UNACKNOWLEDGED;
    if (_type.equals("none") || _type.equals("unacknowledge")) {
        wc = WriteConcern.UNACKNOWLEDGED;
    } else if (_type.equals("normal")) {
        wc = WriteConcern.NORMAL;// www. j  a va 2 s  .  c om
    } else if (_type.equals("safe")) {
        wc = WriteConcern.SAFE;
    } else if (_type.equals("replicas_safe")) {
        wc = WriteConcern.REPLICAS_SAFE;
    } else if (_type.equals("fsync_safe")) {
        wc = WriteConcern.FSYNC_SAFE;
    }
    return wc;
}

From source file:com.stratio.connector.mongodb.core.configuration.MongoClientConfiguration.java

License:Apache License

/**
 * Convert the mongo connector string option to the appropriate write concern.
 *
 * @param writeSetting/*from  w ww  .  ja  va2  s .c  o  m*/
 *            the write concern string setting
 * @return the write concern
 * @throws MongoValidationException
 *             if the value cannot be parsed to a WriteConcern
 */
private WriteConcern settingToWritePreference(String writeSetting) throws MongoValidationException {

    WriteConcern writeConcern = null;
    switch (writeSetting.trim().toLowerCase()) {
    case "acknowledged":
        writeConcern = WriteConcern.ACKNOWLEDGED;
        break;
    case "unacknowledged":
        writeConcern = WriteConcern.UNACKNOWLEDGED;
        break;
    case "replica_acknowledged":
        writeConcern = WriteConcern.REPLICA_ACKNOWLEDGED;
        break;
    case "journaled":
        writeConcern = WriteConcern.JOURNALED;
        break;
    default:
        throw new MongoValidationException("Write preference " + writeSetting + " is not a legal value");
    }
    return writeConcern;

}

From source file:com.tomtom.speedtools.tracer.mongo.MongoDBTraceHandler.java

License:Apache License

@Nonnull
static DBCollection getDBCollection(@Nonnull final String servers, @Nonnull final String database,
        @Nonnull final String userName, @Nonnull final String password, final int sizeMB,
        final int connectTimeoutMsecs) throws UnknownHostException {
    assert servers != null;
    assert database != null;
    assert userName != null;
    assert password != null;
    assert connectTimeoutMsecs >= 0;

    LOG.info("getDBCollection: Creating MongoDB connection for traces: {}", servers);
    final Mongo mongo = MongoConnectionCache.getMongoDB(servers, connectTimeoutMsecs, userName, database,
            password);/*from  w  w w  . j a v a2s. c om*/

    // If this is a replica set, set read preference to secondary for traces.
    final List<ServerAddress> serverAddressList = mongo.getServerAddressList();
    if (serverAddressList.size() > 1) {
        mongo.setReadPreference(ReadPreference.secondary());
    }

    // Should writes fail, then don't throw exceptions, just ignore.
    // We care more about not disturbing the primary system then our own (traces) integrity.
    mongo.setWriteConcern(WriteConcern.UNACKNOWLEDGED);

    // The connection point may actually be null... Not an error.
    final String connectPoint = mongo.getConnectPoint();
    LOG.info("getDBCollection: MongoDB connection for traces established: '{}' at {}", database, connectPoint);

    final DB db = mongo.getDB(database);
    final DBObject options = new BasicDBObject();
    options.put("capped", true);
    options.put("size", sizeMB * MEGABYTE);

    final DBCollection collection;
    if (!db.getCollectionNames().contains(Constants.COLLECTION_NAME)) {
        collection = db.createCollection(Constants.COLLECTION_NAME, options);
    } else {
        collection = db.getCollection(Constants.COLLECTION_NAME);
    }

    return collection;
}