diff --git a/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/CommandListenerBuildItem.java b/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/CommandListenerBuildItem.java new file mode 100644 index 0000000000000..159bb5d856c8f --- /dev/null +++ b/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/CommandListenerBuildItem.java @@ -0,0 +1,18 @@ +package io.quarkus.mongodb.deployment; + +import java.util.List; + +import io.quarkus.builder.item.SimpleBuildItem; + +public final class CommandListenerBuildItem extends SimpleBuildItem { + + private List commandListenerClassNames; + + public CommandListenerBuildItem(List commandListenerClassNames) { + this.commandListenerClassNames = commandListenerClassNames; + } + + public List getCommandListenerClassNames() { + return commandListenerClassNames; + } +} diff --git a/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/MongoClientProcessor.java b/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/MongoClientProcessor.java index 68434bbd8275b..533fa7446a622 100644 --- a/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/MongoClientProcessor.java +++ b/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/MongoClientProcessor.java @@ -23,6 +23,7 @@ import org.jboss.jandex.IndexView; import com.mongodb.client.MongoClient; +import com.mongodb.event.CommandListener; import com.mongodb.event.ConnectionPoolListener; import io.quarkus.arc.deployment.AdditionalBeanBuildItem; @@ -82,11 +83,21 @@ BsonDiscriminatorBuildItem collectBsonDiscriminators(CombinedIndexBuildItem inde } @BuildStep - List addCodecsAndDiscriminatorsToNative(CodecProviderBuildItem codecProviders, - BsonDiscriminatorBuildItem bsonDiscriminators) { + CommandListenerBuildItem collectCommandListeners(CombinedIndexBuildItem indexBuildItem) { + Collection commandListenerClasses = indexBuildItem.getIndex() + .getAllKnownImplementors(DotName.createSimple(CommandListener.class.getName())); + List names = commandListenerClasses.stream().map(ci -> ci.name().toString()).collect(Collectors.toList()); + return new CommandListenerBuildItem(names); + } + + @BuildStep + List addCodecsAndDiscriminatorsAndListenersToNative(CodecProviderBuildItem codecProviders, + BsonDiscriminatorBuildItem bsonDiscriminators, + CommandListenerBuildItem commandListeners) { List reflectiveClassNames = new ArrayList<>(); reflectiveClassNames.addAll(codecProviders.getCodecProviderClassNames()); reflectiveClassNames.addAll(bsonDiscriminators.getBsonDiscriminatorClassNames()); + reflectiveClassNames.addAll(commandListeners.getCommandListenerClassNames()); return reflectiveClassNames.stream() .map(s -> new ReflectiveClassBuildItem(true, true, false, s)) @@ -149,6 +160,7 @@ void build( SslNativeConfigBuildItem sslNativeConfig, CodecProviderBuildItem codecProvider, BsonDiscriminatorBuildItem bsonDiscriminator, + CommandListenerBuildItem commandListener, List connectionPoolListenerProvider, BuildProducer mongoConnections, BuildProducer syntheticBeanBuildItemBuildProducer, @@ -171,7 +183,7 @@ void build( syntheticBeanBuildItemBuildProducer.produce(SyntheticBeanBuildItem.configure(MongoClientSupport.class) .scope(Singleton.class) .supplier(recorder.mongoClientSupportSupplier(codecProvider.getCodecProviderClassNames(), - bsonDiscriminator.getBsonDiscriminatorClassNames(), + bsonDiscriminator.getBsonDiscriminatorClassNames(), commandListener.getCommandListenerClassNames(), poolListenerList, sslNativeConfig.isExplicitlyDisabled())) .done()); diff --git a/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MockCommandListener.java b/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MockCommandListener.java index 606bc774b6f91..a5d177e3fae81 100644 --- a/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MockCommandListener.java +++ b/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MockCommandListener.java @@ -1,21 +1,22 @@ package io.quarkus.mongodb; -import javax.enterprise.context.ApplicationScoped; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anyOf; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; import com.mongodb.event.CommandListener; import com.mongodb.event.CommandStartedEvent; -import io.quarkus.test.Mock; - -@Mock -@ApplicationScoped public class MockCommandListener implements CommandListener { private CommandStartedEvent commandStartedEvent; @Override - public void commandStarted(CommandStartedEvent event) { - this.commandStartedEvent = event; + public void commandStarted(CommandStartedEvent startedEvent) { + this.commandStartedEvent = startedEvent; + assertThat(startedEvent, notNullValue()); + assertThat(startedEvent.getCommandName(), anyOf(equalTo("listDatabases"), equalTo("endSessions"))); } public CommandStartedEvent getCommandStartedEvent() { diff --git a/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MongoCommandListenerTest.java b/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MongoCommandListenerTest.java index fa4441e762eba..b72930798b6c1 100644 --- a/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MongoCommandListenerTest.java +++ b/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MongoCommandListenerTest.java @@ -11,7 +11,6 @@ import org.junit.jupiter.api.extension.RegisterExtension; import com.mongodb.client.MongoClient; -import com.mongodb.event.CommandStartedEvent; import io.quarkus.test.QuarkusUnitTest; @@ -20,9 +19,6 @@ public class MongoCommandListenerTest extends MongoTestBase { @Inject MongoClient client; - @Inject - MockCommandListener listener; - @RegisterExtension static final QuarkusUnitTest config = new QuarkusUnitTest() .setArchiveProducer( @@ -39,10 +35,6 @@ void cleanup() { @Test void testClientInitialization() { assertThat(client.listDatabaseNames().first()).isNotEmpty(); - assertThat(listener).isNotNull(); - CommandStartedEvent startedEvent = listener.getCommandStartedEvent(); - assertThat(startedEvent).isNotNull(); - assertThat(startedEvent.getCommandName()).isEqualTo("listDatabases"); } } diff --git a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientRecorder.java b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientRecorder.java index 0c7dd436c3ef1..4a0ef368f595b 100644 --- a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientRecorder.java +++ b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientRecorder.java @@ -9,7 +9,6 @@ import javax.enterprise.util.AnnotationLiteral; import com.mongodb.client.MongoClient; -import com.mongodb.event.CommandListener; import com.mongodb.event.ConnectionPoolListener; import io.quarkus.arc.Arc; @@ -22,7 +21,7 @@ @Recorder public class MongoClientRecorder { - public Supplier mongoClientSupportSupplier(List codecProviders, List bsonDiscriminators, + public Supplier mongoClientSupportSupplier(List codecProviders, List bsonDiscriminators, List commandListeners, List> connectionPoolListenerSuppliers, boolean disableSslSupport) { return new Supplier() { @Override @@ -32,14 +31,9 @@ public MongoClientSupport get() { for (Supplier item : connectionPoolListenerSuppliers) { connectionPoolListeners.add(item.get()); } - final CommandListener commandListener = Arc.container().instance(CommandListener.class).get(); - MongoClientSupport mongoClientSupport = new MongoClientSupport(codecProviders, bsonDiscriminators, + return new MongoClientSupport(codecProviders, bsonDiscriminators, commandListeners, connectionPoolListeners, disableSslSupport); - if (null != commandListener) { - mongoClientSupport.addCommandListener(commandListener); - } - return mongoClientSupport; } }; } diff --git a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientSupport.java b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientSupport.java index e371c898e5778..aeb2bf8525161 100644 --- a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientSupport.java +++ b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClientSupport.java @@ -1,9 +1,7 @@ package io.quarkus.mongodb.runtime; -import java.util.ArrayList; import java.util.List; -import com.mongodb.event.CommandListener; import com.mongodb.event.ConnectionPoolListener; public class MongoClientSupport { @@ -11,21 +9,11 @@ public class MongoClientSupport { private final List codecProviders; private final List bsonDiscriminators; private final List connectionPoolListeners; - private final List commandListeners; + private final List commandListeners; private final boolean disableSslSupport; public MongoClientSupport(List codecProviders, List bsonDiscriminators, - List connectionPoolListeners, boolean disableSslSupport) { - this.codecProviders = codecProviders; - this.bsonDiscriminators = bsonDiscriminators; - this.connectionPoolListeners = connectionPoolListeners; - this.commandListeners = new ArrayList<>(); - this.disableSslSupport = disableSslSupport; - } - - public MongoClientSupport(List codecProviders, List bsonDiscriminators, - List connectionPoolListeners, List commandListeners, - boolean disableSslSupport) { + List commandListeners, List connectionPoolListeners, boolean disableSslSupport) { this.codecProviders = codecProviders; this.bsonDiscriminators = bsonDiscriminators; this.connectionPoolListeners = connectionPoolListeners; @@ -45,14 +33,10 @@ public List getConnectionPoolListeners() { return connectionPoolListeners; } - public List getCommandListeners() { + public List getCommandListeners() { return commandListeners; } - public void addCommandListener(CommandListener commandListener) { - commandListeners.add(commandListener); - } - public boolean isDisableSslSupport() { return disableSslSupport; } diff --git a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClients.java b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClients.java index 4992b5bee755c..45fa557910ecf 100644 --- a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClients.java +++ b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClients.java @@ -44,6 +44,7 @@ import com.mongodb.connection.ServerSettings; import com.mongodb.connection.SocketSettings; import com.mongodb.connection.SslSettings; +import com.mongodb.event.CommandListener; import com.mongodb.event.ConnectionPoolListener; import io.quarkus.mongodb.impl.ReactiveMongoClientImpl; @@ -249,7 +250,11 @@ private MongoClientSettings createMongoConfiguration(MongoClientConfig config) { CodecRegistries.fromProviders(providers)); settings.codecRegistry(registry); - settings.commandListenerList(mongoClientSupport.getCommandListeners()); + List listeners = new ArrayList<>(); + if (!mongoClientSupport.getCommandListeners().isEmpty()) { + listeners.addAll(getCommandListeners(mongoClientSupport.getCommandListeners())); + } + settings.commandListenerList(listeners); config.applicationName.ifPresent(settings::applicationName); @@ -386,6 +391,21 @@ private List getCodecProviders(List classNames) { return providers; } + private List getCommandListeners(List classNames) { + List listeners = new ArrayList<>(); + for (String name : classNames) { + try { + Class clazz = Thread.currentThread().getContextClassLoader().loadClass(name); + Constructor clazzConstructor = clazz.getConstructor(); + listeners.add((CommandListener) clazzConstructor.newInstance()); + } catch (Exception e) { + LOGGER.warnf(e, "Unable to load the command listener class %s", name); + } + } + + return listeners; + } + @PreDestroy public void stop() { for (MongoClient client : mongoclients.values()) {