use of com.mongodb.event.ClusterClosedEvent in project mongo-java-driver by mongodb.
the class InitialDnsSeedlistDiscoveryTest method shouldDiscoverSrvRecord.
@Test
public void shouldDiscoverSrvRecord() throws InterruptedException {
assumeFalse(isServerlessTest());
assumeFalse(isError);
final CountDownLatch seedsLatch = new CountDownLatch(1);
final CountDownLatch hostsLatch = new CountDownLatch(1);
final ConnectionString connectionString = new ConnectionString(uri);
final SslSettings sslSettings = getSslSettings(connectionString);
assumeTrue("SSL settings don't match", getSslSettings().isEnabled() == sslSettings.isEnabled());
MongoClientSettings settings = MongoClientSettings.builder().applyToClusterSettings(new Block<ClusterSettings.Builder>() {
@Override
public void apply(final ClusterSettings.Builder builder) {
builder.applyConnectionString(connectionString).addClusterListener(new ClusterListener() {
@Override
public void clusterOpening(final ClusterOpeningEvent event) {
}
@Override
public void clusterClosed(final ClusterClosedEvent event) {
}
@Override
public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event) {
List<String> seedsList = event.getNewDescription().getServerDescriptions().stream().map(ServerDescription::getAddress).map(ServerAddress::toString).collect(Collectors.toList());
List<String> okHostsList = event.getNewDescription().getServerDescriptions().stream().filter(ServerDescription::isOk).map(ServerDescription::getAddress).map(ServerAddress::toString).collect(Collectors.toList());
hostsCheck(seedsList, seeds, numSeeds, seedsLatch);
hostsCheck(okHostsList, hosts, numHosts, hostsLatch);
}
});
}
private void hostsCheck(final List<String> actual, @Nullable final List<String> expected, @Nullable final Integer expectedSize, final CountDownLatch latch) {
if (expected == null && expectedSize == null) {
latch.countDown();
} else if (expected != null && actual.size() == expected.size() && actual.containsAll(expected)) {
latch.countDown();
} else if (expectedSize != null && actual.size() == expectedSize) {
latch.countDown();
}
}
}).applyToSslSettings(new Block<SslSettings.Builder>() {
@Override
public void apply(final SslSettings.Builder builder) {
builder.applySettings(sslSettings);
builder.invalidHostNameAllowed(true);
}
}).build();
try (MongoClient client = createMongoClient(settings)) {
assertTrue(seedsLatch.await(ClusterFixture.TIMEOUT, TimeUnit.SECONDS));
assertTrue(hostsLatch.await(ClusterFixture.TIMEOUT, TimeUnit.SECONDS));
assertTrue(client.getDatabase("admin").runCommand(new Document("ping", 1)).containsKey("ok"));
}
}
use of com.mongodb.event.ClusterClosedEvent in project mongo-java-driver by mongodb.
the class BaseCluster method close.
@Override
public void close() {
if (!isClosed()) {
isClosed = true;
phase.get().countDown();
clusterListener.clusterClosed(new ClusterClosedEvent(clusterId));
stopWaitQueueHandler();
}
}
use of com.mongodb.event.ClusterClosedEvent in project mongo-java-driver by mongodb.
the class AsynchronousClusterEventListenerTest method testEventsPublished.
@Test
public void testEventsPublished() throws InterruptedException {
AllClusterEventListener targetListener = new AllClusterEventListener();
ClusterId clusterId = new ClusterId();
ServerId serverId = new ServerId(clusterId, new ServerAddress());
ConnectionId connectionId = new ConnectionId(serverId);
AsynchronousClusterEventListener listener = AsynchronousClusterEventListener.startNew(clusterId, targetListener, targetListener, targetListener);
ClusterOpeningEvent clusterOpeningEvent = new ClusterOpeningEvent(clusterId);
listener.clusterOpening(clusterOpeningEvent);
assertEquals(clusterOpeningEvent, targetListener.take());
ClusterDescriptionChangedEvent clusterDescriptionChangedEvent = new ClusterDescriptionChangedEvent(clusterId, new ClusterDescription(ClusterConnectionMode.SINGLE, ClusterType.STANDALONE, Collections.emptyList()), new ClusterDescription(ClusterConnectionMode.SINGLE, ClusterType.STANDALONE, Collections.emptyList()));
listener.clusterDescriptionChanged(clusterDescriptionChangedEvent);
assertEquals(clusterDescriptionChangedEvent, targetListener.take());
ServerHeartbeatStartedEvent serverHeartbeatStartedEvent = new ServerHeartbeatStartedEvent(connectionId);
listener.serverHearbeatStarted(serverHeartbeatStartedEvent);
assertEquals(serverHeartbeatStartedEvent, targetListener.take());
ServerHeartbeatSucceededEvent serverHeartbeatSucceededEvent = new ServerHeartbeatSucceededEvent(connectionId, new BsonDocument(), 1, true);
listener.serverHeartbeatSucceeded(serverHeartbeatSucceededEvent);
assertEquals(serverHeartbeatSucceededEvent, targetListener.take());
ServerHeartbeatFailedEvent serverHeartbeatFailedEvent = new ServerHeartbeatFailedEvent(connectionId, 1, true, new IOException());
listener.serverHeartbeatFailed(serverHeartbeatFailedEvent);
assertEquals(serverHeartbeatFailedEvent, targetListener.take());
ServerOpeningEvent serverOpeningEvent = new ServerOpeningEvent(serverId);
listener.serverOpening(serverOpeningEvent);
assertEquals(serverOpeningEvent, targetListener.take());
ServerDescriptionChangedEvent serverDescriptionChangedEvent = new ServerDescriptionChangedEvent(serverId, ServerDescription.builder().address(new ServerAddress()).type(UNKNOWN).state(CONNECTING).build(), ServerDescription.builder().address(new ServerAddress()).type(STANDALONE).state(CONNECTED).build());
listener.serverDescriptionChanged(serverDescriptionChangedEvent);
assertEquals(serverDescriptionChangedEvent, targetListener.take());
ServerClosedEvent serverClosedEvent = new ServerClosedEvent(serverId);
listener.serverClosed(serverClosedEvent);
assertEquals(serverClosedEvent, targetListener.take());
ClusterClosedEvent clusterClosedEvent = new ClusterClosedEvent(clusterId);
listener.clusterClosed(clusterClosedEvent);
assertEquals(clusterClosedEvent, targetListener.take());
// The thread should die after publishing the ClusterClosedEvent
listener.getPublishingThread().join(5000);
}
use of com.mongodb.event.ClusterClosedEvent in project mongo-java-driver by mongodb.
the class BaseCluster method close.
@Override
public void close() {
if (!isClosed()) {
isClosed = true;
phase.get().countDown();
clusterListener.clusterClosed(new ClusterClosedEvent(clusterId));
stopWaitQueueHandler();
}
}
use of com.mongodb.event.ClusterClosedEvent in project mongo-java-driver by mongodb.
the class LoadBalancedCluster method close.
@Override
public void close() {
if (!closed.getAndSet(true)) {
LOGGER.info(format("Cluster closed with id %s", clusterId));
if (dnsSrvRecordMonitor != null) {
dnsSrvRecordMonitor.close();
}
ClusterableServer localServer;
lock.lock();
try {
condition.signalAll();
localServer = server;
} finally {
lock.unlock();
}
if (localServer != null) {
localServer.close();
}
clusterListener.clusterClosed(new ClusterClosedEvent(clusterId));
}
}
Aggregations