static {
   updaterIntervalMS =
       Integer.parseInt(System.getProperty("com.mongodb.updaterIntervalMS", "5000"));
   slaveAcceptableLatencyMS =
       Integer.parseInt(System.getProperty("com.mongodb.slaveAcceptableLatencyMS", "15"));
   inetAddrCacheMS = Integer.parseInt(System.getProperty("com.mongodb.inetAddrCacheMS", "300000"));
   _mongoOptions.connectTimeout =
       Integer.parseInt(System.getProperty("com.mongodb.updaterConnectTimeoutMS", "20000"));
   _mongoOptions.socketTimeout =
       Integer.parseInt(System.getProperty("com.mongodb.updaterSocketTimeoutMS", "20000"));
 }
 static {
   updaterIntervalMS =
       Integer.parseInt(System.getProperty("com.mongodb.updaterIntervalMS", "5000"));
   updaterIntervalNoMasterMS =
       Integer.parseInt(System.getProperty("com.mongodb.updaterIntervalNoMasterMS", "10"));
   slaveAcceptableLatencyMS =
       Integer.parseInt(System.getProperty("com.mongodb.slaveAcceptableLatencyMS", "15"));
   inetAddrCacheMS = Integer.parseInt(System.getProperty("com.mongodb.inetAddrCacheMS", "300000"));
   latencySmoothFactor =
       Float.parseFloat(System.getProperty("com.mongodb.latencySmoothFactor", "4"));
   _mongoOptionsDefaults.connectTimeout =
       Integer.parseInt(System.getProperty("com.mongodb.updaterConnectTimeoutMS", "20000"));
   _mongoOptionsDefaults.socketTimeout =
       Integer.parseInt(System.getProperty("com.mongodb.updaterSocketTimeoutMS", "20000"));
 }
 /** 数据库初始化 * */
 static {
   try {
     mg =
         new Mongo(
             PropertiesUtil.getValue("MongodbIP"),
             Integer.parseInt(PropertiesUtil.getValue("MongodbPort")));
     MongoOptions opt = mg.getMongoOptions();
     opt.connectionsPerHost = CONNECTIONSPERHOST;
     opt.threadsAllowedToBlockForConnectionMultiplier =
         THREADSALLOWEDTOBLOCKFORCONNECTIONMULTIPLIER;
     db = mg.getDB(PropertiesUtil.getValue("MongodbDataBase"));
     collection = db.getCollection(PropertiesUtil.getValue("MongodbDataBaseCollection"));
   } catch (Exception e) {
     logger.error(e.getMessage());
   }
 }
  @Test
  public void testHasFinalizer() throws UnknownHostException {
    DBCollection c = _db.getCollection("HasFinalizerTest");
    c.drop();

    for (int i = 0; i < 1000; i++) c.save(new BasicDBObject("_id", i), WriteConcern.SAFE);

    // finalizer is on by default so after calling hasNext should report that it has one
    DBCursor cursor = c.find();
    assertFalse(cursor.hasFinalizer());
    cursor.hasNext();
    assertTrue(cursor.hasFinalizer());
    cursor.close();

    // no finalizer if there is no cursor, as there should not be for a query with only one result
    cursor = c.find(new BasicDBObject("_id", 1));
    cursor.hasNext();
    assertFalse(cursor.hasFinalizer());
    cursor.close();

    // no finalizer if there is no cursor, as there should not be for a query with negative batch
    // size
    cursor = c.find();
    cursor.batchSize(-1);
    cursor.hasNext();
    assertFalse(cursor.hasFinalizer());
    cursor.close();

    // finally, no finalizer if disabled in mongo options
    MongoOptions mongoOptions = new MongoOptions();
    mongoOptions.cursorFinalizerEnabled = false;
    Mongo m = new Mongo("127.0.0.1", mongoOptions);
    try {
      c = m.getDB(cleanupDB).getCollection("HasFinalizerTest");
      cursor = c.find();
      cursor.hasNext();
      assertFalse(cursor.hasFinalizer());
      cursor.close();
    } finally {
      m.close();
    }
  }
