在使用Spring集成RabbitMQ時,須要監聽RabbiMQ消息。通常選擇性實現下面倆接口:java
MessageListener 只能獲取到Message信息redis
ChannelAwareMessageListener 能獲取到Message以及Channel信息api
不一樣點在於channel能夠手工確認消息以及拒絕ide
public class TestMessageListener implements ChannelAwareMessageListener { private static final Logger logger = LoggerFactory.getLogger(TraceAnalyzeListener.class); @Autowired private TraceAnalyzeService traceAnalyzeService; @Autowired private ElasticOperationService elasticOperationService; @Autowired private RedisTemplate<String, String> redisTemplate; @Autowired private TraceMessageSender traceMessageSender; @Override public void onMessage(Message message, Channel channel) throws Exception { //消息的標識,false只確認當前一個消息收到,true確認全部consumer得到的消息 //channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); //確認成功收到消息 //ack返回false,並從新回到隊列,api裏面解釋得很清楚 //channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true); //拒絕消息 //channel.basicReject(message.getMessageProperties().getDeliveryTag(), true); } }