Example usage for com.mongodb CommandResult getException

List of usage examples for com.mongodb CommandResult getException

Introduction

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

Prototype

@Nullable
public MongoException getException() 

Source Link

Document

Utility method to create an exception from a failed command.

Usage

From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java

License:Open Source License

/**
 * profiling start/* ww  w. java  2s .co m*/
 * @param userDB
 * @param level 0 : stop, 1 or 2 : start
 * @return
 * @throws Exception
 */
public static boolean commandProfiling(UserDBDAO userDB, int level) throws Exception {
    DB mongoDB = findDB(userDB);

    DBObject queryObj = new BasicDBObject("profile", level);
    CommandResult cr = mongoDB.command(queryObj);

    if (cr.ok()) {
        return true;
    } else {
        throw cr.getException();
    }
}

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestDBStats.java

License:Open Source License

/**
 * @param args/*from   w  ww .j  a v  a  2  s .  c o m*/
 */
public static void main(String[] args) throws Exception {
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");

    CommandResult cr = db.getStats();

    System.out.println("[ok]" + cr.ok());
    if (!cr.ok())
        System.out.println("[Exception ]" + cr.getException().toString());
    System.out.println("[toString]" + JSONUtil.getPretty(cr.toString()));
    System.out.println("[size]" + cr.size());

    mongo.close();
}

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestListCommand.java

License:Open Source License

/**
 * @param args//from  w  ww  .j av  a 2 s  .c  o  m
 */
public static void main(String[] args) throws Exception {
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");

    DBObject queryObj = new BasicDBObject("listCommands", 1);
    CommandResult cr = db.command(queryObj);

    System.out.println("[ok]" + cr.ok());
    if (!cr.ok())
        System.out.println("[Exception ]" + cr.getException().toString());
    System.out.println("[toString]" + JSONUtil.getPretty(cr.toString()));
    System.out.println("[size]" + cr.size());

    mongo.close();
}

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestProfilling.java

License:Open Source License

/**
 * @param args/*from w  ww . ja v  a2 s . co  m*/
 */
public static void main(String[] args) throws Exception {
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");

    // ??       
    System.out.println("####[profilling  ]######################################################");
    CommandResult cr = db.command(new BasicDBObject("profile", 0));

    System.out.println("[ok]" + cr.ok());
    if (!cr.ok())
        System.out.println("[Exception ]" + cr.getException().toString());
    System.out.println("[toString]" + JSONUtil.getPretty(cr.toString()));
    System.out.println("[size]" + cr.size());
    System.out.println("####[profilling  ]######################################################");

    //  ?          
    System.out.println(
            "####[profilling collections  ]######################################################");
    if (db.collectionExists("system.profile")) {
        DBCollection profileColl = db.getCollection("system.profile");
        profileColl.drop();
    }
    System.out.println(
            "####[profilling collections  ]######################################################");
    //  ?     

    // system.profile collection ? 
    System.out.println(
            "####[profilling collections ? ]######################################################");
    DBObject dbObject = (DBObject) JSON.parse("{capped:true, size:8000000}");
    DBCollection dbColl = db.createCollection("system.profile", dbObject);
    BasicDBObject newProfileColl = (BasicDBObject) dbColl.getStats().copy();
    System.out.println(
            "####[profilling collections ? ]######################################################");
    // system.profile collection ? 

    System.out.println("####[profilling ]######################################################");
    cr = db.command(new BasicDBObject("profile", 2));

    System.out.println("[ok]" + cr.ok());
    if (!cr.ok())
        System.out.println("[Exception ]" + cr.getException().toString());
    System.out.println("[toString]" + JSONUtil.getPretty(cr.toString()));
    System.out.println("[size]" + cr.size());
    System.out.println("####[profilling ]######################################################");

    //      //#######################################################################################################
    //      //#######################################################################################################
    //      //#######################################################################################################      
    System.out.println("####[start profilling result]######################################################");
    DBCollection myColl = db.getCollection("system.profile");

    BasicDBObject query = new BasicDBObject();
    query.put("millis", new BasicDBObject("$gt", 4));

    DBCursor myCursor = myColl.find();
    while (myCursor.hasNext()) {
        DBObject dbObj = myCursor.next();
        System.out.println(dbObj.get("ts") + " - " + dbObj.get("ns") + " - " + dbObj.get("nscanned") + "/"
                + dbObj.get("nreturned") + " millis :" + dbObj.get("millis"));
    }
    System.out.println("####[end profilling result]######################################################");

    mongo.close();
}

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestReIndex.java

License:Open Source License

/**
 * @param args// w  w w.ja va  2s. c  o m
 */
