コード例 #1
0
  @Override
  protected void onBeforeProcess(IRuntimeConfig runtimeConfig) throws IOException {
    super.onBeforeProcess(runtimeConfig);

    MongodConfig config = getConfig();

    File tmpDbDir;
    if (config.getDatabaseDir() != null) {
      tmpDbDir = Files.createOrCheckDir(config.getDatabaseDir());
    } else {
      tmpDbDir = Files.createTempDir("embedmongo-db");
      dbDirIsTemp = true;
    }
    this.dbDir = tmpDbDir;
  }
コード例 #2
0
  // ### ... automagic
  public void testFreeServerPortAuto() throws UnknownHostException, IOException {
    MongodConfig mongodConfig = new MongodConfig(Version.Main.V2_0);

    MongodStarter runtime = MongodStarter.getDefaultInstance();

    MongodExecutable mongodExecutable = null;
    try {
      mongodExecutable = runtime.prepare(mongodConfig);
      MongodProcess mongod = mongodExecutable.start();

      Mongo mongo =
          new Mongo(
              new ServerAddress(
                  mongodConfig.net().getServerAddress(), mongodConfig.net().getPort()));
      DB db = mongo.getDB("test");
      DBCollection col = db.createCollection("testCol", new BasicDBObject());
      col.save(new BasicDBObject("testDoc", new Date()));

    } finally {
      if (mongodExecutable != null) mongodExecutable.stop();
    }
  }
  private void initMongoInstances() throws Exception {
    logger.debug("*** initMongoInstances ***");
    CommandResult cr;

    // Create 3 mongod processes
    mongodConfig1 =
        new MongodConfig(
            new GenericVersion(mongoVersion),
            new Net(mongoPort1, Network.localhostIsIPv6()),
            new Storage(null, REPLICA_SET_NAME, 20),
            new Timeout());
    MongodStarter starter = MongodStarter.getDefaultInstance();
    mongodExe1 = starter.prepare(mongodConfig1);
    mongod1 = mongodExe1.start();
    mongodConfig2 =
        new MongodConfig(
            new GenericVersion(mongoVersion),
            new Net(mongoPort2, Network.localhostIsIPv6()),
            new Storage(null, REPLICA_SET_NAME, 20),
            new Timeout());
    mongodExe2 = starter.prepare(mongodConfig2);
    mongod2 = mongodExe2.start();
    mongodConfig3 =
        new MongodConfig(
            new GenericVersion(mongoVersion),
            new Net(mongoPort3, Network.localhostIsIPv6()),
            new Storage(null, REPLICA_SET_NAME, 20),
            new Timeout());
    mongodExe3 = starter.prepare(mongodConfig3);
    mongod3 = mongodExe3.start();
    String server1 = Network.getLocalHost().getHostName() + ":" + mongodConfig1.net().getPort();
    String server2 = Network.getLocalHost().getHostName() + ":" + mongodConfig2.net().getPort();
    String server3 = Network.getLocalHost().getHostName() + ":" + mongodConfig3.net().getPort();
    logger.debug("Server #1: {}", server1);
    logger.debug("Server #2: {}", server2);
    logger.debug("Server #3: {}", server3);
    Thread.sleep(2000);
    MongoClientOptions mco =
        MongoClientOptions.builder()
            .autoConnectRetry(true)
            .connectTimeout(15000)
            .socketTimeout(60000)
            .build();
    mongo =
        new MongoClient(
            new ServerAddress(Network.getLocalHost().getHostName(), mongodConfig1.net().getPort()),
            mco);
    mongoAdminDB = mongo.getDB(ADMIN_DATABASE_NAME);

    cr = mongoAdminDB.command(new BasicDBObject("isMaster", 1));
    logger.debug("isMaster: " + cr);

    // Initialize replica set
    cr =
        mongoAdminDB.command(
            new BasicDBObject(
                "replSetInitiate",
                (DBObject)
                    JSON.parse(
                        "{'_id': '"
                            + REPLICA_SET_NAME
                            + "', 'members': [{'_id': 0, 'host': '"
                            + server1
                            + "'}, {'_id': 1, 'host': '"
                            + server2
                            + "'}, {'_id': 2, 'host': '"
                            + server3
                            + "', 'arbiterOnly' : true}]} }")));
    logger.debug("replSetInitiate: " + cr);

    Thread.sleep(5000);
    cr = mongoAdminDB.command(new BasicDBObject("replSetGetStatus", 1));
    logger.info("replSetGetStatus: " + cr);

    // Check replica set status before to proceed
    while (!isReplicaSetStarted(cr)) {
      logger.debug("Waiting for 3 seconds...");
      Thread.sleep(3000);
      cr = mongoAdminDB.command(new BasicDBObject("replSetGetStatus", 1));
      logger.debug("replSetGetStatus: " + cr);
    }

    mongo.close();
    mongo = null;

    // Initialize a new client using all instances.
    List<ServerAddress> mongoServers = new ArrayList<ServerAddress>();
    mongoServers.add(
        new ServerAddress(Network.getLocalHost().getHostName(), mongodConfig1.net().getPort()));
    mongoServers.add(
        new ServerAddress(Network.getLocalHost().getHostName(), mongodConfig2.net().getPort()));
    mongoServers.add(
        new ServerAddress(Network.getLocalHost().getHostName(), mongodConfig3.net().getPort()));
    mongo = new MongoClient(mongoServers, mco);
    Assert.assertNotNull(mongo);
    mongo.setReadPreference(ReadPreference.secondaryPreferred());
    mongo.setWriteConcern(WriteConcern.REPLICAS_SAFE);
  }