1. 程式人生 > >java rabbitmq ack訊息確認機制

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