public static void main(String[] args) throws Exception {
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");

    DBObject queryObj = new BasicDBObject("reIndex", "store");
    CommandResult cr = db.command(queryObj);

    System.out.println("[ok]" + cr.ok());
    if (!cr.ok())
        System.out.println("[Exception ]" + cr.getException().toString());
    System.out.println("[toString]" + cr.toString());
    System.out.println("[size]" + cr.size());

    mongo.close();
}

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestServerStatus.java

License:Open Source License

/**
 * @param args/*  ww w  .  j av a  2  s .  c  o m*/
 */
public static void main(String[] args) throws Exception {
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");

    DBObject queryObj = new BasicDBObject("serverStatus", 0);
    CommandResult cr = db.command(queryObj);

    String strHost = cr.getString("host");
    String version = cr.getString("version");
    String process = cr.getString("process");
    String pid = cr.getString("pid");
    String uptime = cr.getString("uptime");
    String uptimeMillis = cr.getString("uptimeMillis");
    String uptimeEstimate = cr.getString("uptimeEstimate");
    String localTime = cr.getString("localTime");

    System.out.println("[strHost]\t " + strHost);
    System.out.println("[version]\t " + version);

    System.out.println("[process]\t " + process);
    System.out.println("[pid]\t " + pid);
    System.out.println("[uptime]\t " + uptime);
    System.out.println("[uptimeMillis]\t " + uptimeMillis);
    System.out.println("[uptimeEstimate]\t " + uptimeEstimate);
    System.out.println("[localTime]\t " + localTime);

    System.out.println("[ok]" + cr.ok());
    if (!cr.ok())
        System.out.println("[Exception ]" + cr.getException().toString());
    System.out.println("[toString]" + cr.toString());
    System.out.println("[size]" + cr.size());

    mongo.close();
}

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestShardInformation.java

License:Open Source License

/**
 * @param args//from  w  w  w  . ja  v a2s  .c  om
 */
public static void main(String[] args) throws Exception {
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection("127.0.0.1", 27018);
    DB db = mongo.getDB("admin");

    DBObject queryObj = new BasicDBObject("listshards", 1);
    CommandResult cr = db.command(queryObj);
    if (cr.ok()) {
        System.out.println(cr.toString());
    } else {
        System.out.println(cr.getException());
    }

    // shard key ?? ? ?  .
    final BasicDBObject shardKey = new BasicDBObject("TrackId", 1);
    final BasicDBObject cmd = new BasicDBObject("shardcollection", "test.Track");
    cmd.put("key", shardKey);
    CommandResult result4 = mongo.getDB("admin").command(cmd);

    System.out.println("====>" + result4);
}

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestTop.java

License:Open Source License

/**
 * @param args//from   ww  w  .j  a  v a  2 s  . c  om
 */
public static void main(String[] args) throws Exception {
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("admin");

    DBObject queryObj = new BasicDBObject("top", 1);
    CommandResult cr = db.command(queryObj);
    if (cr.ok()) {
        System.out.println(cr.toString());
    } else {

        System.out.println(cr.getException());
    }
}

From source file:com.jhkt.playgroundArena.shared.tasks.MongoDBTaskRunner.java

License:Apache License

private void processWriteResult(WriteResult wr) {
    CommandResult result = wr.getLastError();
    if (!result.ok()) {
        throw new RuntimeException(result.getErrorMessage(), result.getException());
    }/* w  w w  . j  ava 2s .  com*/
}

From source file:com.syncleus.maven.plugins.mongodb.StartMongoMojo.java

License:Open Source Community License

private void processScriptFile(final DB db, final File scriptFile) throws MojoExecutionException {
    Scanner scanner = null;//from   w  ww . jav  a2  s.  c  o m
    final StringBuilder instructions = new StringBuilder();
    try {
        scanner = new Scanner(scriptFile);
        while (scanner.hasNextLine()) {
            instructions.append(scanner.nextLine()).append("\n");
        }
    } catch (final FileNotFoundException e) {
        throw new MojoExecutionException("Unable to find file with name '" + scriptFile.getName() + "'", e);
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
    final CommandResult result;
    try {
        final String evalString = "(function() {" + instructions.toString() + "})();";
        result = db.doEval(evalString, new Object[0]);
    } catch (final MongoException e) {
        throw new MojoExecutionException("Unable to execute file with name '" + scriptFile.getName() + "'", e);
    }
    if (!result.ok()) {
        getLog().error("- file " + scriptFile.getName() + " parsed with error: " + result.getErrorMessage());
        throw new MojoExecutionException("Error while executing instructions from file '" + scriptFile.getName()
                + "': " + result.getErrorMessage(), result.getException());
    }
    getLog().info("- file " + scriptFile.getName() + " parsed successfully");
}