Search in sources :

Example 6 with MongoConfigurationException

use of com.mongodb.MongoConfigurationException in project mongo-java-driver by mongodb.

the class LoadBalancedClusterTest method shouldTimeoutSelectServerAsynchronouslyWhenThereIsSRVLookupException.

@Test
public void shouldTimeoutSelectServerAsynchronouslyWhenThereIsSRVLookupException() {
    // given
    String srvHostName = "foo.bar.com";
    ServerAddress resolvedServerAddress = new ServerAddress("host1");
    ClusterableServer expectedServer = mock(ClusterableServer.class);
    ClusterSettings clusterSettings = ClusterSettings.builder().serverSelectionTimeout(10, MILLISECONDS).mode(ClusterConnectionMode.LOAD_BALANCED).srvHost(srvHostName).build();
    ClusterableServerFactory serverFactory = mockServerFactory(resolvedServerAddress, expectedServer);
    DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory = mock(DnsSrvRecordMonitorFactory.class);
    when(dnsSrvRecordMonitorFactory.create(eq(srvHostName), eq(clusterSettings.getSrvServiceName()), any())).thenAnswer(invocation -> new TestDnsSrvRecordMonitor(invocation.getArgument(2)).sleepTime(Duration.ofMillis(1)).exception(new MongoConfigurationException("Unable to resolve SRV record")));
    cluster = new LoadBalancedCluster(new ClusterId(), clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
    FutureResultCallback<ServerTuple> callback = new FutureResultCallback<>();
    cluster.selectServerAsync(mock(ServerSelector.class), callback);
    MongoTimeoutException exception = assertThrows(MongoTimeoutException.class, callback::get);
    assertEquals("Timed out after 10 ms while waiting to resolve SRV records for foo.bar.com. " + "Resolution exception was 'com.mongodb.MongoConfigurationException: Unable to resolve SRV record'", exception.getMessage());
}
Also used : FutureResultCallback(com.mongodb.async.FutureResultCallback) MongoConfigurationException(com.mongodb.MongoConfigurationException) ClusterSettings(com.mongodb.connection.ClusterSettings) ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) MongoTimeoutException(com.mongodb.MongoTimeoutException) ServerSelector(com.mongodb.selector.ServerSelector) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 7 with MongoConfigurationException

use of com.mongodb.MongoConfigurationException in project mongo-java-driver by mongodb.

the class DefaultDnsResolver method resolveAdditionalQueryParametersFromTxtRecords.

/*
      A TXT record is just a string
      We require each to be one or more query parameters for a MongoDB connection string.
      Here we concatenate TXT records together with a '&' separator as required by connection strings
    */
@Override
public String resolveAdditionalQueryParametersFromTxtRecords(final String host) {
    String additionalQueryParameters = "";
    InitialDirContext dirContext = createDnsDirContext();
    try {
        Attributes attributes = dirContext.getAttributes(host, new String[] { "TXT" });
        Attribute attribute = attributes.get("TXT");
        if (attribute != null) {
            NamingEnumeration<?> txtRecordEnumeration = attribute.getAll();
            if (txtRecordEnumeration.hasMore()) {
                // Remove all space characters, as the DNS resolver for TXT records inserts a space character
                // between each character-string in a single TXT record.  That whitespace is spurious in
                // this context and must be removed
                additionalQueryParameters = ((String) txtRecordEnumeration.next()).replaceAll("\\s", "");
                if (txtRecordEnumeration.hasMore()) {
                    throw new MongoConfigurationException(format("Multiple TXT records found for host '%s'.  Only one is permitted", host));
                }
            }
        }
    } catch (NamingException e) {
        throw new MongoConfigurationException("Unable to look up TXT record for host " + host, e);
    } finally {
        try {
            dirContext.close();
        } catch (NamingException e) {
        // ignore
        }
    }
    return additionalQueryParameters;
}
Also used : MongoConfigurationException(com.mongodb.MongoConfigurationException) Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext)

Aggregations

MongoConfigurationException (com.mongodb.MongoConfigurationException)7 NamingException (javax.naming.NamingException)3 Test (org.junit.jupiter.api.Test)3 MongoTimeoutException (com.mongodb.MongoTimeoutException)2 ServerAddress (com.mongodb.ServerAddress)2 ClusterId (com.mongodb.connection.ClusterId)2 ClusterSettings (com.mongodb.connection.ClusterSettings)2 ServerSelector (com.mongodb.selector.ServerSelector)2 Attribute (javax.naming.directory.Attribute)2 Attributes (javax.naming.directory.Attributes)2 InitialDirContext (javax.naming.directory.InitialDirContext)2 RepeatedTest (org.junit.jupiter.api.RepeatedTest)2 FutureResultCallback (com.mongodb.async.FutureResultCallback)1 ServerDescription (com.mongodb.connection.ServerDescription)1 LatencyMinimizingServerSelector (com.mongodb.internal.selector.LatencyMinimizingServerSelector)1 ReadPreferenceServerSelector (com.mongodb.internal.selector.ReadPreferenceServerSelector)1 WritableServerSelector (com.mongodb.internal.selector.WritableServerSelector)1 CompositeServerSelector (com.mongodb.selector.CompositeServerSelector)1 ArrayList (java.util.ArrayList)1 BsonArray (org.bson.BsonArray)1