关闭

mongoClient备忘

标签: mongoClient
68人阅读 评论(0) 收藏 举报

1. MongoDBCredential.java

public class MongoDBCredential {


private String databaseName;

private String username;

private String password;


public String getDatabaseName() {

return databaseName;

}


public void setDatabaseName(String databaseName) {

this.databaseName = databaseName;

}


public String getUsername() {

return username;

}


public void setUsername(String username) {

this.username = username;

}


public String getPassword() {

return password;

}


public void setPassword(String password) {

this.password = password;

}

}


2.MongoDBConfig.java

public class MongoDBConfig {


private String addresses;

private List<MongoDBCredentialT> credentials;


public String getAddresses() {

return addresses;

}


public void setAddresses(String addresses) {

this.addresses = addresses;

}


public List<MongoDBCredentialT> getCredentials() {

return credentials;

}


public void setCredentials(List<MongoDBCredentialT> credentials) {

this.credentials = credentials;

}

public List<MongoCredential> buildCredentials() {

List<MongoCredential> _credentials = new ArrayList<MongoCredential>();

for(MongoDBCredentialT credential : credentials) {

MongoCredential mc = MongoCredential.createCredential(credential.getUsername(), credential.getDatabaseName(), credential.getPassword().toCharArray());

_credentials.add(mc);

}

return _credentials;

}

public List<ServerAddress> buildAddresses() {

List<ServerAddress> addresses = new ArrayList<ServerAddress>();

String[] sources = this.addresses.split(";");

for(String item : sources) {

String[] hp = item.split(":");

addresses.add(new ServerAddress(hp[0], Integer.valueOf(hp[1])));

}

return addresses;

}

}


3.MongoDBDriver.java

public class MongoDBDriver {


private MongoClient mongoClient;

private MongoDBConfigT mongoDBConfig;

private Properties properties;


public void setMongoDBConfig(MongoDBConfigT mongoDBConfig) {

this.mongoDBConfig = mongoDBConfig;

}


public void setProperties(Properties properties) {

this.properties = properties;

}

private Integer connectionsPerHost = 32;

    private Integer threadsAllowedToBlockForConnectionMultiplier = 5;

    private Integer maxWaitTime = 30000;

    private Integer connectTimeout = 30000;

    private Integer socketTimeout = 30000;

    private Integer maxConnectionIdle = 60000;

    

    public void init() {

    if(properties != null) {

    String perHost = properties.getProperty("mongo.driver.connectionsPerHost");

    if(StringUtils.isNotBlank(perHost)) {

    connectionsPerHost = Integer.valueOf(perHost);

    }

    String multiplier = properties.getProperty("mongo.driver.threadsAllowedToBlockForConnectionMultiplier");

    if(StringUtils.isNotBlank(multiplier)) {

    threadsAllowedToBlockForConnectionMultiplier = Integer.valueOf(multiplier);

    }

    String _maxWaitTime = properties.getProperty("mongo.driver.maxWaitTime");

    if(StringUtils.isNotBlank(_maxWaitTime)) {

    maxWaitTime = Integer.valueOf(_maxWaitTime);

    }

    String _connectTimeout = properties.getProperty("mongo.driver.connectTimeout");

    if(StringUtils.isNotBlank(_connectTimeout)) {

    connectTimeout = Integer.valueOf(_connectTimeout);

    }

    String _socketTimeout = properties.getProperty("mongo.driver.socketTimeout");

    if(StringUtils.isNotBlank(_socketTimeout)) {

    socketTimeout = Integer.valueOf(_socketTimeout);

    }

    String _maxConnectionIdle = properties.getProperty("mongo.driver.maxConnectionIdle");

    if(StringUtils.isNotBlank(_maxConnectionIdle)) {

    maxConnectionIdle = Integer.valueOf(_maxConnectionIdle);

    }

    }

    

    execute();

    }

    

    public void execute() {

    MongoClientOptions.Builder builder = MongoClientOptions.builder();

    builder.connectionsPerHost(connectionsPerHost);

    builder.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);

    builder.maxWaitTime(maxWaitTime);

    builder.connectTimeout(connectTimeout);

    builder.socketTimeout(socketTimeout);

    builder.maxConnectionIdleTime(maxConnectionIdle);

    

    MongoClientOptions options = builder.build();

    mongoClient = new MongoClient(mongoDBConfig.buildAddresses(), mongoDBConfig.buildCredentials(), options);

    }


public MongoClient getMongoClient() {

return mongoClient;

}


public MongoDatabase getDatabase(String databaseName) {

return this.mongoClient.getDatabase(databaseName);

}

public DB getDB(String dbName) {

return this.mongoClient.getDB(dbName);

}

}


4.MongoDBClient.java

public class MongoDBClientT {

private MongoDBDriverT mongoDBDriver;

private String databaseName;


public void setMongoDBDriver(MongoDBDriverT mongoDBDriver) {

this.mongoDBDriver = mongoDBDriver;

}


public void setDatabaseName(String databaseName) {

this.databaseName = databaseName;

}

public MongoDatabase getDatabase() {

return this.mongoDBDriver.getDatabase(this.databaseName);

}

public static void main(String[] args) {

MongoDBDriverT driver = new MongoDBDriverT();

List<MongoDBCredentialT> credentials = new ArrayList<MongoDBCredentialT>();

MongoDBCredentialT credential = new MongoDBCredentialT();

credential.setDatabaseName("moin-fs");

credential.setUsername("root");

credential.setPassword("root123");

credentials.add(credential);

MongoDBConfigT config = new MongoDBConfigT();

config.setCredentials(credentials);

config.setAddresses("10.10.10.42:27017");

driver.setMongoDBConfig(config);

driver.init();

MongoDBClientT client = new MongoDBClientT();

client.setMongoDBDriver(driver);

client.setDatabaseName("moin-fs");

MongoDatabase db = client.getDatabase();

ListCollectionsIterable<Document> collectionsdb.listCollections();

MongoCursor<Document> it = collections.iterator();

while(it.hasNext()) {

System.out.print(it.next().toJson());

}

}

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6151次
    • 积分:173
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:5篇
    • 译文:5篇
    • 评论:0条