RabbitMQ消费client注解模式 @RabbitListener自动创建队列

@Component
@Slf4j
public class RabbitMqConsumer {

    @Autowired
    DingdingUtil dingdingUtil;

    @RabbitHandler
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue("my_queue"),
                    exchange = @Exchange(value = "my_exchange", type = ExchangeTypes.FANOUT)
            ))
    public void processCreate(String message) {
        log.info("------------------Received: " + message + "   ----------------------------------------------------------------------");

    }


}

必须有以下配置类,否则无法自动创建队列

@Configuration
@EnableRabbit
public class RabbitConfig {

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        // 服务启动时候开启自动启动
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }

}

 

posted @ 2023-02-22 13:35  然然1907  阅读(455)  评论(0编辑  收藏  举报