3

I am trying to connect to aws DocumentDB with async mongoClient.

I created a DocumentDB cluster in aws and success connect via ssh command line.

I went over here and created MongoClient and success connected and insert events.

But when I tried create com.mongodb.async.client.MongoClient, connection failed with folowing error:

No server chosen by WritableServerSelector from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=aws-cluster:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message}, caused by {io.netty.handler.timeout.ReadTimeoutException}}]}. Waiting for 30000 ms before timing out.

    ClusterSettings clusterSettings = ClusterSettings.builder()
                .applyConnectionString(new ConnectionString(connectionString)).build();
        List<MongoCredential> credentials = new ArrayList<>();
        credentials.add(
                 MongoCredential.createCredential(
                         mongoUserName,
                         mongoDBName,
                         mongoPassword));

    MongoClientSettings settings = MongoClientSettings.builder()
            .credentialList(credentials)
            .clusterSettings(clusterSettings)
            .streamFactoryFactory(new NettyStreamFactoryFactory())
            .writeConcern(WriteConcern.ACKNOWLEDGED)
            .build();
    com.mongodb.async.client.MongoClient mongoClient = MongoClients.create(settings);



    MongoDatabase testDB = mongoClient.getDatabase("myDB");
    MongoCollection<Document> collection = testDB.getCollection("test");
    Document doc = new Document("name", "MongoDB").append("type", "database");

    //**trying insert document => here I got an error**
    collection.insertOne(doc, new SingleResultCallback<Void>() {
            @Override
            public void onResult(final Void result, final Throwable t) {
                System.out.println("Inserted!");
            }
        });

Do you have any ideas, why does it happen?

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
Maria Dorohin
  • 355
  • 4
  • 17

3 Answers3

2

I solved it by using uri:

String uri = "mongodb://<username>:<Password>@<hostname>:27017/?ssl=true&ssl_ca_certs=cert";
MongoClientSettings settings = MongoClientSettings.builder()
                        .streamFactoryFactory(new NettyStreamFactoryFactory())
                        .applyConnectionString(new ConnectionString(uri))
                        .build();

com.mongodb.async.client.MongoClient mongoClient = MongoClients.create(settings);
Maria Dorohin
  • 355
  • 4
  • 17
0

I encountered a similar error , for me it was related to the TLS configs.

I disabled the TLS in documentDB https://docs.aws.amazon.com/documentdb/latest/developerguide/security.encryption.ssl.html

Shachaf.Gortler
  • 5,655
  • 14
  • 43
  • 71
0

In my case I had to restart the cluster after disabling the TLS. (TLS was not needed for the use case). After the restart the connection was established successfully.

Claudiu Matei
  • 4,091
  • 3
  • 19
  • 33