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());
}
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;
}
Aggregations