2

I've got a MongoDB cartridge installed on openshift and i'm having troubles with connecting to it from java code. Ip address, port and credentials are taken from openshift's RockMongo cartridge. The following method invocation:

public Document insert(String audio, String username) {
    Document document = new Document();
    document.put("username", username);
    document.put("audio", audio);
    document.put("timestamp", new Date());
    collection.insertOne(document);

    return document;
}

and this mongo client configuration:

private static MongoClient build() throws UnknownHostException {
    if (mongoClient == null) {
        mongoClient = new MongoClient(
                new MongoClientURI( "mongodb://admin:password@X.X.X.X:27017/dbName"));
    }

        return mongoClient;
    }

public static MongoCollection<Document> getCollection(String collectionName) {
        try {
            build();
        } catch (UnknownHostException e) {

        }
        MongoDatabase db = mongoClient.getDatabase(dbName);

        MongoCollection<Document> collection = db.getCollection(collectionName);
        return collection;
    }

results in INFO: No server chosen by PrimaryServerSelector from cluster description ClusterDescription, and exception: Timed out after 30000 ms while waiting for a server that matches PrimaryServerSelector.

EDIT: I can't connect with mongoDB service on openshift via mongo terminal application either: "exception: connect failed", so I think it's openshift configuration issue. Port forwarding and the service itself are started

mcjkwlczk
  • 145
  • 15

1 Answers1

0

I suppose you have not correctly configure cluster (message in logs told about this problem), I'm not sure how OpenShift Cratridge works, but I recommend you to check if it has mondo-db correctly started. Check it via ssh client and run mongo-db command to check its status and if started. Take a look on this question: Java MongoClient cannot connect to primary, I suppose it give you some idea how to check where you have problem.

Community
  • 1
  • 1
sphinks
  • 3,048
  • 8
  • 39
  • 55