Skip to content

Commit

Permalink
Polish "Migrating to mongo client 4.x"
Browse files Browse the repository at this point in the history
  • Loading branch information
izeye committed Mar 23, 2021
1 parent 7a377e7 commit b8c71f0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @author Christophe Bornet
* @author Jonatan Ivanov
* @since 1.2.0
* @implNote This implementation requires MongoDB Java driver 4 or later.
*/
@NonNullApi
@NonNullFields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ void setup() {
clusterId = new AtomicReference<>();
MongoClientSettings settings = MongoClientSettings.builder()
.addCommandListener(new MongoMetricsCommandListener(registry))
.applyToClusterSettings(builder -> builder.hosts(singletonList(new ServerAddress(HOST, port))))
.applyToClusterSettings(builder -> builder.addClusterListener(new ClusterListener() {
@Override
public void clusterOpening(ClusterOpeningEvent event) {
clusterId.set(event.getClusterId().getValue());
}
})).build();
.applyToClusterSettings(builder -> builder
.hosts(singletonList(new ServerAddress(HOST, port)))
.addClusterListener(new ClusterListener() {
@Override
public void clusterOpening(ClusterOpeningEvent event) {
clusterId.set(event.getClusterId().getValue());
}
})).build();
mongo = MongoClients.create(settings);
}

Expand Down Expand Up @@ -110,13 +111,14 @@ public Iterable<Tag> commandTags(CommandEvent event) {
};
MongoClientSettings settings = MongoClientSettings.builder()
.addCommandListener(new MongoMetricsCommandListener(registry, tagsProvider))
.applyToClusterSettings(builder -> builder.hosts(singletonList(new ServerAddress(HOST, port))))
.applyToClusterSettings(builder -> builder.addClusterListener(new ClusterListener() {
@Override
public void clusterOpening(ClusterOpeningEvent event) {
clusterId.set(event.getClusterId().getValue());
}
}))
.applyToClusterSettings(builder -> builder
.hosts(singletonList(new ServerAddress(HOST, port)))
.addClusterListener(new ClusterListener() {
@Override
public void clusterOpening(ClusterOpeningEvent event) {
clusterId.set(event.getClusterId().getValue());
}
}))
.build();
try (MongoClient mongo = MongoClients.create(settings)) {
mongo.getDatabase("test")
Expand All @@ -143,13 +145,14 @@ public Iterable<Tag> commandTags(CommandEvent event) {
};
MongoClientSettings settings = MongoClientSettings.builder()
.addCommandListener(new MongoMetricsCommandListener(registry, tagsProvider))
.applyToClusterSettings(builder -> builder.hosts(singletonList(new ServerAddress(HOST, port))))
.applyToClusterSettings(builder -> builder.addClusterListener(new ClusterListener() {
@Override
public void clusterOpening(ClusterOpeningEvent event) {
clusterId.set(event.getClusterId().getValue());
}
}))
.applyToClusterSettings(builder -> builder
.hosts(singletonList(new ServerAddress(HOST, port)))
.addClusterListener(new ClusterListener() {
@Override
public void clusterOpening(ClusterOpeningEvent event) {
clusterId.set(event.getClusterId().getValue());
}
}))
.build();
try (MongoClient mongo = MongoClients.create(settings)) {
mongo.getDatabase("test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ void shouldCreatePoolMetrics() {
.minSize(2)
.addConnectionPoolListener(new MongoMetricsConnectionPoolListener(registry))
)
.applyToClusterSettings(builder -> builder.hosts(singletonList(new ServerAddress(HOST, port))))
.applyToClusterSettings(builder -> builder.addClusterListener(new ClusterListener() {
@Override
public void clusterOpening(ClusterOpeningEvent event) {
clusterId.set(event.getClusterId().getValue());
}
}))
.applyToClusterSettings(builder -> builder
.hosts(singletonList(new ServerAddress(HOST, port)))
.addClusterListener(new ClusterListener() {
@Override
public void clusterOpening(ClusterOpeningEvent event) {
clusterId.set(event.getClusterId().getValue());
}
}))
.build();
MongoClient mongo = MongoClients.create(settings);

Expand Down Expand Up @@ -98,13 +99,14 @@ void shouldCreatePoolMetricsWithCustomTags() {
.minSize(2)
.addConnectionPoolListener(connectionPoolListener)
)
.applyToClusterSettings(builder -> builder.hosts(singletonList(new ServerAddress(HOST, port))))
.applyToClusterSettings(builder -> builder.addClusterListener(new ClusterListener() {
@Override
public void clusterOpening(ClusterOpeningEvent event) {
clusterId.set(event.getClusterId().getValue());
}
}))
.applyToClusterSettings(builder -> builder
.hosts(singletonList(new ServerAddress(HOST, port)))
.addClusterListener(new ClusterListener() {
@Override
public void clusterOpening(ClusterOpeningEvent event) {
clusterId.set(event.getClusterId().getValue());
}
}))
.build();
MongoClient mongo = MongoClients.create(settings);

Expand Down

0 comments on commit b8c71f0

Please sign in to comment.