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; } }
有疑问加w,dreamingmymoon