Ejemplo n.º 5
0
  @SuppressWarnings("deprecation")
  @Test
  public void testApplyOptions() throws UnknownHostException {
    MongoOptions options = new MongoOptions();

    // test defaults
    Mongo m = new Mongo("localhost", options);
    assertEquals(ReadPreference.primary(), m.getReadPreference());
    assertEquals(WriteConcern.NORMAL, m.getWriteConcern());
    assertEquals(0, m.getOptions() & Bytes.QUERYOPTION_SLAVEOK);
    m.close();

    // test setting options
    options.setReadPreference(ReadPreference.nearest());
    options.slaveOk = true;
    options.safe = true;

    m = new Mongo("localhost", options);
    assertEquals(ReadPreference.nearest(), m.getReadPreference());
    assertEquals(WriteConcern.SAFE, m.getWriteConcern());
    assertEquals(Bytes.QUERYOPTION_SLAVEOK, m.getOptions() & Bytes.QUERYOPTION_SLAVEOK);
    m.close();
  }
 @Test
 public void testMongoSingletonWithPropertyPlaceHolders() throws Exception {
   assertTrue(ctx.containsBean("mongo"));
   MongoFactoryBean mfb = (MongoFactoryBean) ctx.getBean("&mongo");
   String host = (String) getField(mfb, "host");
   Integer port = (Integer) getField(mfb, "port");
   assertEquals("127.0.0.1", host);
   assertEquals(new Integer(27017), port);
   Mongo mongo = mfb.getObject();
   MongoOptions mongoOpts = mongo.getMongoOptions();
   assertEquals(8, mongoOpts.connectionsPerHost);
   assertEquals(1000, mongoOpts.connectTimeout);
   assertEquals(1500, mongoOpts.maxWaitTime);
   assertEquals(true, mongoOpts.autoConnectRetry);
   assertEquals(1500, mongoOpts.socketTimeout);
   assertEquals(4, mongoOpts.threadsAllowedToBlockForConnectionMultiplier);
   assertEquals(true, mongoOpts.socketKeepAlive);
   assertEquals(true, mongoOpts.fsync);
   assertEquals(true, mongoOpts.slaveOk);
   assertEquals(1, mongoOpts.getWriteConcern().getW());
   assertEquals(0, mongoOpts.getWriteConcern().getWtimeout());
   assertEquals(true, mongoOpts.getWriteConcern().fsync());
 }
