spring-amqp icon indicating copy to clipboard operation
spring-amqp copied to clipboard

Factories created by LocalizedQueueConnectionFactory do not inherit customizations applied to defaultConnectionFactory

Open vkochnev opened this issue 3 years ago • 2 comments

Affects Version(s): 2.3.11

ConnectionFactory created by Spring Boot can be customized in several ways, ConnectionFactoryCustomizers, ConnectionNameStrategy to name a few. Also there is at least one post processor RabbitConnectionFactoryMetricsPostProcessor enabling metrics.

Code creating connection factories inside of LocalizedQueueConnectionFactory uses minimal configuration effectively ignoring all the customizations. This issue might be considered to span across Spring Amqp, Spring Boot and Spring Cloud Stream projects.

I believe it can be solved with introduction of ConnectionFactoryBuilder bean (something similar to RestTemplateBuilder) which will produce connection factories customized in similar fashion, so LocalizedQueueConnectionFactory will be able to reuse it. Or maybe there might be another approach I don't see.

vkochnev avatar Nov 03 '21 14:11 vkochnev

LocalizedQueueConnectionFactory.createConnectionFactory() is protected for this reason; you can subclass, override that method, call it, and further modify the factory.

Is there something else that a builder would provide?

garyrussell avatar Nov 09 '21 20:11 garyrussell

Builder can provide the single source of truth for factory configuration. I do can extend LocalizedQueueConnectionFactory, but for me personally there are several difficulties:

  • I use Spring Cloud Stream and this factory is created like this https://github.com/spring-cloud/spring-cloud-stream-binder-rabbit/blob/09c8b9bdc5a172b4034b32ff4d70b094ccb616b7/spring-cloud-stream-binder-rabbit/src/main/java/org/springframework/cloud/stream/binder/rabbit/RabbitMessageChannelBinder.java#L257 so I'll have to override related classes and configurations, which is not straightforward.
  • I'll need to figure out what configurations were applied to default factory and I definitely will miss something cause there are several ways to configure that factory.
  • If factory is post-processed (e.g. wrapped), then making a copy might be close to impossible without hardcoding an exact variant and this approach is not supportable.

What implemented in Spring Cloud Stream doesn't work actually, I can file an issue to Spring Cloud Stream Rabbit Binder for example (I linked one minor issue already), but I feel that solution should be more comprehensive.

vkochnev avatar Nov 10 '21 19:11 vkochnev