使用@RabbitListener注解接受消息时如何进行手动ack

由于进行手动ack需要调用当前channel的basicAck方法,所以在注解注解的处理方法的入参中需要加入channel入参以及deleveryTag入参。下面看一个示例

/**
* @param message :解码后的消息
* @param delicveryTag :使用@Header接口获取messageProperties中的DELIVERY_TAG属性。
* @param channel : 接受消息所使用的信道
*/
@RabbitListener(queues = "sys.topic.login.message")
    public void process1(@Payload String message, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel)throws Exception {
        if(message.equals("1")){
            int i = 1/0;
        }
        channel.basicAck(deliveryTag,false);
        System.out.print("这里是接收者1答应消息: ");
        System.out.println("SYS_TOPIC_ORDER_CALCULATE_ZZ_FEE process1  : " + message);
    }

猜你喜欢

转载自blog.csdn.net/m0_37556444/article/details/82627723