package com.hnnd.mq.NackAndReQueue;import com.rabbitmq.client.AMQP;import com.rabbitmq.client.Channel;import com.rabbitmq.client.DefaultConsumer;import com.rabbitmq.client.Envelope;import org.springframework.util.StringUtils;import java.io.IOException;/** * Created by Administrator on 2018/10/19. */publicclassAngleCustomConsumerextendsDefaultConsumer{private Channel channel;/** * Constructs a new instance and records its association to the passed-in channel. ** @param channel the channel to which this consumer is attached */publicAngleCustomConsumer(Channel channel){super(channel);this.channel = channel;}/** * 处理消息
* @param consumerTag
* @param envelope
* @param properties
* @param body
* @throws IOException */publicvoidhandleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,byte[] body)throws IOException
{try{
Thread.currentThread().sleep(1000);}catch(InterruptedException e){
e.printStackTrace();}
System.out.println(properties.getHeaders());
String num = properties.getHeaders().get("num").toString();if(num.equals("1")){
System.out.println("业务系统处理消息异常消息重新回队列"+newString(body));
channel.basicNack(envelope.getDeliveryTag(),false,true);}else{
System.out.println("自定义的消息消费端");
System.out.println("consumerTag="+ consumerTag);
System.out.println("envelope="+ envelope);
System.out.println("properties="+ properties);
System.out.println("body="+newString(body));//消费端的手动签收,加入我关闭手动签收,也关闭自动签收,那么消费端只会接收到一条消息
channel.basicAck(envelope.getDeliveryTag(),false);}}}