java rabbitmq ack消息确认机制

ackage com.example.demo.ConsumerDemo;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ConsumerDemo implements ChannelAwareMessageListener{

@Override
public void onMessage(Message message, Channel channel){
String messageRec = new String(message.getBody());
System.out.println("接收到的字符串消息是 => " + messageRec);
try{
调用方法
//消息消费掉
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
}catch(Exception ex){
//消息重新回到队列
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false,true);
logger.error(“Exception”,ex);
}
}
}

spring-mq配置文件:手动消息确认
spring.rabbitmq.listener.simple.acknowledge-mode=manual

你可能感兴趣的:(java rabbitmq ack消息确认机制)