How do I create/configure my own AMQP ContainerFactory in Spring Boot?

心不动则不痛 提交于 2019-12-25 01:53:47

问题


I added this as an attempt to create and configure my own SimpleRabbitListenerContainerFactory to replace the default

@Bean
open fun myFactory(cf: ConnectionFactory): SimpleRabbitListenerContainerFactory {
    val factory = SimpleRabbitListenerContainerFactory()
    factory.setConnectionFactory(cf)
    factory.setDefaultRequeueRejected(false)
    factory.setAfterReceivePostProcessors(MessagePostProcessor {
        it.messageProperties.contentType = MediaType.APPLICATION_JSON_VALUE
        return@MessagePostProcessor it
    })
    return factory
}

When the code is running I still see

o.s.a.r.listener.BlockingQueueConsumer   : Rejecting messages (requeue=true)

which leads me to believe that spring boot isn't using my ContainerFactory. I suppose I don't really care if it's using mine if I can configure it correctly. What's the best way to resolve this?


回答1:


See the documentation.

The framework looks for a factory named rabbitListenerContainerFactory by default, unless you set the containerFactory property on the annotation.

By default, the infrastructure looks for a bean named rabbitListenerContainerFactory as the source for the factory to use to ...

To override Boot's bean, name it rabbitListenerContainerFactory.

When doing that, it's generally best to use the configurer so your boot properties will be applied...

@Bean(name = "rabbitListenerContainerFactory")
public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(
        SimpleRabbitListenerContainerFactoryConfigurer configurer,
        ConnectionFactory connectionFactory) {

    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    configurer.configure(factory, connectionFactory);

    factory.setDefaultRequeueRejected(false)
    factory.setAfterReceivePostProcessors(MessagePostProcessor {
        it.messageProperties.contentType = MediaType.APPLICATION_JSON_VALUE
        return@MessagePostProcessor it
    })

    return factory;
}


来源:https://stackoverflow.com/questions/55148241/how-do-i-create-configure-my-own-amqp-containerfactory-in-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!