SpringBoot整合RabbitMQ,手动订阅配置

 

1、yml中rabbitmq配置

 


#YML部分配置
spring:
  # mq配置
  rabbitmq:
    username: admin
    password: admin
    publisher-confirms: true 
    publisher-returns: true
    listener: 
      simple: 
        acknowledge-mode: manual #消费手动确认

 

2、消费者代码片段

@RabbitListener(queuesToDeclare = {@Queue("test.queue")})
	public void getMessage(Message message, Channel channel) throws IOException {
		
		try{
            byte[] bodyByte = message.getBody();
		    String body = String.valueOf(SerializationUtils.deserialize(bodyByte));
            log.info("原始数据:" + body);
        }catch(Exception e){
            log.error("监听异常:" + e.getMessage());
        }finally{
            //手工ack
		    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
		

	}


Tips:成长从点滴开始

你可能感兴趣的:(SpringBoot整合RabbitMQ,手动订阅配置)