Ejemplo n.º 7
0
  @Test
  @SuppressWarnings("deprecation")
  public void testTurnOffSlaveOk() throws MongoException, UnknownHostException {
    MongoOptions mongoOptions = new MongoOptions();

    mongoOptions.slaveOk = true;

    Mongo mongo = new Mongo("localhost", mongoOptions);
    try {
      mongo.addOption(Bytes.QUERYOPTION_PARTIAL);
      mongo.addOption(Bytes.QUERYOPTION_AWAITDATA);

      int isSlaveOk = mongo.getOptions() & Bytes.QUERYOPTION_SLAVEOK;

      assertEquals(Bytes.QUERYOPTION_SLAVEOK, isSlaveOk);

      mongo.setOptions(mongo.getOptions() & (~Bytes.QUERYOPTION_SLAVEOK));

      assertEquals(Bytes.QUERYOPTION_AWAITDATA | Bytes.QUERYOPTION_PARTIAL, mongo.getOptions());
    } finally {
      mongo.close();
    }
  }
  private void init(String dbPrefix) {
    try {
      mongoReplicaProps = new ArrayList<ServerAddress>();
      // mongoReplicaProps.add(new ServerAddress("mongodb1.qa.sg1.hike.in", 27017));
      mongoReplicaProps.add(new ServerAddress("10.0.1.141", 27017));
      mongoReplicaProps.add(new ServerAddress("10.0.1.141", 27017));
      MongoOptions options = new MongoOptions();
      options.autoConnectRetry = true;
      options.readPreference = ReadPreference.primaryPreferred();
      options.setThreadsAllowedToBlockForConnectionMultiplier(50);
      options.setConnectionsPerHost(100);
      options.slaveOk = true;
      mongo = new Mongo(mongoReplicaProps, options);
      userDB = mongo.getDB("userdb");

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * Method invoked when a {@link MongoSession} needs to be created.
   *
   * @param username the username to use for authentication. NOTE: Please use a dummy user if you
   *     have disabled Mongo authentication
   * @param password the password to use for authentication. NOTE: Please use a dummy password if
   *     you have disabled Mongo authentication
   * @param database Name of the database
   * @return the newly created {@link MongoSession}
   * @throws org.mule.api.ConnectionException
   */
  @Connect
  public void connect(
      @ConnectionKey String username,
      @Password String password,
      @Optional @Default("test") String database)
      throws ConnectionException {
    DB db = null;
    try {
      MongoOptions options = new MongoOptions();

      if (connectionsPerHost != null) {
        options.connectionsPerHost = connectionsPerHost;
      }
      if (threadsAllowedToBlockForConnectionMultiplier != null) {
        options.threadsAllowedToBlockForConnectionMultiplier =
            threadsAllowedToBlockForConnectionMultiplier;
      }
      if (maxWaitTime != null) {
        options.maxWaitTime = maxWaitTime;
      }
      if (connectTimeout != null) {
        options.connectTimeout = connectTimeout;
      }
      if (socketTimeout != null) {
        options.socketTimeout = socketTimeout;
      }
      if (autoConnectRetry != null) {
        options.autoConnectRetry = autoConnectRetry;
      }
      if (slaveOk != null) {
        options.slaveOk = slaveOk;
      }
      if (safe != null) {
        options.safe = safe;
      }
      if (w != null) {
        options.w = w;
      }
      if (wtimeout != null) {
        options.wtimeout = wtimeout;
      }
      if (fsync != null) {
        options.fsync = fsync;
      }
      if (database != null) {
        this.database = database;
      }

      mongo = getOrCreateMongoInstance(host, port, options);
      db = getDatabase(mongo, username, password, database);
    } catch (MongoException me) {
      throw new ConnectionException(ConnectionExceptionCode.UNKNOWN, null, me.getMessage());
    } catch (UnknownHostException e) {
      throw new ConnectionException(ConnectionExceptionCode.UNKNOWN_HOST, null, e.getMessage());
    }
    this.client = new MongoClientImpl(db);
  }
  @Test
  @SuppressWarnings("deprecation")
  public void testGetterSetters() throws Exception {

    MongoOptions options = new MongoOptions();

    options.setConnectionsPerHost(100);
    options.setThreadsAllowedToBlockForConnectionMultiplier(101);
    options.setMaxWaitTime(102);
    options.setConnectTimeout(103);
    options.setSocketTimeout(104);
    options.setSocketKeepAlive(true);
    options.setSafe(true);
    options.setW(106);
    options.setWtimeout(107);
    options.setFsync(true);
    options.setJ(false);
    options.setDbDecoderFactory(null);
    options.setDbEncoderFactory(null);
    options.setDescription("very cool");
    options.setReadPreference(ReadPreference.secondary());
    options.setSocketFactory(SSLSocketFactory.getDefault());
    options.setAlwaysUseMBeans(true);
    options.setCursorFinalizerEnabled(false);
    options.requiredReplicaSetName = "set1";

    assertEquals(options.getConnectionsPerHost(), 100);
    assertEquals(options.getThreadsAllowedToBlockForConnectionMultiplier(), 101);
    assertEquals(options.getMaxWaitTime(), 102);
    assertEquals(options.getConnectTimeout(), 103);
    assertEquals(options.getSocketTimeout(), 104);
    assertEquals(options.isSocketKeepAlive(), true);
    assertEquals(options.isSafe(), true);
    assertEquals(options.getW(), 106);
    assertEquals(options.getWtimeout(), 107);
    assertEquals(options.isFsync(), true);
    assertEquals(options.isJ(), false);
    assertEquals(options.getDbDecoderFactory(), null);
    assertEquals(options.getDbEncoderFactory(), null);
    assertEquals(options.getDescription(), "very cool");
    assertEquals(options.getReadPreference(), ReadPreference.secondary());
    assertEquals(options.isAlwaysUseMBeans(), true);
    assertEquals(options.getSocketFactory(), options.socketFactory);
    assertEquals(options.isCursorFinalizerEnabled(), false);
    assertEquals(options.getRequiredReplicaSetName(), "set1");
  }
  @Test
  @SuppressWarnings("deprecation")
  public void testCopy() throws Exception {

    MongoOptions options = new MongoOptions();

    options.connectionsPerHost = 100;
    options.threadsAllowedToBlockForConnectionMultiplier = 101;
    options.maxWaitTime = 102;
    options.connectTimeout = 103;
    options.socketTimeout = 104;
    options.socketKeepAlive = true;
    options.safe = true;
    options.w = 106;
    options.wtimeout = 107;
    options.fsync = true;
    options.j = false;
    options.dbDecoderFactory = null;
    options.dbEncoderFactory = null;
    options.description = "cool";
    options.readPreference = ReadPreference.secondary();
    options.cursorFinalizerEnabled = true;
    options.socketFactory = SSLSocketFactory.getDefault();
    options.alwaysUseMBeans = true;
    options.requiredReplicaSetName = "set1";

    MongoOptions copy = options.copy();
    assertEquals(options.connectionsPerHost, copy.connectionsPerHost);
    assertEquals(
        options.threadsAllowedToBlockForConnectionMultiplier,
        copy.threadsAllowedToBlockForConnectionMultiplier);
    assertEquals(options.maxWaitTime, copy.maxWaitTime);
    assertEquals(options.connectTimeout, copy.connectTimeout);
    assertEquals(options.socketTimeout, copy.socketTimeout);
    assertEquals(options.socketKeepAlive, copy.socketKeepAlive);
    assertEquals(options.safe, copy.safe);
    assertEquals(options.w, copy.w);
    assertEquals(options.wtimeout, copy.wtimeout);
    assertEquals(options.fsync, copy.fsync);
    assertEquals(options.j, copy.j);
    assertEquals(options.dbDecoderFactory, copy.dbDecoderFactory);
    assertEquals(options.dbEncoderFactory, copy.dbEncoderFactory);
    assertEquals(options.description, copy.description);
    assertEquals(options.readPreference, copy.readPreference);
    assertEquals(options.alwaysUseMBeans, copy.alwaysUseMBeans);
    assertEquals(options.socketFactory, copy.socketFactory);
    assertEquals(options.requiredReplicaSetName, copy.requiredReplicaSetName);
  }
  @Test
  @SuppressWarnings("deprecation")
  public void testToClientOptions() throws UnknownHostException {
    MongoOptions options = new MongoOptions();
    options.description = "my client";
    options.fsync = true;
    options.readPreference = ReadPreference.secondary();
    options.requiredReplicaSetName = "test";
    options.cursorFinalizerEnabled = false;
    options.alwaysUseMBeans = true;
    options.connectTimeout = 100;
    options.maxWaitTime = 500;
    options.socketKeepAlive = true;
    options.threadsAllowedToBlockForConnectionMultiplier = 10;

    MongoClientOptions clientOptions = options.toClientOptions();

    assertEquals(options.requiredReplicaSetName, clientOptions.getRequiredReplicaSetName());
    assertEquals(options.description, clientOptions.getDescription());
    assertEquals(WriteConcern.ACKNOWLEDGED.withFsync(true), clientOptions.getWriteConcern());
    assertEquals(0, clientOptions.getMinConnectionsPerHost());
    assertEquals(10, clientOptions.getConnectionsPerHost());
    assertEquals(100, clientOptions.getConnectTimeout());
    assertEquals(500, clientOptions.getMaxWaitTime());
    assertEquals(ReadPreference.secondary(), clientOptions.getReadPreference());
    assertEquals(10, clientOptions.getThreadsAllowedToBlockForConnectionMultiplier());
    assertTrue(clientOptions.isSocketKeepAlive());
    assertFalse(clientOptions.isSslEnabled());
    assertEquals(options.dbDecoderFactory, clientOptions.getDbDecoderFactory());
    assertEquals(options.dbEncoderFactory, clientOptions.getDbEncoderFactory());
    assertEquals(15, clientOptions.getLocalThreshold());
    assertTrue(clientOptions.isAlwaysUseMBeans());
    assertFalse(clientOptions.isCursorFinalizerEnabled());
  }
  @Test
  @SuppressWarnings("deprecation")
  public void testGetWriteConcern() {
    MongoOptions options = new MongoOptions();
    assertEquals(WriteConcern.NORMAL, options.getWriteConcern());

    options.reset();
    options.safe = true;
    assertEquals(WriteConcern.SAFE, options.getWriteConcern());

    options.reset();
    options.w = 3;
    assertEquals(new WriteConcern(3), options.getWriteConcern());

    options.reset();
    options.wtimeout = 3000;
    assertEquals(
        WriteConcern.ACKNOWLEDGED.withWTimeout(3000, TimeUnit.MILLISECONDS),
        options.getWriteConcern());

    options.reset();
    options.fsync = true;
    assertEquals(WriteConcern.ACKNOWLEDGED.withFsync(true), options.getWriteConcern());

    options.reset();
    options.j = true;
    assertEquals(WriteConcern.ACKNOWLEDGED.withJournal(true), options.getWriteConcern());
  }