amqp - ThreadChannelConnectionFactory 是否应该与 RabbitAdmin 自动声明兼容?

标签 amqp spring-amqp

如下面的(失败)测试所示,创建连接时未声明可声明对象:

private const val QUEUE = "test"

@SpringBootTest
class ThreadChannelConnectionFactoryTest {

    @Autowired
    private lateinit var admin: AmqpAdmin

    @Test
    fun queueExists() {
        assertThat(admin.getQueueInfo(QUEUE)).isNotNull
    }

    @SpringBootApplication(proxyBeanMethods = false)
    protected class TestApplication {

        @Bean
        fun queue() = QueueBuilder.nonDurable(QUEUE).build()

        @Bean
        fun connectionFactory() = ThreadChannelConnectionFactory(RabbitConnectionFactoryBean().rabbitConnectionFactory)
    }
}

注释掉 connectionFactory bean 定义使测试通过,因为 Spring Boot 随后会创建一个老式的 CachingConnectionFactory。

这有什么充分的理由吗?还是只是被忽视了?如果我需要两个功能集,我有什么选择?

  • ThreadLocal channel
  • 自动可声明声明

编辑:从 2.3.1 开始,以下解决方法似乎有效:

class ConnectionListenerFiringThreadChannelConnectionFactory(connectionFactory: ConnectionFactory, isPublisher: Boolean) : ThreadChannelConnectionFactory(connectionFactory) {

    init {
        if (!isPublisher) {
            setPublisherConnectionFactory(ConnectionListenerFiringThreadChannelConnectionFactory(connectionFactory, true))
        }
    }

    constructor(connectionFactory: ConnectionFactory) : this(connectionFactory, false)

    override fun createConnection(): Connection {
        val connection = super.createConnection()
        connectionListener.onCreate(connection)

        return connection
    }
}

最佳答案

这是一个错误;您的解决方法是正确的。

https://github.com/spring-projects/spring-amqp/issues/1268

关于amqp - ThreadChannelConnectionFactory 是否应该与 RabbitAdmin 自动声明兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64877681/

相关文章:

java - 使用spring amqp模板清除rabbitmq队列?

rabbitmq - Spring-amqp 两个 TTL 不同的队列

python - 使用 Python 绑定(bind)在 Qpid Proton 中设置自定义消息属性

python - 如何删除或推迟 AMQP 队列中的消息

go - 是否可以向 protobuf 服务添加自定义方法类型?

java - 如何添加一个额外的监听器来监听 Spring AMQP 队列?

rabbitmq - 让 RabbitMQ 只监听环回接口(interface)?

javascript - 如何使用 amqplib 库中的 channel.assertQueue 函数用于 node.JS?

xml - Grails(spring-amqp) - 通过 XML 配置使用 SSL 连接 RabbitMQ

java - 如何优雅地停止使用 @RabbitListener 消费消息