Redis
class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
lazy-init="true">
MongoDB
public class DBUtil {
private MongoClient mongoClient;
private MongoDatabase database;
private String host;
private String dbName;
private String username;
private String password;
private String replSetName;
public String getHost() {
return this.host;
}
public void setHost(String host) {
this.host = host;
}
public String getDbName() {
return this.dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public void init() throws Exception {
String[] hostArr = this.host.split("\\,");
List serverList = new ArrayList<>();
for (String h : hostArr) {
serverList.add(new ServerAddress(h));
}
Object credentialsList = null;
if ((this.username != null) && (this.username.trim().length() > 0) && (this.password != null)
&& (this.password.trim().length() > 0)) {
credentialsList = new ArrayList<>();
((List) credentialsList).add(MongoCredential.createCredential(this.username, this.dbName, this.password.toCharArray()));
}
//this.mongoClient = new MongoClient(serverList, (List) credentialsList);
MongoClientOptions options = MongoClientOptions.builder()
.requiredReplicaSetName(replSetName)
.readPreference(ReadPreference.secondaryPreferred())
.build();
this.mongoClient = new MongoClient(serverList, options);
this.database = this.mongoClient.getDatabase(this.dbName);
}
public void destroy() {
if (this.mongoClient != null) {
this.mongoClient.close();
}
}
public MongoCollection getColl(String name) {
return this.database.getCollection(name);
}
public int nextValue(String counterName) {
Document filter = new Document("name", counterName);
Document fields = new Document("value", Boolean.valueOf(true));
Document update = new Document("$inc", new Document("value", Integer.valueOf(1)));
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().projection(fields).upsert(true);
Document newObj = (Document) this.database.getCollection("counter").findOneAndUpdate(filter, update, options);
return newObj.getInteger("value").intValue();
}
}
mongodb.replSetName= mongodb.dbName= mongodb.userName= mongodb.password=