cg3410的博客

不积跬步无以至千里,不积小流无以成江海。

mongoClient备忘

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());

}

}

}


阅读更多
文章标签: mongoClient
上一篇使用springmvc拦截器进行登录控制
下一篇深入理解java虚拟机笔记(第二章:java内存区域)
想对作者说点什么? 我来说一句

mongoclient.lib

2015年02月02日 34.05MB 下载

mongoclient mac客户端

2018年05月21日 120.69MB 下载

好用桌面日历备忘软件

2010年07月16日 956KB 下载

linxux 备忘 linxux 笔记

2011年06月06日 335B 下载

propertyGrid 控件自定义简单示例

2011年05月08日 50KB 下载

git-flow 备忘清单

2018年06月29日 515KB 下载

C语言课程设计 备忘

2011年12月23日 382KB 下载

AS400 命令备忘

2008年05月07日 1KB 下载

数据库语句大全,留着备忘

2011年08月29日 21KB 下载

PADS非常用操作备忘

2010年12月11日 26KB 下载

没有更多推荐了,返回首页

关闭
关闭