From 4eb8ad81d4329f46aa12d53b9176f64a91a115a9 Mon Sep 17 00:00:00 2001 From: Andrus Adamchik Date: Sat, 9 Dec 2023 19:07:51 -0500 Subject: [PATCH] Switching to injectable factories per bootique/bootique#345 --- .../rabbitmq/client/RabbitMQModule.java | 14 ++----- .../rabbitmq/client/RmqObjectsFactory.java | 38 ++++++++++--------- .../AMQPConnectionFactoryFactory.java | 5 +-- .../connection/ConnectionFactoryFactory.java | 7 ++-- .../URIConnectionFactoryFactory.java | 5 +-- .../client/pubsub/RmqSubEndpointFactory.java | 13 +++++-- .../tester/ConnectionPropertyBuilder.java | 4 +- .../tester/TestConnectionFactoryFactory.java | 13 ++++++- 8 files changed, 54 insertions(+), 45 deletions(-) diff --git a/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/RabbitMQModule.java b/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/RabbitMQModule.java index cb736cf..3b8d29c 100644 --- a/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/RabbitMQModule.java +++ b/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/RabbitMQModule.java @@ -23,12 +23,10 @@ import io.bootique.ModuleCrate; import io.bootique.config.ConfigurationFactory; import io.bootique.di.Binder; -import io.bootique.di.Injector; import io.bootique.di.Provides; import io.bootique.rabbitmq.client.channel.RmqChannelManager; import io.bootique.rabbitmq.client.connection.RmqConnectionManager; import io.bootique.rabbitmq.client.topology.RmqTopologyManager; -import io.bootique.shutdown.ShutdownManager; import javax.inject.Singleton; @@ -56,11 +54,8 @@ RmqObjectsFactory provideRmqObjectsFactory(ConfigurationFactory configFactory) { @Singleton @Provides - RmqConnectionManager provideConnectionManager( - RmqObjectsFactory factory, - ShutdownManager shutdownManager, - Injector injector) { - return factory.createConnectionManager(injector, shutdownManager); + RmqConnectionManager provideConnectionManager(RmqObjectsFactory factory) { + return factory.createConnectionManager(); } @Singleton @@ -80,8 +75,7 @@ RmqTopologyManager provideTopologyManager(RmqObjectsFactory factory) { RmqEndpoints provideEndpoints( RmqObjectsFactory factory, RmqChannelManager channelManager, - RmqTopologyManager topologyManager, - ShutdownManager shutdownManager) { - return factory.createEndpoints(channelManager, topologyManager, shutdownManager); + RmqTopologyManager topologyManager) { + return factory.createEndpoints(channelManager, topologyManager); } } diff --git a/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/RmqObjectsFactory.java b/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/RmqObjectsFactory.java index 6c9d6d1..1049c58 100644 --- a/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/RmqObjectsFactory.java +++ b/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/RmqObjectsFactory.java @@ -22,7 +22,6 @@ import com.rabbitmq.client.ConnectionFactory; import io.bootique.annotation.BQConfig; import io.bootique.annotation.BQConfigProperty; -import io.bootique.di.Injector; import io.bootique.rabbitmq.client.channel.PoolingChannelManager; import io.bootique.rabbitmq.client.channel.RmqChannelManager; import io.bootique.rabbitmq.client.channel.SimpleChannelManager; @@ -35,6 +34,7 @@ import io.bootique.rabbitmq.client.topology.*; import io.bootique.shutdown.ShutdownManager; +import javax.inject.Inject; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -47,6 +47,8 @@ @BQConfig public class RmqObjectsFactory { + private final ShutdownManager shutdownManager; + private Map connections; private Map exchanges; private Map queues; @@ -54,6 +56,18 @@ public class RmqObjectsFactory { private Map sub; private int channelPoolCapacity; + @Inject + public RmqObjectsFactory(ShutdownManager shutdownManager) { + this.shutdownManager = shutdownManager; + } + + public RmqConnectionManager createConnectionManager() { + Map factories = createConnectionFactories(); + return shutdownManager.onShutdown( + new RmqConnectionManager(factories), + RmqConnectionManager::shutdown); + } + /** * @since 3.0 */ @@ -73,17 +87,10 @@ public RmqTopologyManager createTopologyManager() { createQueueConfigs()); } - public RmqEndpoints createEndpoints(RmqChannelManager channelManager, RmqTopologyManager topologyManager, ShutdownManager shutdownManager) { + public RmqEndpoints createEndpoints(RmqChannelManager channelManager, RmqTopologyManager topologyManager) { return new RmqEndpoints( createPubEndpoints(channelManager, topologyManager), - createSubEndpoints(channelManager, topologyManager, shutdownManager)); - } - - public RmqConnectionManager createConnectionManager(Injector injector, ShutdownManager shutdownManager) { - Map factories = createConnectionFactories(injector); - return shutdownManager.onShutdown( - new RmqConnectionManager(factories), - RmqConnectionManager::shutdown); + createSubEndpoints(channelManager, topologyManager)); } protected Map createQueueConfigs() { @@ -106,13 +113,13 @@ protected Map createExchangeConfigs() { return map; } - protected Map createConnectionFactories(Injector injector) { + protected Map createConnectionFactories() { if (connections == null || connections.isEmpty()) { return Collections.emptyMap(); } Map map = new HashMap<>(); - connections.forEach((k, v) -> map.put(k, v.createConnectionFactory(k, injector))); + connections.forEach((k, v) -> map.put(k, v.createConnectionFactory(k))); return map; } @@ -129,17 +136,14 @@ protected Map createPubEndpoints( return map; } - protected Map createSubEndpoints( - RmqChannelManager channelManager, - RmqTopologyManager topologyManager, - ShutdownManager shutdownManager) { + protected Map createSubEndpoints(RmqChannelManager channelManager, RmqTopologyManager topologyManager) { if (sub == null || sub.isEmpty()) { return Collections.emptyMap(); } Map map = new HashMap<>(); - sub.forEach((k, v) -> map.put(k, v.create(channelManager, topologyManager, shutdownManager))); + sub.forEach((k, v) -> map.put(k, v.create(channelManager, topologyManager))); return map; } diff --git a/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/connection/AMQPConnectionFactoryFactory.java b/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/connection/AMQPConnectionFactoryFactory.java index 6393a44..93ef98b 100644 --- a/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/connection/AMQPConnectionFactoryFactory.java +++ b/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/connection/AMQPConnectionFactoryFactory.java @@ -23,7 +23,6 @@ import com.rabbitmq.client.ConnectionFactory; import io.bootique.annotation.BQConfig; import io.bootique.annotation.BQConfigProperty; -import io.bootique.di.Injector; @BQConfig @JsonTypeName("amqp") @@ -36,7 +35,7 @@ public class AMQPConnectionFactoryFactory extends ConnectionFactoryFactory { private int port = -1; @Override - protected ConnectionFactory configureFactory(ConnectionFactory factory, String connectionName, Injector injector) { + protected ConnectionFactory configureFactory(ConnectionFactory factory, String connectionName) { if (username != null) { factory.setUsername(username); } @@ -55,7 +54,7 @@ protected ConnectionFactory configureFactory(ConnectionFactory factory, String c factory.setPort(port); - return super.configureFactory(factory, connectionName, injector); + return super.configureFactory(factory, connectionName); } @BQConfigProperty diff --git a/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/connection/ConnectionFactoryFactory.java b/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/connection/ConnectionFactoryFactory.java index 4b00e79..640846f 100644 --- a/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/connection/ConnectionFactoryFactory.java +++ b/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/connection/ConnectionFactoryFactory.java @@ -24,7 +24,6 @@ import io.bootique.annotation.BQConfig; import io.bootique.annotation.BQConfigProperty; import io.bootique.config.PolymorphicConfiguration; -import io.bootique.di.Injector; @BQConfig @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl = AMQPConnectionFactoryFactory.class) @@ -40,11 +39,11 @@ public abstract class ConnectionFactoryFactory implements PolymorphicConfigurati private Boolean topologyRecovery; private Long networkRecoveryInterval; - public ConnectionFactory createConnectionFactory(String connectionName, Injector injector) { - return configureFactory(new ConnectionFactory(), connectionName, injector); + public ConnectionFactory createConnectionFactory(String connectionName) { + return configureFactory(new ConnectionFactory(), connectionName); } - protected ConnectionFactory configureFactory(ConnectionFactory factory, String connectionName, Injector injector) { + protected ConnectionFactory configureFactory(ConnectionFactory factory, String connectionName) { // let's preserve RMQ defaults if parameters are not setup explicitly if (requestedChannelMax != null) { diff --git a/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/connection/URIConnectionFactoryFactory.java b/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/connection/URIConnectionFactoryFactory.java index 5344fbc..7b38bac 100644 --- a/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/connection/URIConnectionFactoryFactory.java +++ b/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/connection/URIConnectionFactoryFactory.java @@ -23,7 +23,6 @@ import com.rabbitmq.client.ConnectionFactory; import io.bootique.annotation.BQConfig; import io.bootique.annotation.BQConfigProperty; -import io.bootique.di.Injector; @BQConfig @JsonTypeName("uri") @@ -32,13 +31,13 @@ public class URIConnectionFactoryFactory extends ConnectionFactoryFactory { private String uri; @Override - protected ConnectionFactory configureFactory(ConnectionFactory factory, String connectionName, Injector injector) { + protected ConnectionFactory configureFactory(ConnectionFactory factory, String connectionName) { try { factory.setUri(uri); } catch (Exception e) { throw new RuntimeException("Failed to initialize RabbitMQ URI connection factory", e); } - return super.configureFactory(factory, connectionName, injector); + return super.configureFactory(factory, connectionName); } @BQConfigProperty diff --git a/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/pubsub/RmqSubEndpointFactory.java b/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/pubsub/RmqSubEndpointFactory.java index 418f4ae..26986a5 100644 --- a/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/pubsub/RmqSubEndpointFactory.java +++ b/bootique-rabbitmq-client/src/main/java/io/bootique/rabbitmq/client/pubsub/RmqSubEndpointFactory.java @@ -24,6 +24,7 @@ import io.bootique.rabbitmq.client.topology.*; import io.bootique.shutdown.ShutdownManager; +import javax.inject.Inject; import java.util.Objects; /** @@ -32,6 +33,8 @@ @BQConfig public class RmqSubEndpointFactory { + private final ShutdownManager shutdownManager; + private String connection; private String exchangeConfig; private String exchangeName; @@ -40,10 +43,12 @@ public class RmqSubEndpointFactory { private String routingKey; private boolean autoAck = true; - public RmqSubEndpoint create( - RmqChannelManager channelManager, - RmqTopologyManager topologyManager, - ShutdownManager shutdownManager) { + @Inject + public RmqSubEndpointFactory(ShutdownManager shutdownManager) { + this.shutdownManager = shutdownManager; + } + + public RmqSubEndpoint create(RmqChannelManager channelManager, RmqTopologyManager topologyManager) { Objects.requireNonNull(connection, "Subscriber connection name is undefined"); RmqEndpointDriver driver = new RmqEndpointDriver(channelManager, connection); diff --git a/bootique-rabbitmq-junit5/src/main/java/io/bootique/rabbitmq/junit5/tester/ConnectionPropertyBuilder.java b/bootique-rabbitmq-junit5/src/main/java/io/bootique/rabbitmq/junit5/tester/ConnectionPropertyBuilder.java index 041a71d..80d6ac4 100644 --- a/bootique-rabbitmq-junit5/src/main/java/io/bootique/rabbitmq/junit5/tester/ConnectionPropertyBuilder.java +++ b/bootique-rabbitmq-junit5/src/main/java/io/bootique/rabbitmq/junit5/tester/ConnectionPropertyBuilder.java @@ -27,8 +27,8 @@ */ public class ConnectionPropertyBuilder { - private String propertyPrefix; - private BQCoreModuleExtender extender; + private final String propertyPrefix; + private final BQCoreModuleExtender extender; protected ConnectionPropertyBuilder(String propertyPrefix, BQCoreModuleExtender extender) { this.propertyPrefix = propertyPrefix; diff --git a/bootique-rabbitmq-junit5/src/main/java/io/bootique/rabbitmq/junit5/tester/TestConnectionFactoryFactory.java b/bootique-rabbitmq-junit5/src/main/java/io/bootique/rabbitmq/junit5/tester/TestConnectionFactoryFactory.java index 922aed0..59b971a 100644 --- a/bootique-rabbitmq-junit5/src/main/java/io/bootique/rabbitmq/junit5/tester/TestConnectionFactoryFactory.java +++ b/bootique-rabbitmq-junit5/src/main/java/io/bootique/rabbitmq/junit5/tester/TestConnectionFactoryFactory.java @@ -28,6 +28,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.inject.Inject; + @JsonTypeName("bqrmqtest") // must be able to deserialize over the existing configs, so instruct Jackson to be lenient @JsonIgnoreProperties(ignoreUnknown = true) @@ -35,8 +37,15 @@ public class TestConnectionFactoryFactory extends ConnectionFactoryFactory { private static final Logger LOGGER = LoggerFactory.getLogger(TestConnectionFactoryFactory.class); + private final Injector injector; + + @Inject + public TestConnectionFactoryFactory(Injector injector) { + this.injector = injector; + } + @Override - protected ConnectionFactory configureFactory(ConnectionFactory factory, String connectionName, Injector injector) { + protected ConnectionFactory configureFactory(ConnectionFactory factory, String connectionName) { RmqTester tester = injector.getInstance(Key.get(RmqTester.class, connectionName)); String url = tester.getAmqpUrl(); @@ -49,6 +58,6 @@ protected ConnectionFactory configureFactory(ConnectionFactory factory, String c throw new RuntimeException("Failed to initialize RabbitMQ URI connection factory", e); } - return super.configureFactory(factory, connectionName, injector); + return super.configureFactory(factory, connectionName); } }