问题
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