Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-520

There is no way to chack master in java's client api when new Mongo(serverList)

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.6.5, 2.7.2
    • Component/s: API

      Issure url:https://jira.mongodb.org/browse/JAVA-519

      We just want to check master in starting our web application.

      A full client code example:

      public class MongoDataSource {

      private static Logger log = LoggerFactory.getLogger(MongoDataSource.class);
      private DB db;
      private String dbName;
      private Mongo mongo;
      private String writeConcernName;
      private WriteConcern writeConcern;
      private boolean slaveOk;
      private Set<String> hostNames = new HashSet<String>();
      private List<ServerAddress> hosts = new ArrayList<ServerAddress>();
      private boolean checkMaster;

      public MongoDataSource(String replicaHosts, String dbName, String writeConcernName, boolean slaveOk,boolean checkMaster){
      String names[] = replicaHosts.split(",");
      for (String name : names)

      { hostNames.add(name); }

      this.dbName = dbName;
      this.writeConcernName = writeConcernName;
      this.slaveOk = slaveOk;
      this.checkMaster=checkMaster;
      }

      public void init() throws UnknownHostException, MongoException {
      for (String hostName : hostNames)

      { String tmp[] = hostName.split(":"); ServerAddress addr = new ServerAddress(tmp[0], Integer.parseInt(tmp[1])); hosts.add(addr); }

      mongo = new Mongo(hosts);
      db = mongo.getDB(dbName);
      WriteConcern wc = WriteConcern.valueOf(writeConcernName);
      db.setWriteConcern(wc);
      if (this.slaveOk)

      { db.slaveOk(); }

      if (checkMaster)

      { db.getCollectionNames(); }

      }

      public void destroy()

      { mongo.close(); }

      }

      configuration in spring xml file:
      <bean id="mongods" class="com.alibaba.cep.ep.dao.impl.MongoDataSource" init-method="init" destroy-method="destroy">
      <constructor-arg value="10.20.30.56:11112"></constructor-arg>
      <constructor-arg value="db"></constructor-arg>
      <constructor-arg value="SAFE"></constructor-arg>
      <constructor-arg value="false"></constructor-arg>
      <constructor-arg value="true"></constructor-arg>
      </bean>

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            boringkiller xfc
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: