diff options
author | Kate Hsuan <kate.hsuan@qct.io> | 2019-04-15 13:31:54 +0800 |
---|---|---|
committer | Kate Hsuan <kate.hsuan@qct.io> | 2019-04-15 13:31:54 +0800 |
commit | 4a315e06b47803f80b54282c98db8aa52e3cc585 (patch) | |
tree | 5cb1a0f32cee890ab04150f07c00941f9026dd41 | |
parent | f9b8345c99bdf176aa438e555f7f29003027b297 (diff) |
Fix Mongo connection method
Address and credential must be a list.
Issue-ID: DCAEGEN2-1411
Change-Id: I5705d4086476ea02d9eb54c0cf4c329c1848f5cf
Signed-off-by: Kate Hsuan <kate.hsuan@qct.io>
-rw-r--r-- | components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/MongodbService.java | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/MongodbService.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/MongodbService.java index 35426afb..fb3f806c 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/MongodbService.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/MongodbService.java @@ -48,6 +48,7 @@ import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; +import com.mongodb.DB; /** * Service for using MongoDB @@ -62,6 +63,7 @@ public class MongodbService { @Autowired private ApplicationConfiguration config; + private boolean dbReady = false; @Autowired private DbService dbService; @@ -98,13 +100,30 @@ public class MongodbService { builder.sslEnabled(Boolean.TRUE.equals(mongodb.getEncrypt()));// getEncrypt() can be null } MongoClientOptions options = builder.build(); + List<ServerAddress> addrs = new ArrayList<ServerAddress>(); - if (credential == null) { - mongoClient = new MongoClient(new ServerAddress(host, port), options); - } else { - mongoClient = new MongoClient(new ServerAddress(host, port), credential, options); + addrs.add(new ServerAddress(host, port)); // FIXME should be a list of address + + + try { + if(StringUtils.isNoneBlank(userName) && StringUtils.isNoneBlank(password)) + { + credential = MongoCredential.createCredential(userName, databaseName, password.toCharArray()); + List<MongoCredential> credentialList = new ArrayList<MongoCredential>(); + credentialList.add(credential); + mongoClient = new MongoClient(addrs, credentialList, options); + }else + { + mongoClient = new MongoClient(addrs, options); + } + }catch(Exception ex){ + dbReady = false; + log.error("Fail to initiate MongoDB" + mongodb.getHost()); + return; } - database = mongoClient.getDatabase(databaseName); + database = mongoClient.getDatabase(mongodb.getDatabase()); + dbReady = true; + } @PreDestroy @@ -113,6 +132,8 @@ public class MongodbService { } public void saveJsons(Topic topic, List<JSONObject> jsons) { + if(dbReady == false) + return; List<Document> documents = new ArrayList<>(jsons.size()); for (JSONObject json : jsons) { //convert org.json JSONObject to MongoDB Document |