如下配置消费队列,期望会自动创建注解中的queue和exchange
@Component@RabbitListener(bindings =@QueueBinding(
value =@Queue(value =MqDefConstant.QUEUE_DEAL_ORDER_REFUND_1, durable ="true", autoDelete ="false"),
exchange =@Exchange(value =MqDefConstant.EXCHANGE_ORDER_REFUND, type =ExchangeTypes.FANOUT)), containerFactory ="rabbitListenerContainerFactory")publicclassOrderRefundConsumer{privatestaticfinalLogger logger =LoggerFactory.getLogger(OrderRefundConsumer.class);@RabbitHandlerpublicvoidconsumer(OrderRefundMsg msg){//...}}
但是启动报错,日志如下
com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue 'RETAILSTORE.TOPIC.ALIPAYNOTIFYTOPIC' in vhost '/', class-id=50, method-id=10)
org.springframework.amqp.rabbit.listener.QueuesNotAvailableException: Cannot prepare queue for listener. Either the queue doesn't exist or the broker will not allow us to use it.
查看RabbitListener定义
大意如下,如果定义了RabbitAdmin的Bean,就会自动创建队列
修改rabbit配置,增加RabbitAdmin定义,如下
@Configuration@EnableRabbitpublicclassRabbitConfig{@BeanpublicRabbitAdminrabbitAdmin(ConnectionFactory connectionFactory){RabbitAdmin rabbitAdmin =newRabbitAdmin(connectionFactory);// 服务启动时候开启自动启动
rabbitAdmin.setAutoStartup(true);return rabbitAdmin;}}
再次启动,成功创建。
版权归原作者 呼哇呼哇time 所有, 如有侵权,请联系我们删除。